Changeset 120d5bc in mainline
- Timestamp:
- 2017-09-13T20:49:43Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5918c00
- Parents:
- 6c4eedf
- Location:
- uspace
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/df/df.c
r6c4eedf r120d5bc 28 28 29 29 /** @addtogroup df 30 * @brief Df utility.30 * @brief Print amounts of free and used disk space. 31 31 * @{ 32 32 */ … … 35 35 */ 36 36 37 #include <cap.h> 38 #include <stdbool.h> 37 39 #include <stdio.h> 38 40 #include <stdlib.h> … … 46 48 #define NAME "df" 47 49 48 #define HEADER_TABLE "Filesystem %4u-blocksUsed Available Used%% Mounted on"49 #define HEADER_TABLE_ HR "Filesystem Size UsedAvailable Used%% Mounted on"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" 50 52 51 53 #define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0) 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); 54 55 static bool display_blocks; 56 57 static int size_to_human_readable(uint64_t, size_t, char **); 61 58 static void print_header(void); 62 static voidprint_statfs(struct statfs *, char *, char *);59 static int print_statfs(struct statfs *, char *, char *); 63 60 static void print_usage(void); 64 61 … … 67 64 int optres, errflg = 0; 68 65 struct statfs st; 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) { 66 int rc; 67 68 display_blocks = false; 69 70 /* Parse command-line options */ 71 while ((optres = getopt(argc, argv, ":ubh")) != -1) { 77 72 switch(optres) { 78 case ' u':73 case 'h': 79 74 print_usage(); 80 75 return 0; 81 76 82 case 'h':83 human_readable = 1;84 break;85 86 77 case 'b': 87 str_uint32_t(optarg, NULL, 0, 0, &unit_size);78 display_blocks = true; 88 79 break; 89 80 … … 123 114 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) { 124 115 if (vfs_statfs_path(mtab_ent->mp, &st) == 0) { 125 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp); 116 rc = print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp); 117 if (rc != EOK) 118 return 1; 126 119 } else { 127 120 fprintf(stderr, "Cannot get information for '%s' (%d).\n", … … 134 127 } 135 128 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]); 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); 147 136 } 148 137 149 138 static void print_header(void) 150 139 { 151 if ( human_readable)152 printf(HEADER_TABLE _HR);140 if (!display_blocks) 141 printf(HEADER_TABLE); 153 142 else 154 printf(HEADER_TABLE , unit_size);143 printf(HEADER_TABLE_BLK); 155 144 156 145 putchar('\n'); 157 146 } 158 147 159 static voidprint_statfs(struct statfs *st, char *name, char *mountpoint)148 static int print_statfs(struct statfs *st, char *name, char *mountpoint) 160 149 { 161 150 uint64_t const used_blocks = st->f_blocks - st->f_bfree; 162 151 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks); 152 char *str; 153 int rc; 163 154 164 155 printf("%10s", name); 165 156 166 if (human_readable) { 167 char tmp[1024]; 168 157 if (!display_blocks) { 169 158 /* Print size */ 170 size_to_human_readable(tmp, st->f_blocks * st->f_bsize); 171 printf(" %14s", tmp); 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); 172 164 173 165 /* Number of used blocks */ 174 size_to_human_readable(tmp, used_blocks * st->f_bsize); 175 printf(" %14s", tmp); 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); 176 171 177 172 /* Number of available blocks */ 178 size_to_human_readable(tmp, st->f_bfree * st->f_bsize); 179 printf(" %14s", tmp); 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); 180 178 181 179 /* Percentage of used blocks */ … … 185 183 printf(" %s\n", mountpoint); 186 184 } else { 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 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; 196 195 } 197 196 198 197 static void print_usage(void) 199 198 { 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"); 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"); 205 203 } 206 204 -
uspace/lib/c/generic/cap.c
r6c4eedf r120d5bc 81 81 * and @c cv_max gives the maximum value. 82 82 */ 83 int cap_ spec_to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size,83 int cap_to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size, 84 84 uint64_t *rblocks) 85 85 { -
uspace/lib/c/include/cap.h
r6c4eedf r120d5bc 94 94 extern void cap_simplify(cap_spec_t *); 95 95 extern void cap_from_blocks(uint64_t, size_t, cap_spec_t *); 96 extern int cap_ spec_to_blocks(cap_spec_t *, cap_vsel_t, size_t, uint64_t *);96 extern int cap_to_blocks(cap_spec_t *, cap_vsel_t, size_t, uint64_t *); 97 97 98 98 #endif -
uspace/lib/fdisk/src/fdisk.c
r6c4eedf r120d5bc 996 996 int rc; 997 997 998 rc = cap_ spec_to_blocks(&pspec->capacity, cv_nom, dev->dinfo.block_size,998 rc = cap_to_blocks(&pspec->capacity, cv_nom, dev->dinfo.block_size, 999 999 &nom_blocks); 1000 1000 if (rc != EOK) 1001 1001 return rc; 1002 1002 1003 rc = cap_ spec_to_blocks(&pspec->capacity, cv_min, dev->dinfo.block_size,1003 rc = cap_to_blocks(&pspec->capacity, cv_min, dev->dinfo.block_size, 1004 1004 &min_blocks); 1005 1005 if (rc != EOK) 1006 1006 return rc; 1007 1007 1008 rc = cap_ spec_to_blocks(&pspec->capacity, cv_max, dev->dinfo.block_size,1008 rc = cap_to_blocks(&pspec->capacity, cv_max, dev->dinfo.block_size, 1009 1009 &max_blocks); 1010 1010 if (rc != EOK)
Note:
See TracChangeset
for help on using the changeset viewer.