Changeset 737227c7 in mainline


Ignore:
Timestamp:
2013-09-15T21:13:35Z (11 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
651c8db
Parents:
73fbcbb
Message:

some minor changes to the df application and cstyle fixes

File:
1 edited

Legend:

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

    r73fbcbb r737227c7  
    5050#define HEADER_TABLE_HR "Filesystem           Size           Used      Available Used%% Mounted on"
    5151
    52 #define PERCENTAGE(x, tot) ((unsigned long long) (100L * (x) / (tot)))
     52#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
    5353#define FSBK_TO_BK(x, fsbk, bk) \
    5454        (((fsbk) != 0 && (fsbk) < (bk)) ? \
     
    5959static unsigned int human_readable;
    6060
    61 static int size_to_human_readable(char buf[], uint64_t bytes);
     61static void size_to_human_readable(char *buf, uint64_t bytes);
    6262static void print_header(void);
    6363static void print_statfs(struct statfs *, char *, char *);
     
    9090 
    9191                case ':':
    92                         fprintf(stderr, "Option -%c requires an operand\n", optopt);
     92                        fprintf(stderr, "Option -%c requires an operand\n",
     93                            optopt);
    9394                        errflg++;
    9495                        break;
     
    100101
    101102                default:
    102                         fprintf(stderr, "Unknown error while parsing command line options.");
     103                        fprintf(stderr,
     104                            "Unknown error while parsing command line options");
    103105                        errflg++;
    104106                        break;
     
    107109
    108110        if (optind > argc) {
    109                 fprintf(stderr, "Too many input parameter\n");
     111                fprintf(stderr, "Too many input parameters\n");
    110112                errflg++;
    111113        }
     
    118120        LIST_INITIALIZE(mtab_list);
    119121        get_mtab_list(&mtab_list);
     122
    120123        print_header();
    121124        list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
     
    123126                print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
    124127        }
     128
    125129        putchar('\n');
    126130        return 0;
    127131}
    128132
    129 static int size_to_human_readable(char buf[], uint64_t bytes)
     133static void size_to_human_readable(char *buf, uint64_t bytes)
    130134{
    131135        const char *units = "BkMGTPEZY";
    132136        int i = 0;
    133         int limit;
    134 
    135         limit = str_length(units);
     137
    136138        while (bytes >= 1024) {
    137                 if (i >= limit)
    138                         return -1;
    139139                bytes /= 1024;
    140140                i++;
    141141        }
    142         snprintf(buf, 6, "%4llu%c", (unsigned long long)bytes, units[i]);
    143 
    144         return 0;
     142
     143        snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
    145144}
    146145
     
    151150        else
    152151                printf(HEADER_TABLE, unit_size);
     152
    153153        putchar('\n');
    154154}
     
    156156static void print_statfs(struct statfs *st, char *name, char *mountpoint)
    157157{
     158        uint64_t const used_blocks = st->f_blocks - st->f_bfree;
     159        unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
     160
    158161        printf("%10s", name);
    159162
    160163        if (human_readable) {
    161164                char tmp[1024];
     165
     166                /* Print size */
    162167                size_to_human_readable(tmp, st->f_blocks *  st->f_bsize);
    163                 printf(" %14s", tmp);                                                                  /* Size       */
    164                 size_to_human_readable(tmp, (st->f_blocks - st->f_bfree)  *  st->f_bsize);
    165                 printf(" %14s", tmp);                                                                  /* Used       */
     168                printf(" %14s", tmp);
     169
     170                /* Number of used blocks */
     171                size_to_human_readable(tmp, used_blocks  *  st->f_bsize);
     172                printf(" %14s", tmp);
     173
     174                /* Number of available blocks */
    166175                size_to_human_readable(tmp, st->f_bfree *  st->f_bsize);
    167                 printf(" %14s", tmp);                                                                  /* Available  */
    168                 printf(" %4llu%% %s\n",
    169                         (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,        /* Used%      */
    170                         mountpoint                                                                     /* Mounted on */
    171                 );
    172         }
    173         else
    174                 printf(" %15llu %14llu %14llu %4llu%% %s\n",
    175                         FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),                              /* Blocks     */
    176                         FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, unit_size),                /* Used       */
    177                         FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),                               /* Available  */
    178                         (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L,        /* Used%      */
    179                         mountpoint                                                                     /* Mounted on */
    180                 );
     176                printf(" %14s", tmp);
     177
     178                /* Percentage of used blocks */
     179                printf(" %4llu%%",
     180                    PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks));
     181
     182                /* Mount point */
     183                printf(" %s\n", mountpoint);
     184        } else {
     185                /* Blocks / Used blocks / Available blocks / Used% / Mounted on */
     186                printf(" %15llu %14llu %14llu %4u%% %s\n",
     187                    FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),
     188                    FSBK_TO_BK(used_blocks, st->f_bsize, unit_size),
     189                    FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),
     190                    perc_used,
     191                    mountpoint);
     192        }
    181193
    182194}
Note: See TracChangeset for help on using the changeset viewer.