Changes in uspace/app/df/df.c [120d5bc:582a0b8] in mainline


Ignore:
File:
1 edited

Legend:

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

    r120d5bc r582a0b8  
    2828
    2929/** @addtogroup df
    30  * @brief Print amounts of free and used disk space.
     30 * @brief Df utility.
    3131 * @{
    3232 */
     
    3535 */
    3636
    37 #include <cap.h>
    38 #include <stdbool.h>
    3937#include <stdio.h>
    4038#include <stdlib.h>
     
    4846#define NAME  "df"
    4947
    50 #define HEADER_TABLE     "Filesystem           Size           Used      Available Used%% Mounted on"
    51 #define HEADER_TABLE_BLK "Filesystem  Blk. Size     Total        Used   Available Used%% Mounted on"
     48#define HEADER_TABLE    "Filesystem     %4u-blocks           Used      Available Used%% Mounted on"
     49#define HEADER_TABLE_HR "Filesystem           Size           Used      Available Used%% Mounted on"
    5250
    5351#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
    54 
    55 static bool display_blocks;
    56 
    57 static int size_to_human_readable(uint64_t, size_t, char **);
     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 unsigned int unit_size;
     58static unsigned int human_readable;
     59
     60static void size_to_human_readable(char *buf, uint64_t bytes);
    5861static void print_header(void);
    59 static int print_statfs(struct statfs *, char *, char *);
     62static void print_statfs(struct statfs *, char *, char *);
    6063static void print_usage(void);
    6164
     
    6467        int optres, errflg = 0;
    6568        struct statfs st;
    66         int rc;
    67 
    68         display_blocks = false;
    69 
    70         /* Parse command-line options */
    71         while ((optres = getopt(argc, argv, ":ubh")) != -1) {
     69
     70        unit_size = 512;
     71        human_readable = 0;
     72
     73        /******************************************/
     74        /*   Parse command line options...        */
     75        /******************************************/
     76        while ((optres = getopt(argc, argv, ":uhb:")) != -1) {
    7277                switch(optres) {
    73                 case 'h':
     78                case 'u':
    7479                        print_usage();
    7580                        return 0;
    7681
     82                case 'h':
     83                        human_readable = 1;
     84                        break;
     85
    7786                case 'b':
    78                         display_blocks = true;
     87                        str_uint32_t(optarg, NULL, 0, 0, &unit_size);
    7988                        break;
    8089 
     
    114123        list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
    115124                if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
    116                         rc = print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
    117                         if (rc != EOK)
    118                                 return 1;
     125                        print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
    119126                } else {
    120127                        fprintf(stderr, "Cannot get information for '%s' (%d).\n",
     
    127134}
    128135
    129 static int size_to_human_readable(uint64_t nblocks, size_t block_size, char **rptr)
    130 {
    131         cap_spec_t cap;
    132 
    133         cap_from_blocks(nblocks, block_size, &cap);
    134         cap_simplify(&cap);
    135         return cap_format(&cap, rptr);
     136static void size_to_human_readable(char *buf, uint64_t bytes)
     137{
     138        const char *units = "BkMGTPEZY";
     139        int i = 0;
     140
     141        while (bytes >= 1024) {
     142                bytes /= 1024;
     143                i++;
     144        }
     145
     146        snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
    136147}
    137148
    138149static void print_header(void)
    139150{
    140         if (!display_blocks)
    141                 printf(HEADER_TABLE);
     151        if (human_readable)
     152                printf(HEADER_TABLE_HR);
    142153        else
    143                 printf(HEADER_TABLE_BLK);
     154                printf(HEADER_TABLE, unit_size);
    144155
    145156        putchar('\n');
    146157}
    147158
    148 static int print_statfs(struct statfs *st, char *name, char *mountpoint)
     159static void print_statfs(struct statfs *st, char *name, char *mountpoint)
    149160{
    150161        uint64_t const used_blocks = st->f_blocks - st->f_bfree;
    151162        unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
    152         char *str;
    153         int rc;
    154163
    155164        printf("%10s", name);
    156165
    157         if (!display_blocks) {
     166        if (human_readable) {
     167                char tmp[1024];
     168
    158169                /* Print size */
    159                 rc = size_to_human_readable(st->f_blocks, st->f_bsize, &str);
    160                 if (rc != EOK)
    161                         goto error;
    162                 printf(" %14s", str);
    163                 free(str);
     170                size_to_human_readable(tmp, st->f_blocks *  st->f_bsize);
     171                printf(" %14s", tmp);
    164172
    165173                /* Number of used blocks */
    166                 rc = size_to_human_readable(used_blocks, st->f_bsize, &str);
    167                 if (rc != EOK)
    168                         goto error;
    169                 printf(" %14s", str);
    170                 free(str);
     174                size_to_human_readable(tmp, used_blocks  *  st->f_bsize);
     175                printf(" %14s", tmp);
    171176
    172177                /* Number of available blocks */
    173                 rc = size_to_human_readable(st->f_bfree, st->f_bsize, &str);
    174                 if (rc != EOK)
    175                         goto error;
    176                 printf(" %14s", str);
    177                 free(str);
     178                size_to_human_readable(tmp, st->f_bfree *  st->f_bsize);
     179                printf(" %14s", tmp);
    178180
    179181                /* Percentage of used blocks */
     
    183185                printf(" %s\n", mountpoint);
    184186        } else {
    185                 /* Block size / Blocks / Used blocks / Available blocks / Used% / Mounted on */
    186                 printf(" %10" PRIu32 " %9" PRIu64 " %11" PRIu64 " %11" PRIu64 " %4u%% %s\n",
    187                     st->f_bsize, st->f_blocks, used_blocks, st->f_bfree,
    188                     perc_used, mountpoint);
    189         }
    190 
    191         return EOK;
    192 error:
    193         printf("\nError: Out of memory.\n");
    194         return ENOMEM;
     187                /* Blocks / Used blocks / Available blocks / Used% / Mounted on */
     188                printf(" %15llu %14llu %14llu %4u%% %s\n",
     189                    FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),
     190                    FSBK_TO_BK(used_blocks, st->f_bsize, unit_size),
     191                    FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),
     192                    perc_used,
     193                    mountpoint);
     194        }
     195
    195196}
    196197
    197198static void print_usage(void)
    198199{
    199         printf("Syntax: %s [<options>] \n", NAME);
    200         printf("Options:\n");
    201         printf("  -h Print help\n");
    202         printf("  -b Print exact block sizes and numbers\n");
     200        printf("syntax: %s [-u] [-h] [-b <size>] \n", NAME);
     201        printf("  u : Show usage.\n");
     202        printf("  h : \"Human-readable\" output.\n");
     203        printf("  b : Scale block sizes by selected size.\n");
     204        printf("\n");
    203205}
    204206
Note: See TracChangeset for help on using the changeset viewer.