Changeset 3432ddb in mainline for uspace/app/df/df.c


Ignore:
Timestamp:
2013-08-07T20:37:22Z (11 years ago)
Author:
Manuele Conti <conti.ma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
81bd5f4
Parents:
781408e
Message:

Implement human readable command option.

File:
1 edited

Legend:

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

    r781408e r3432ddb  
    4747#define NAME  "df"
    4848
    49 #define HEADER_TABLE "Filesystem     %4u-blocks           Used      Available Used%% Mounted on\n"
     49#define HEADER_TABLE    "Filesystem     %4u-blocks           Used      Available Used%% Mounted on"
     50#define HEADER_TABLE_HR "Filesystem           Size           Used      Available Used%% Mounted on"
    5051
    5152#define PERCENTAGE(x, tot) ((unsigned long long) (100L * (x) / (tot))) 
     
    5657
    5758static unsigned int unit_size;
     59static unsigned int human_readable;
    5860
     61static int size_to_human_readable(char buf[], uint64_t bytes)
     62static void print_header(void);
    5963static void print_statfs(struct statfs *, char *, char *);
    6064static void print_usage(void);
     
    6670       
    6771        unit_size = 512;
     72        human_readable = 0;
    6873
    6974        /******************************************/
     
    7378                switch(optres) {
    7479                case 'h':
     80                        human_readable = 1;
    7581                        break;
    7682
     
    111117        LIST_INITIALIZE(mtab_list);
    112118        get_mtab_list(&mtab_list);
    113         printf(HEADER_TABLE, unit_size);
     119        print_header();
    114120        list_foreach(mtab_list, cur) {
    115121                mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
     
    122128}
    123129
     130static int size_to_human_readable(char buf[], uint64_t bytes)
     131{
     132        const char *units = "BkMGTPEZY";
     133        int i = 0;
     134        int limit;
     135
     136        limit = str_length(units);
     137        while (bytes >= 1024) {
     138                if (i >= limit)
     139                        return -1;
     140                bytes /= 1024;
     141                i++;
     142        }
     143        snprintf(buf, 6, "%4llu%c", (unsigned long long)bytes, units[i]);
     144
     145        return 0;
     146}
     147
     148static void print_header(void)
     149{
     150        if (human_readable)
     151                printf(HEADER_TABLE_HR);
     152        else
     153                printf(HEADER_TABLE, unit_size);
     154        putchar('\n');
     155}
     156
    124157static void print_statfs(struct statfs *st, char *name, char *mountpoint)
    125158{
    126159        printf("%10s", name);
    127         printf(" %15llu %14llu %14llu %4llu%% %s\n",
    128                 FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),                              /* Blocks     */
    129                 FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, unit_size),                /* Used       */
    130                 FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),                               /* Available  */
    131                 (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,        /* Used%      */
    132                 mountpoint                                                                     /* Mounted on */
    133         );
     160       
     161        if (human_readable) {
     162                char tmp[1024];
     163                size_to_human_readable(tmp, st->f_blocks *  st->f_bsize);
     164                printf(" %14s", tmp);                                                                 /* Size       */
     165                size_to_human_readable(tmp, (st->f_blocks - st->f_bfree)  *  st->f_bsize);
     166                printf(" %14s", tmp);                                                                 /* Used       */
     167                size_to_human_readable(tmp, st->f_bfree *  st->f_bsize);
     168                printf(" %14s", tmp);                                                                 /* Available  */
     169                printf(" %4llu%% %s\n",
     170                        (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,        /* Used%      */
     171                        mountpoint                                                                     /* Mounted on */
     172                );
     173        }
     174        else
     175                printf(" %15llu %14llu %14llu %4llu%% %s\n",
     176                        FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),                              /* Blocks     */
     177                        FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, unit_size),                /* Used       */
     178                        FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),                               /* Available  */
     179                        (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,        /* Used%      */
     180                        mountpoint                                                                     /* Mounted on */
     181                );
     182       
    134183}
    135184
Note: See TracChangeset for help on using the changeset viewer.