diff options
author | Jim Meyering <jim@meyering.net> | 1998-06-29 15:34:54 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1998-06-29 15:34:54 +0000 |
commit | 09d344df5b37c78536d5016e066bb6715d469688 (patch) | |
tree | 3d5167def87699ea649f50995517ae84f8c8cd88 /src | |
parent | 43f4099c952d78cf79b4327b939b84916169957f (diff) | |
download | coreutils-09d344df5b37c78536d5016e066bb6715d469688.tar.xz |
(human_readable_base, output_units): Remove;
replace with new variable output_block_size. All uses changed.
(long_options, usage, main): Add --block-size.
(main, decode_switches): Use new human_block_size function to
initialize output block size consistently with other programs.
From Paul Eggert.
Diffstat (limited to 'src')
-rw-r--r-- | src/du.c | 65 | ||||
-rw-r--r-- | src/ls.c | 58 |
2 files changed, 49 insertions, 74 deletions
@@ -26,9 +26,9 @@ arguments have been processed. This can be used to find out the disk usage of a directory, with some files excluded. -h Print sizes in human readable format (1k 234M 2G, etc). - -k Print sizes in kilobytes instead of 512 byte blocks - (the default required by POSIX). - -m Print sizes in megabytes instead of 512 byte blocks + -H Similar, but use powers of 1000 not 1024. + -k Print sizes in kilobytes. + -m Print sizes in megabytes. -b Print sizes in bytes. -S Count the size of each directory separately, not including the sizes of subdirectories. @@ -143,11 +143,9 @@ static int opt_dereference_arguments = 0; is at level 0, so `du --max-depth=0' is equivalent to `du -s'. */ static int max_depth = INT_MAX; -/* base used for human style output */ -static int human_readable_base; - -/* The units to count in. */ -static int output_units; +/* If positive, the units to use when printing sizes; + if negative, the human-readable base. */ +static int output_block_size; /* Accumulated path for file or directory being processed. */ static String *path; @@ -180,6 +178,7 @@ static uintmax_t tot_size = 0; static struct option const long_options[] = { {"all", no_argument, &opt_all, 1}, + {"block-size", required_argument, 0, 129}, {"bytes", no_argument, NULL, 'b'}, {"count-links", no_argument, &opt_count_all, 1}, {"dereference", no_argument, NULL, 'L'}, @@ -217,15 +216,16 @@ usage (int status, char *reason) Summarize disk usage of each FILE, recursively for directories.\n\ \n\ -a, --all write counts for all files, not just directories\n\ + --block-size=SIZE use SIZE-byte blocks\n\ -b, --bytes print size in bytes\n\ -c, --total produce a grand total\n\ -D, --dereference-args dereference PATHs when symbolic link\n\ -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\ -H, --si likewise, but use powers of 1000 not 1024\n\ - -k, --kilobytes use 1024-byte blocks\n\ + -k, --kilobytes like --block-size=1024\n\ -l, --count-links count sizes many times if hard linked\n\ -L, --dereference dereference all symbolic links\n\ - -m, --megabytes use 1048576-byte blocks\n\ + -m, --megabytes like --block-size=1048576\n\ -S, --separate-dirs do not include size of subdirectories\n\ -s, --summarize display only a total for each argument\n\ -x, --one-file-system skip directories on different filesystems\n\ @@ -249,7 +249,6 @@ main (int argc, char **argv) { int c; char *cwd_only[2]; - char *bs; int max_depth_specified = 0; /* If nonzero, display only a total for each argument. */ @@ -266,21 +265,7 @@ main (int argc, char **argv) exclude = new_exclude (); xstat = lstat; - if (getenv ("POSIXLY_CORRECT")) - output_units = 512; - else if ((bs = getenv ("BLOCKSIZE")) - && strncmp (bs, "HUMAN", sizeof ("HUMAN") - 1) == 0) - { - human_readable_base = 1024; - output_units = 1; - } - else if (bs && STREQ (bs, "SI")) - { - human_readable_base = 1000; - output_units = 1; - } - else - output_units = 1024; + human_block_size (getenv ("DU_BLOCK_SIZE"), 0, &output_block_size); while ((c = getopt_long (argc, argv, "abchHklmsxDLSX:", long_options, NULL)) != -1) @@ -296,8 +281,7 @@ main (int argc, char **argv) break; case 'b': - human_readable_base = 0; - output_units = 1; + output_block_size = 1; break; case 'c': @@ -305,18 +289,15 @@ main (int argc, char **argv) break; case 'h': - human_readable_base = 1024; - output_units = 1; + output_block_size = -1024; break; case 'H': - human_readable_base = 1000; - output_units = 1; + output_block_size = -1000; break; case 'k': - human_readable_base = 0; - output_units = 1024; + output_block_size = 1024; break; case 13: /* --max-depth=N */ @@ -329,8 +310,7 @@ main (int argc, char **argv) break; case 'm': - human_readable_base = 0; - output_units = 1024 * 1024; + output_block_size = 1024 * 1024; break; case 'l': @@ -366,6 +346,10 @@ main (int argc, char **argv) add_exclude (exclude, optarg); break; + case 129: + human_block_size (optarg, 1, &output_block_size); + break; + default: usage (1, (char *) 0); } @@ -413,8 +397,8 @@ main (int argc, char **argv) } /* Print N_BLOCKS followed by STRING on a line. NBLOCKS is the number of - ST_NBLOCKSIZE-byte blocks; convert it to OUTPUT_UNITS units before - printing. If HUMAN_READABLE_BASE is nonzero, use a human readable + ST_NBLOCKSIZE-byte blocks; convert it to OUTPUT_BLOCK_SIZE units before + printing. If OUTPUT_BLOCK_SIZE is negative, use a human readable notation instead. */ static void @@ -422,8 +406,7 @@ print_size (uintmax_t n_blocks, const char *string) { char buf[LONGEST_HUMAN_READABLE + 1]; printf ("%s\t%s\n", - human_readable (n_blocks, buf, ST_NBLOCKSIZE, output_units, - human_readable_base), + human_readable (n_blocks, buf, ST_NBLOCKSIZE, output_block_size), string); FFLUSH (stdout); } @@ -490,7 +473,7 @@ du_files (char **files) free_cwd (&cwd); } -/* Print (if appropriate) the size (in units determined by `output_units') +/* Print (if appropriate) the size (in units determined by `output_block_size') of file or directory ENT. Return the size of ENT in units of 512-byte blocks. TOP is one for external calls, zero for recursive calls. LAST_DEV is the device that the parent directory of ENT is on. @@ -317,13 +317,13 @@ int inhibit_group; static int numeric_ids; -/* Nonzero means mention the size in 512 byte blocks of each file. -s */ +/* Nonzero means mention the size in blocks of each file. -s */ static int print_block_size; -/* The units to count blocks in. */ - -static int output_units; +/* If positive, the units to use when printing sizes; + if negative, the human-readable base. */ +static int output_block_size; /* Precede each line of long output (per file) with a string like `m,n:' where M is the number of characters after the `:' and before the @@ -409,10 +409,6 @@ struct col_ext_type *col_ext_list = NULL; /* Buffer for color sequences */ static char *color_buf; -/* base used for human style output */ - -static int human_readable_base; - /* Nonzero means mention the inode number of each file. -i */ static int print_inode; @@ -532,6 +528,7 @@ static struct option const long_options[] = {"help", no_argument, &show_help, 1}, {"version", no_argument, &show_version, 1}, {"color", optional_argument, 0, 13}, + {"block-size", required_argument, 0, 17}, {NULL, 0, NULL, 0} }; @@ -843,7 +840,6 @@ decode_switches (int argc, char **argv) sort_reverse = 0; numeric_ids = 0; print_block_size = 0; - output_units = getenv ("POSIXLY_CORRECT") ? 512 : 1024; indicator_style = none; print_inode = 0; trace_links = 0; @@ -857,11 +853,7 @@ decode_switches (int argc, char **argv) && 0 <= (i = argmatch (p, quoting_style_args))) set_quoting_style (NULL, (enum quoting_style) i); - if ((p = getenv ("BLOCKSIZE")) - && strncmp (p, "HUMAN", sizeof ("HUMAN") - 1) == 0) - human_readable_base = 1024; - else if (p && STREQ (p, "SI")) - human_readable_base = 1000; + human_block_size (getenv ("LS_BLOCK_SIZE"), 0, &output_block_size); line_length = 80; if ((p = getenv ("COLUMNS")) && *p) @@ -950,11 +942,11 @@ decode_switches (int argc, char **argv) break; case 'h': - human_readable_base = 1024; + output_block_size = -1024; break; case 'H': - human_readable_base = 1000; + output_block_size = -1000; break; case 'i': @@ -962,7 +954,7 @@ decode_switches (int argc, char **argv) break; case 'k': - output_units = 1024; + output_block_size = 1024; break; case 'l': @@ -1178,14 +1170,15 @@ decode_switches (int argc, char **argv) qmark_funny_chars = 0; break; + case 17: + human_block_size (optarg, 1, &output_block_size); + break; + default: usage (EXIT_FAILURE); } } - if (human_readable_base) - output_units = 1; - filename_quoting_options = clone_quoting_options (NULL); if (indicator_style != none) for (p = "*=@|" + (int) indicator_style - 1; *p; p++) @@ -1610,8 +1603,7 @@ print_dir (const char *name, const char *realname) p = _("total"); DIRED_FPUTS (p, stdout, strlen (p)); DIRED_PUTCHAR (' '); - p = human_readable (total_blocks, buf, ST_NBLOCKSIZE, output_units, - human_readable_base); + p = human_readable (total_blocks, buf, ST_NBLOCKSIZE, output_block_size); DIRED_FPUTS (p, stdout, strlen (p)); DIRED_PUTCHAR ('\n'); } @@ -1802,7 +1794,7 @@ gobble_file (const char *name, int explicit_arg, const char *dirname) { char buf[LONGEST_HUMAN_READABLE + 1]; int len = strlen (human_readable (blocks, buf, ST_NBLOCKSIZE, - output_units, human_readable_base)); + output_block_size)); if (block_size_size < len) block_size_size = len < 7 ? len : 7; } @@ -2240,7 +2232,7 @@ print_long_format (const struct fileinfo *f) { char hbuf[LONGEST_HUMAN_READABLE + 1]; sprintf (p, "%*s ", INODE_DIGITS, - human_readable ((uintmax_t) f->stat.st_ino, hbuf, 1, 1, 0)); + human_readable ((uintmax_t) f->stat.st_ino, hbuf, 1, 1)); p += strlen (p); } @@ -2249,8 +2241,7 @@ print_long_format (const struct fileinfo *f) char hbuf[LONGEST_HUMAN_READABLE + 1]; sprintf (p, "%*s ", block_size_size, human_readable ((uintmax_t) ST_NBLOCKS (f->stat), hbuf, - ST_NBLOCKSIZE, output_units, - human_readable_base)); + ST_NBLOCKSIZE, output_block_size)); p += strlen (p); } @@ -2283,8 +2274,8 @@ print_long_format (const struct fileinfo *f) { char hbuf[LONGEST_HUMAN_READABLE + 1]; sprintf (p, "%8s ", - human_readable ((uintmax_t) f->stat.st_size, - hbuf, 1, 1, human_readable_base)); + human_readable ((uintmax_t) f->stat.st_size, hbuf, 1, + output_block_size < 0 ? output_block_size : 1)); } p += strlen (p); @@ -2317,13 +2308,13 @@ print_long_format (const struct fileinfo *f) if (when < 0) { - const char *num = human_readable (- (uintmax_t) when, hbuf, 1, 1, 0); + const char *num = human_readable (- (uintmax_t) when, hbuf, 1, 1); int sign_width = width - strlen (num); sprintf (p, "%*s%s ", sign_width < 0 ? 0 : sign_width, "-", num); } else sprintf (p, "%*s ", width, - human_readable ((uintmax_t) when, hbuf, 1, 1, 0)); + human_readable ((uintmax_t) when, hbuf, 1, 1)); p += strlen (p); } @@ -2422,12 +2413,12 @@ print_file_name_and_frills (const struct fileinfo *f) if (print_inode) printf ("%*s ", INODE_DIGITS, - human_readable ((uintmax_t) f->stat.st_ino, buf, 1, 1, 0)); + human_readable ((uintmax_t) f->stat.st_ino, buf, 1, 1)); if (print_block_size) printf ("%*s ", block_size_size, human_readable ((uintmax_t) ST_NBLOCKS (f->stat), buf, - ST_NBLOCKSIZE, output_units, human_readable_base)); + ST_NBLOCKSIZE, output_block_size)); print_name_with_quoting (f->name, f->stat.st_mode, f->linkok, NULL); @@ -2868,6 +2859,7 @@ Sort entries alphabetically if none of -cftuSUX nor --sort.\n\ -a, --all do not hide entries starting with .\n\ -A, --almost-all do not list implied . and ..\n\ -b, --escape print octal escapes for nongraphic characters\n\ + --block-size=SIZE use SIZE-byte blocks\n\ -B, --ignore-backups do not list implied entries ending with ~\n\ -c sort by change time; with -l: show ctime\n\ -C list entries by columns\n\ @@ -2890,7 +2882,7 @@ Sort entries alphabetically if none of -cftuSUX nor --sort.\n\ none (default), classify (-F), file-type (-p)\n\ -i, --inode print index number of each file\n\ -I, --ignore=PATTERN do not list implied entries matching shell PATTERN\n\ - -k, --kilobytes use 1024 byte blocks\n\ + -k, --kilobytes like --block-size=1024\n\ -l use a long listing format\n\ -L, --dereference list entries pointed to by symbolic links\n\ -m fill width with a comma separated list of entries\n\ |