Changes in uspace/app/df/df.c [332513a:651c8db] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/df/df.c
r332513a r651c8db 50 50 #define HEADER_TABLE_HR "Filesystem Size Used Available Used%% Mounted on" 51 51 52 #define PERCENTAGE(x, tot) ( (unsigned long long) (100L * (x) / (tot)))52 #define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0) 53 53 #define FSBK_TO_BK(x, fsbk, bk) \ 54 54 (((fsbk) != 0 && (fsbk) < (bk)) ? \ … … 59 59 static unsigned int human_readable; 60 60 61 static int size_to_human_readable(char buf[], uint64_t bytes);61 static void size_to_human_readable(char *buf, uint64_t bytes); 62 62 static void print_header(void); 63 63 static void print_statfs(struct statfs *, char *, char *); … … 90 90 91 91 case ':': 92 fprintf(stderr, "Option -%c requires an operand\n", optopt); 92 fprintf(stderr, "Option -%c requires an operand\n", 93 optopt); 93 94 errflg++; 94 95 break; … … 100 101 101 102 default: 102 fprintf(stderr, "Unknown error while parsing command line options."); 103 fprintf(stderr, 104 "Unknown error while parsing command line options"); 103 105 errflg++; 104 106 break; … … 107 109 108 110 if (optind > argc) { 109 fprintf(stderr, "Too many input parameter \n");111 fprintf(stderr, "Too many input parameters\n"); 110 112 errflg++; 111 113 } … … 118 120 LIST_INITIALIZE(mtab_list); 119 121 get_mtab_list(&mtab_list); 122 120 123 print_header(); 121 124 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) { … … 123 126 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp); 124 127 } 128 125 129 putchar('\n'); 126 130 return 0; 127 131 } 128 132 129 static int size_to_human_readable(char buf[], uint64_t bytes)133 static void size_to_human_readable(char *buf, uint64_t bytes) 130 134 { 131 135 const char *units = "BkMGTPEZY"; 132 136 int i = 0; 133 int limit; 134 135 limit = str_length(units); 137 136 138 while (bytes >= 1024) { 137 if (i >= limit)138 return -1;139 139 bytes /= 1024; 140 140 i++; 141 141 } 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]); 145 144 } 146 145 … … 151 150 else 152 151 printf(HEADER_TABLE, unit_size); 152 153 153 putchar('\n'); 154 154 } … … 156 156 static void print_statfs(struct statfs *st, char *name, char *mountpoint) 157 157 { 158 uint64_t const used_blocks = st->f_blocks - st->f_bfree; 159 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks); 160 158 161 printf("%10s", name); 159 162 160 163 if (human_readable) { 161 164 char tmp[1024]; 165 166 /* Print size */ 162 167 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 */ 166 175 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(" %4u%%", perc_used); 180 181 /* Mount point */ 182 printf(" %s\n", mountpoint); 183 } else { 184 /* Blocks / Used blocks / Available blocks / Used% / Mounted on */ 185 printf(" %15llu %14llu %14llu %4u%% %s\n", 186 FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size), 187 FSBK_TO_BK(used_blocks, st->f_bsize, unit_size), 188 FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size), 189 perc_used, 190 mountpoint); 191 } 181 192 182 193 }
Note:
See TracChangeset
for help on using the changeset viewer.