Changes in uspace/app/df/df.c [120d5bc:582a0b8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/df/df.c
r120d5bc r582a0b8 28 28 29 29 /** @addtogroup df 30 * @brief Print amounts of free and used disk space.30 * @brief Df utility. 31 31 * @{ 32 32 */ … … 35 35 */ 36 36 37 #include <cap.h>38 #include <stdbool.h>39 37 #include <stdio.h> 40 38 #include <stdlib.h> … … 48 46 #define NAME "df" 49 47 50 #define HEADER_TABLE "Filesystem SizeUsed Available Used%% Mounted on"51 #define HEADER_TABLE_ BLK "Filesystem Blk. Size Total UsedAvailable 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" 52 50 53 51 #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 57 static unsigned int unit_size; 58 static unsigned int human_readable; 59 60 static void size_to_human_readable(char *buf, uint64_t bytes); 58 61 static void print_header(void); 59 static intprint_statfs(struct statfs *, char *, char *);62 static void print_statfs(struct statfs *, char *, char *); 60 63 static void print_usage(void); 61 64 … … 64 67 int optres, errflg = 0; 65 68 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) { 72 77 switch(optres) { 73 case ' h':78 case 'u': 74 79 print_usage(); 75 80 return 0; 76 81 82 case 'h': 83 human_readable = 1; 84 break; 85 77 86 case 'b': 78 display_blocks = true;87 str_uint32_t(optarg, NULL, 0, 0, &unit_size); 79 88 break; 80 89 … … 114 123 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) { 115 124 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); 119 126 } else { 120 127 fprintf(stderr, "Cannot get information for '%s' (%d).\n", … … 127 134 } 128 135 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); 136 static 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]); 136 147 } 137 148 138 149 static void print_header(void) 139 150 { 140 if ( !display_blocks)141 printf(HEADER_TABLE );151 if (human_readable) 152 printf(HEADER_TABLE_HR); 142 153 else 143 printf(HEADER_TABLE _BLK);154 printf(HEADER_TABLE, unit_size); 144 155 145 156 putchar('\n'); 146 157 } 147 158 148 static intprint_statfs(struct statfs *st, char *name, char *mountpoint)159 static void print_statfs(struct statfs *st, char *name, char *mountpoint) 149 160 { 150 161 uint64_t const used_blocks = st->f_blocks - st->f_bfree; 151 162 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks); 152 char *str;153 int rc;154 163 155 164 printf("%10s", name); 156 165 157 if (!display_blocks) { 166 if (human_readable) { 167 char tmp[1024]; 168 158 169 /* 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); 164 172 165 173 /* 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); 171 176 172 177 /* 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); 178 180 179 181 /* Percentage of used blocks */ … … 183 185 printf(" %s\n", mountpoint); 184 186 } 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 195 196 } 196 197 197 198 static void print_usage(void) 198 199 { 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"); 203 205 } 204 206
Note:
See TracChangeset
for help on using the changeset viewer.