Changeset be99d30 in mainline


Ignore:
Timestamp:
2013-07-18T22:20:11Z (11 years ago)
Author:
Manuele Conti <conti.ma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8a8a08d1
Parents:
e2f706e
Message:

Fix output style.
Add argument parser.
Add usage function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/df/df.c

    re2f706e rbe99d30  
    3939#include <unistd.h>
    4040#include <stdint.h>
     41#include <getopt.h>
    4142#include <sys/statfs.h>
    4243#include <errno.h>
     
    4647#define NAME  "df"
    4748
    48 #define HEADER_TABLE "Filesystem    512-blocks      Used      Available  Used% Mounted on"
     49#define HEADER_TABLE "Filesystem     512-blocks           Used      Available Used% Mounted on"
    4950
    5051#define PERCENTAGE(x, tot) ((unsigned long long) (100L * (x) / (tot))) 
     52#define FSBK_TO_BK(x, fsbk, bk) \
     53        (((fsbk) != 0 && (fsbk) < (bk)) ? \
     54                (unsigned long long) ((x) / ((bk) / (fsbk))) : \
     55                (unsigned long long) ((x) * ((fsbk) / (bk))))
     56
     57static void print_statfs(struct statfs *, char *, char *);
     58static void print_usage(void);
    5159
    5260int main(int argc, char *argv[])
    5361{
     62        int optres, errflg = 0;
    5463        struct statfs st;
     64       
     65        /******************************************/
     66        /*   Parse command line options...        */
     67        /******************************************/
     68        while ((optres = getopt(argc, argv, ":hi")) != -1) {
     69                switch(optres) {
     70                case 'h':
     71                        break;
     72                case 'i':
     73                        break;
     74   
     75                case ':':       
     76                        fprintf(stderr, "Option -%c requires an operand\n", optopt);
     77                        errflg++;
     78                        break;
    5579
     80                case '?':
     81                        fprintf(stderr, "Unrecognized option: -%c\n", optopt);
     82                        errflg++;
     83                        break;
     84
     85                default:
     86                        fprintf(stderr, "Unknown error while parsing command line options.");
     87                        errflg++;
     88                        break;
     89                }
     90        }
     91
     92        if (optind > argc) {
     93                fprintf(stderr, "Too many input parameter\n");
     94                errflg++;
     95        }
     96       
     97        if (errflg) {
     98                print_usage();
     99                return 1;
     100        }
     101       
    56102        LIST_INITIALIZE(mtab_list);
    57103        get_mtab_list(&mtab_list);
     
    61107                    link);
    62108                statfs(mtab_ent->mp, &st);
    63                 printf("block size:%lu\n", (unsigned long)st.f_bsize);
    64                 printf("%13s %15llu %9llu %9llu %3llu%% %s\n",
    65                         mtab_ent->fs_name,
    66                         (unsigned long long) st.f_blocks * st.f_bsize,
    67                         (unsigned long long) (st.f_blocks - st.f_bfree) * st.f_bsize,
    68                         (unsigned long long) st.f_bfree * st.f_bsize,
    69                         (st.f_blocks)?PERCENTAGE(st.f_blocks - st.f_bfree, st.f_blocks):0L,
    70                         mtab_ent->mp);
     109                print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
    71110        }
    72111        putchar('\n'); 
     
    74113}
    75114
     115static void print_statfs(struct statfs *st, char *name, char *mountpoint)
     116{
     117        printf("%10s", name);
     118        printf(" %14llu %14llu %14llu %4llu%% %s\n",
     119                FSBK_TO_BK(st->f_blocks, st->f_bsize, 512),                              /* Blocks     */
     120                FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, 512),                /* Used       */
     121                FSBK_TO_BK(st->f_bfree, st->f_bsize, 512),                               /* Available  */
     122                (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,  /* Used%      */
     123                mountpoint                                                               /* Mounted on */
     124        );
     125}
     126
     127static void print_usage(void)
     128{
     129  printf("syntax: %s [-h] [-i]\n", NAME);
     130  printf("  h : \"Human-redable\" output.\n"); 
     131  printf("  i : Include statistics on the number of free inodes. \n");
     132  printf("\n");
     133}
     134
    76135/** @}
    77136 */
Note: See TracChangeset for help on using the changeset viewer.