* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / du.c
blob5f4e796dbdcbf70dd08552b17d3715d86697615b
1 /* du -- summarize disk usage
2 Copyright (C) 1988-1991, 1995-2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Differences from the Unix du:
19 * Doesn't simply ignore the names of regular files given as arguments
20 when -a is given.
22 By tege@sics.se, Torbjorn Granlund,
23 and djm@ai.mit.edu, David MacKenzie.
24 Variable blocks added by lm@sgi.com and eggert@twinsun.com.
25 Rewritten to use nftw, then to use fts by Jim Meyering. */
27 #include <config.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31 #include <assert.h>
32 #include "system.h"
33 #include "argmatch.h"
34 #include "error.h"
35 #include "exclude.h"
36 #include "fprintftime.h"
37 #include "hash.h"
38 #include "human.h"
39 #include "inttostr.h"
40 #include "quote.h"
41 #include "quotearg.h"
42 #include "readtokens0.h"
43 #include "same.h"
44 #include "stat-time.h"
45 #include "xfts.h"
46 #include "xstrtol.h"
48 extern bool fts_debug;
50 /* The official name of this program (e.g., no `g' prefix). */
51 #define PROGRAM_NAME "du"
53 #define AUTHORS \
54 "Torbjorn Granlund", "David MacKenzie, Paul Eggert", "Jim Meyering"
56 #if DU_DEBUG
57 # define FTS_CROSS_CHECK(Fts) fts_cross_check (Fts)
58 # define DEBUG_OPT "d"
59 #else
60 # define FTS_CROSS_CHECK(Fts)
61 # define DEBUG_OPT
62 #endif
64 /* Initial size of the hash table. */
65 #define INITIAL_TABLE_SIZE 103
67 /* Hash structure for inode and device numbers. The separate entry
68 structure makes it easier to rehash "in place". */
70 struct entry
72 ino_t st_ino;
73 dev_t st_dev;
76 /* A set of dev/ino pairs. */
77 static Hash_table *htab;
79 /* Define a class for collecting directory information. */
81 struct duinfo
83 /* Size of files in directory. */
84 uintmax_t size;
86 /* Latest time stamp found. If tmax.tv_sec == TYPE_MINIMUM (time_t)
87 && tmax.tv_nsec < 0, no time stamp has been found. */
88 struct timespec tmax;
91 /* Initialize directory data. */
92 static inline void
93 duinfo_init (struct duinfo *a)
95 a->size = 0;
96 a->tmax.tv_sec = TYPE_MINIMUM (time_t);
97 a->tmax.tv_nsec = -1;
100 /* Set directory data. */
101 static inline void
102 duinfo_set (struct duinfo *a, uintmax_t size, struct timespec tmax)
104 a->size = size;
105 a->tmax = tmax;
108 /* Accumulate directory data. */
109 static inline void
110 duinfo_add (struct duinfo *a, struct duinfo const *b)
112 a->size += b->size;
113 if (timespec_cmp (a->tmax, b->tmax) < 0)
114 a->tmax = b->tmax;
117 /* A structure for per-directory level information. */
118 struct dulevel
120 /* Entries in this directory. */
121 struct duinfo ent;
123 /* Total for subdirectories. */
124 struct duinfo subdir;
127 /* Name under which this program was invoked. */
128 char *program_name;
130 /* If true, display counts for all files, not just directories. */
131 static bool opt_all = false;
133 /* If true, rather than using the disk usage of each file,
134 use the apparent size (a la stat.st_size). */
135 static bool apparent_size = false;
137 /* If true, count each hard link of files with multiple links. */
138 static bool opt_count_all = false;
140 /* If true, output the NUL byte instead of a newline at the end of each line. */
141 static bool opt_nul_terminate_output = false;
143 /* If true, print a grand total at the end. */
144 static bool print_grand_total = false;
146 /* If nonzero, do not add sizes of subdirectories. */
147 static bool opt_separate_dirs = false;
149 /* Show the total for each directory (and file if --all) that is at
150 most MAX_DEPTH levels down from the root of the hierarchy. The root
151 is at level 0, so `du --max-depth=0' is equivalent to `du -s'. */
152 static size_t max_depth = SIZE_MAX;
154 /* Human-readable options for output. */
155 static int human_output_opts;
157 /* If true, print most recently modified date, using the specified format. */
158 static bool opt_time = false;
160 /* Type of time to display. controlled by --time. */
162 enum time_type
164 time_mtime, /* default */
165 time_ctime,
166 time_atime
169 static enum time_type time_type = time_mtime;
171 /* User specified date / time style */
172 static char const *time_style = NULL;
174 /* Format used to display date / time. Controlled by --time-style */
175 static char const *time_format = NULL;
177 /* The units to use when printing sizes. */
178 static uintmax_t output_block_size;
180 /* File name patterns to exclude. */
181 static struct exclude *exclude;
183 /* Grand total size of all args, in bytes. Also latest modified date. */
184 static struct duinfo tot_dui;
186 #define IS_DIR_TYPE(Type) \
187 ((Type) == FTS_DP \
188 || (Type) == FTS_DNR)
190 /* For long options that have no equivalent short option, use a
191 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
192 enum
194 APPARENT_SIZE_OPTION = CHAR_MAX + 1,
195 EXCLUDE_OPTION,
196 FILES0_FROM_OPTION,
197 HUMAN_SI_OPTION,
199 /* FIXME: --kilobytes is deprecated (but not -k); remove in late 2006 */
200 KILOBYTES_LONG_OPTION,
202 MAX_DEPTH_OPTION,
204 /* FIXME: --megabytes is deprecated (but not -m); remove in late 2006 */
205 MEGABYTES_LONG_OPTION,
207 TIME_OPTION,
208 TIME_STYLE_OPTION
211 static struct option const long_options[] =
213 {"all", no_argument, NULL, 'a'},
214 {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
215 {"block-size", required_argument, NULL, 'B'},
216 {"bytes", no_argument, NULL, 'b'},
217 {"count-links", no_argument, NULL, 'l'},
218 {"dereference", no_argument, NULL, 'L'},
219 {"dereference-args", no_argument, NULL, 'D'},
220 {"exclude", required_argument, NULL, EXCLUDE_OPTION},
221 {"exclude-from", required_argument, NULL, 'X'},
222 {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
223 {"human-readable", no_argument, NULL, 'h'},
224 {"si", no_argument, NULL, HUMAN_SI_OPTION},
225 {"kilobytes", no_argument, NULL, KILOBYTES_LONG_OPTION},
226 {"max-depth", required_argument, NULL, MAX_DEPTH_OPTION},
227 {"null", no_argument, NULL, '0'},
228 {"megabytes", no_argument, NULL, MEGABYTES_LONG_OPTION},
229 {"no-dereference", no_argument, NULL, 'P'},
230 {"one-file-system", no_argument, NULL, 'x'},
231 {"separate-dirs", no_argument, NULL, 'S'},
232 {"summarize", no_argument, NULL, 's'},
233 {"total", no_argument, NULL, 'c'},
234 {"time", optional_argument, NULL, TIME_OPTION},
235 {"time-style", required_argument, NULL, TIME_STYLE_OPTION},
236 {GETOPT_HELP_OPTION_DECL},
237 {GETOPT_VERSION_OPTION_DECL},
238 {NULL, 0, NULL, 0}
241 static char const *const time_args[] =
243 "atime", "access", "use", "ctime", "status", NULL
245 static enum time_type const time_types[] =
247 time_atime, time_atime, time_atime, time_ctime, time_ctime
249 ARGMATCH_VERIFY (time_args, time_types);
251 /* `full-iso' uses full ISO-style dates and times. `long-iso' uses longer
252 ISO-style time stamps, though shorter than `full-iso'. `iso' uses shorter
253 ISO-style time stamps. */
254 enum time_style
256 full_iso_time_style, /* --time-style=full-iso */
257 long_iso_time_style, /* --time-style=long-iso */
258 iso_time_style /* --time-style=iso */
261 static char const *const time_style_args[] =
263 "full-iso", "long-iso", "iso", NULL
265 static enum time_style const time_style_types[] =
267 full_iso_time_style, long_iso_time_style, iso_time_style
269 ARGMATCH_VERIFY (time_style_args, time_style_types);
271 void
272 usage (int status)
274 if (status != EXIT_SUCCESS)
275 fprintf (stderr, _("Try `%s --help' for more information.\n"),
276 program_name);
277 else
279 printf (_("\
280 Usage: %s [OPTION]... [FILE]...\n\
281 or: %s [OPTION]... --files0-from=F\n\
282 "), program_name, program_name);
283 fputs (_("\
284 Summarize disk usage of each FILE, recursively for directories.\n\
286 "), stdout);
287 fputs (_("\
288 Mandatory arguments to long options are mandatory for short options too.\n\
289 "), stdout);
290 fputs (_("\
291 -a, --all write counts for all files, not just directories\n\
292 --apparent-size print apparent sizes, rather than disk usage; although\n\
293 the apparent size is usually smaller, it may be\n\
294 larger due to holes in (`sparse') files, internal\n\
295 fragmentation, indirect blocks, and the like\n\
296 -B, --block-size=SIZE use SIZE-byte blocks\n\
297 -b, --bytes equivalent to `--apparent-size --block-size=1'\n\
298 -c, --total produce a grand total\n\
299 -D, --dereference-args dereference FILEs that are symbolic links\n\
300 "), stdout);
301 fputs (_("\
302 --files0-from=F summarize disk usage of the NUL-terminated file\n\
303 names specified in file F\n\
304 -H like --si, but also evokes a warning; will soon\n\
305 change to be equivalent to --dereference-args (-D)\n\
306 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\
307 --si like -h, but use powers of 1000 not 1024\n\
308 -k like --block-size=1K\n\
309 -l, --count-links count sizes many times if hard linked\n\
310 -m like --block-size=1M\n\
311 "), stdout);
312 fputs (_("\
313 -L, --dereference dereference all symbolic links\n\
314 -P, --no-dereference don't follow any symbolic links (this is the default)\n\
315 -0, --null end each output line with 0 byte rather than newline\n\
316 -S, --separate-dirs do not include size of subdirectories\n\
317 -s, --summarize display only a total for each argument\n\
318 "), stdout);
319 fputs (_("\
320 -x, --one-file-system skip directories on different file systems\n\
321 -X FILE, --exclude-from=FILE Exclude files that match any pattern in FILE.\n\
322 --exclude=PATTERN Exclude files that match PATTERN.\n\
323 --max-depth=N print the total for a directory (or file, with --all)\n\
324 only if it is N or fewer levels below the command\n\
325 line argument; --max-depth=0 is the same as\n\
326 --summarize\n\
327 "), stdout);
328 fputs (_("\
329 --time show time of the last modification of any file in the\n\
330 directory, or any of its subdirectories\n\
331 --time=WORD show time as WORD instead of modification time:\n\
332 atime, access, use, ctime or status\n\
333 --time-style=STYLE show times using style STYLE:\n\
334 full-iso, long-iso, iso, +FORMAT\n\
335 FORMAT is interpreted like `date'\n\
336 "), stdout);
337 fputs (HELP_OPTION_DESCRIPTION, stdout);
338 fputs (VERSION_OPTION_DESCRIPTION, stdout);
339 fputs (_("\n\
340 SIZE may be (or may be an integer optionally followed by) one of following:\n\
341 kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
342 "), stdout);
343 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
345 exit (status);
348 static size_t
349 entry_hash (void const *x, size_t table_size)
351 struct entry const *p = x;
353 /* Ignoring the device number here should be fine. */
354 /* The cast to uintmax_t prevents negative remainders
355 if st_ino is negative. */
356 return (uintmax_t) p->st_ino % table_size;
359 /* Compare two dev/ino pairs. Return true if they are the same. */
360 static bool
361 entry_compare (void const *x, void const *y)
363 struct entry const *a = x;
364 struct entry const *b = y;
365 return SAME_INODE (*a, *b) ? true : false;
368 /* Try to insert the INO/DEV pair into the global table, HTAB.
369 Return true if the pair is successfully inserted,
370 false if the pair is already in the table. */
371 static bool
372 hash_ins (ino_t ino, dev_t dev)
374 struct entry *ent;
375 struct entry *ent_from_table;
377 ent = xmalloc (sizeof *ent);
378 ent->st_ino = ino;
379 ent->st_dev = dev;
381 ent_from_table = hash_insert (htab, ent);
382 if (ent_from_table == NULL)
384 /* Insertion failed due to lack of memory. */
385 xalloc_die ();
388 if (ent_from_table == ent)
390 /* Insertion succeeded. */
391 return true;
394 /* That pair is already in the table, so ENT was not inserted. Free it. */
395 free (ent);
397 return false;
400 /* Initialize the hash table. */
401 static void
402 hash_init (void)
404 htab = hash_initialize (INITIAL_TABLE_SIZE, NULL,
405 entry_hash, entry_compare, free);
406 if (htab == NULL)
407 xalloc_die ();
410 /* FIXME: this code is nearly identical to code in date.c */
411 /* Display the date and time in WHEN according to the format specified
412 in FORMAT. */
414 static void
415 show_date (const char *format, struct timespec when)
417 struct tm *tm = localtime (&when.tv_sec);
418 if (! tm)
420 char buf[INT_BUFSIZE_BOUND (intmax_t)];
421 error (0, 0, _("time %s is out of range"),
422 (TYPE_SIGNED (time_t)
423 ? imaxtostr (when.tv_sec, buf)
424 : umaxtostr (when.tv_sec, buf)));
425 fputs (buf, stdout);
426 return;
429 fprintftime (stdout, format, tm, 0, when.tv_nsec);
432 /* Print N_BYTES. Convert it to a readable value before printing. */
434 static void
435 print_only_size (uintmax_t n_bytes)
437 char buf[LONGEST_HUMAN_READABLE + 1];
438 fputs (human_readable (n_bytes, buf, human_output_opts,
439 1, output_block_size), stdout);
442 /* Print size (and optionally time) indicated by *PDUI, followed by STRING. */
444 static void
445 print_size (const struct duinfo *pdui, const char *string)
447 print_only_size (pdui->size);
448 if (opt_time)
450 putchar ('\t');
451 show_date (time_format, pdui->tmax);
453 printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n');
454 fflush (stdout);
457 /* This function is called once for every file system object that fts
458 encounters. fts does a depth-first traversal. This function knows
459 that and accumulates per-directory totals based on changes in
460 the depth of the current entry. It returns true on success. */
462 static bool
463 process_file (FTS *fts, FTSENT *ent)
465 bool ok;
466 struct duinfo dui;
467 struct duinfo dui_to_print;
468 size_t level;
469 static size_t prev_level;
470 static size_t n_alloc;
471 /* First element of the structure contains:
472 The sum of the st_size values of all entries in the single directory
473 at the corresponding level. Although this does include the st_size
474 corresponding to each subdirectory, it does not include the size of
475 any file in a subdirectory. Also corresponding last modified date.
476 Second element of the structure contains:
477 The sum of the sizes of all entries in the hierarchy at or below the
478 directory at the specified level. */
479 static struct dulevel *dulvl;
480 bool print = true;
482 const char *file = ent->fts_path;
483 const struct stat *sb = ent->fts_statp;
484 bool skip;
486 /* If necessary, set FTS_SKIP before returning. */
487 skip = excluded_file_name (exclude, ent->fts_path);
488 if (skip)
489 fts_set (fts, ent, FTS_SKIP);
491 switch (ent->fts_info)
493 case FTS_NS:
494 error (0, ent->fts_errno, _("cannot access %s"), quote (file));
495 return false;
497 case FTS_ERR:
498 /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
499 error (0, ent->fts_errno, _("%s"), quote (file));
500 return false;
502 case FTS_DNR:
503 /* Don't return just yet, since although the directory is not readable,
504 we were able to stat it, so we do have a size. */
505 error (0, ent->fts_errno, _("cannot read directory %s"), quote (file));
506 ok = false;
507 break;
509 default:
510 ok = true;
511 break;
514 /* If this is the first (pre-order) encounter with a directory,
515 or if it's the second encounter for a skipped directory, then
516 return right away. */
517 if (ent->fts_info == FTS_D || skip)
518 return ok;
520 /* If the file is being excluded or if it has already been counted
521 via a hard link, then don't let it contribute to the sums. */
522 if (skip
523 || (!opt_count_all
524 && ! S_ISDIR (sb->st_mode)
525 && 1 < sb->st_nlink
526 && ! hash_ins (sb->st_ino, sb->st_dev)))
528 /* Note that we must not simply return here.
529 We still have to update prev_level and maybe propagate
530 some sums up the hierarchy. */
531 duinfo_init (&dui);
532 print = false;
534 else
536 duinfo_set (&dui,
537 (apparent_size
538 ? sb->st_size
539 : (uintmax_t) ST_NBLOCKS (*sb) * ST_NBLOCKSIZE),
540 (time_type == time_mtime ? get_stat_mtime (sb)
541 : time_type == time_atime ? get_stat_atime (sb)
542 : get_stat_ctime (sb)));
545 level = ent->fts_level;
546 dui_to_print = dui;
548 if (n_alloc == 0)
550 n_alloc = level + 10;
551 dulvl = xcalloc (n_alloc, sizeof *dulvl);
553 else
555 if (level == prev_level)
557 /* This is usually the most common case. Do nothing. */
559 else if (level > prev_level)
561 /* Descending the hierarchy.
562 Clear the accumulators for *all* levels between prev_level
563 and the current one. The depth may change dramatically,
564 e.g., from 1 to 10. */
565 size_t i;
567 if (n_alloc <= level)
569 dulvl = xnrealloc (dulvl, level, 2 * sizeof *dulvl);
570 n_alloc = level * 2;
573 for (i = prev_level + 1; i <= level; i++)
575 duinfo_init (&dulvl[i].ent);
576 duinfo_init (&dulvl[i].subdir);
579 else /* level < prev_level */
581 /* Ascending the hierarchy.
582 Process a directory only after all entries in that
583 directory have been processed. When the depth decreases,
584 propagate sums from the children (prev_level) to the parent.
585 Here, the current level is always one smaller than the
586 previous one. */
587 assert (level == prev_level - 1);
588 duinfo_add (&dui_to_print, &dulvl[prev_level].ent);
589 if (!opt_separate_dirs)
590 duinfo_add (&dui_to_print, &dulvl[prev_level].subdir);
591 duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].ent);
592 duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].subdir);
596 prev_level = level;
598 /* Let the size of a directory entry contribute to the total for the
599 containing directory, unless --separate-dirs (-S) is specified. */
600 if ( ! (opt_separate_dirs && IS_DIR_TYPE (ent->fts_info)))
601 duinfo_add (&dulvl[level].ent, &dui);
603 /* Even if this directory is unreadable or we can't chdir into it,
604 do let its size contribute to the total, ... */
605 duinfo_add (&tot_dui, &dui);
607 /* ... but don't print out a total for it, since without the size(s)
608 of any potential entries, it could be very misleading. */
609 if (ent->fts_info == FTS_DNR)
610 return ok;
612 /* If we're not counting an entry, e.g., because it's a hard link
613 to a file we've already counted (and --count-links), then don't
614 print a line for it. */
615 if (!print)
616 return ok;
618 if ((IS_DIR_TYPE (ent->fts_info) && level <= max_depth)
619 || ((opt_all && level <= max_depth) || level == 0))
620 print_size (&dui_to_print, file);
622 return ok;
625 /* Recursively print the sizes of the directories (and, if selected, files)
626 named in FILES, the last entry of which is NULL.
627 BIT_FLAGS controls how fts works.
628 Return true if successful. */
630 static bool
631 du_files (char **files, int bit_flags)
633 bool ok = true;
635 if (*files)
637 FTS *fts = xfts_open (files, bit_flags, NULL);
639 while (1)
641 FTSENT *ent;
643 ent = fts_read (fts);
644 if (ent == NULL)
646 if (errno != 0)
648 /* FIXME: try to give a better message */
649 error (0, errno, _("fts_read failed"));
650 ok = false;
652 break;
654 FTS_CROSS_CHECK (fts);
656 ok &= process_file (fts, ent);
659 /* Ignore failure, since the only way it can do so is in failing to
660 return to the original directory, and since we're about to exit,
661 that doesn't matter. */
662 fts_close (fts);
665 if (print_grand_total)
666 print_size (&tot_dui, _("total"));
668 return ok;
672 main (int argc, char **argv)
674 int c;
675 char *cwd_only[2];
676 bool max_depth_specified = false;
677 char **files;
678 bool ok = true;
679 char *files_from = NULL;
680 struct Tokens tok;
682 /* Bit flags that control how fts works. */
683 int bit_flags = FTS_TIGHT_CYCLE_CHECK;
685 /* Select one of the three FTS_ options that control if/when
686 to follow a symlink. */
687 int symlink_deref_bits = FTS_PHYSICAL;
689 /* If true, display only a total for each argument. */
690 bool opt_summarize_only = false;
692 cwd_only[0] = ".";
693 cwd_only[1] = NULL;
695 initialize_main (&argc, &argv);
696 program_name = argv[0];
697 setlocale (LC_ALL, "");
698 bindtextdomain (PACKAGE, LOCALEDIR);
699 textdomain (PACKAGE);
701 atexit (close_stdout);
703 exclude = new_exclude ();
705 human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
706 &output_block_size);
708 while ((c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
709 long_options, NULL)) != -1)
711 switch (c)
713 #if DU_DEBUG
714 case 'd':
715 fts_debug = true;
716 break;
717 #endif
719 case '0':
720 opt_nul_terminate_output = true;
721 break;
723 case 'a':
724 opt_all = true;
725 break;
727 case APPARENT_SIZE_OPTION:
728 apparent_size = true;
729 break;
731 case 'b':
732 apparent_size = true;
733 human_output_opts = 0;
734 output_block_size = 1;
735 break;
737 case 'c':
738 print_grand_total = true;
739 break;
741 case 'h':
742 human_output_opts = human_autoscale | human_SI | human_base_1024;
743 output_block_size = 1;
744 break;
746 case 'H': /* FIXME: remove warning and move this "case 'H'" to
747 precede --dereference-args in late 2006. */
748 error (0, 0, _("WARNING: use --si, not -H; the meaning of the -H\
749 option will soon\nchange to be the same as that of --dereference-args (-D)"));
750 /* fall through */
751 case HUMAN_SI_OPTION:
752 human_output_opts = human_autoscale | human_SI;
753 output_block_size = 1;
754 break;
756 case KILOBYTES_LONG_OPTION:
757 error (0, 0,
758 _("the --kilobytes option is deprecated; use -k instead"));
759 /* fall through */
760 case 'k':
761 human_output_opts = 0;
762 output_block_size = 1024;
763 break;
765 case MAX_DEPTH_OPTION: /* --max-depth=N */
767 unsigned long int tmp_ulong;
768 if (xstrtoul (optarg, NULL, 0, &tmp_ulong, NULL) == LONGINT_OK
769 && tmp_ulong <= SIZE_MAX)
771 max_depth_specified = true;
772 max_depth = tmp_ulong;
774 else
776 error (0, 0, _("invalid maximum depth %s"),
777 quote (optarg));
778 ok = false;
781 break;
783 case MEGABYTES_LONG_OPTION:
784 error (0, 0,
785 _("the --megabytes option is deprecated; use -m instead"));
786 /* fall through */
787 case 'm':
788 human_output_opts = 0;
789 output_block_size = 1024 * 1024;
790 break;
792 case 'l':
793 opt_count_all = true;
794 break;
796 case 's':
797 opt_summarize_only = true;
798 break;
800 case 'x':
801 bit_flags |= FTS_XDEV;
802 break;
804 case 'B':
805 human_output_opts = human_options (optarg, true, &output_block_size);
806 break;
808 case 'D': /* This will eventually be 'H' (-H), too. */
809 symlink_deref_bits = FTS_COMFOLLOW | FTS_PHYSICAL;
810 break;
812 case 'L': /* --dereference */
813 symlink_deref_bits = FTS_LOGICAL;
814 break;
816 case 'P': /* --no-dereference */
817 symlink_deref_bits = FTS_PHYSICAL;
818 break;
820 case 'S':
821 opt_separate_dirs = true;
822 break;
824 case 'X':
825 if (add_exclude_file (add_exclude, exclude, optarg,
826 EXCLUDE_WILDCARDS, '\n'))
828 error (0, errno, "%s", quotearg_colon (optarg));
829 ok = false;
831 break;
833 case FILES0_FROM_OPTION:
834 files_from = optarg;
835 break;
837 case EXCLUDE_OPTION:
838 add_exclude (exclude, optarg, EXCLUDE_WILDCARDS);
839 break;
841 case TIME_OPTION:
842 opt_time = true;
843 time_type =
844 (optarg
845 ? XARGMATCH ("--time", optarg, time_args, time_types)
846 : time_mtime);
847 break;
849 case TIME_STYLE_OPTION:
850 time_style = optarg;
851 break;
853 case_GETOPT_HELP_CHAR;
855 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
857 default:
858 ok = false;
862 if (!ok)
863 usage (EXIT_FAILURE);
865 if (opt_all & opt_summarize_only)
867 error (0, 0, _("cannot both summarize and show all entries"));
868 usage (EXIT_FAILURE);
871 if (opt_summarize_only && max_depth_specified && max_depth == 0)
873 error (0, 0,
874 _("warning: summarizing is the same as using --max-depth=0"));
877 if (opt_summarize_only && max_depth_specified && max_depth != 0)
879 unsigned long int d = max_depth;
880 error (0, 0, _("warning: summarizing conflicts with --max-depth=%lu"), d);
881 usage (EXIT_FAILURE);
884 if (opt_summarize_only)
885 max_depth = 0;
887 /* Process time style if printing last times. */
888 if (opt_time)
890 if (! time_style)
892 time_style = getenv ("TIME_STYLE");
894 /* Ignore TIMESTYLE="locale", for compatibility with ls. */
895 if (! time_style || STREQ (time_style, "locale"))
896 time_style = "long-iso";
897 else if (*time_style == '+')
899 /* Ignore anything after a newline, for compatibility
900 with ls. */
901 char *p = strchr (time_style, '\n');
902 if (p)
903 *p = '\0';
905 else
907 /* Ignore "posix-" prefix, for compatibility with ls. */
908 static char const posix_prefix[] = "posix-";
909 while (strncmp (time_style, posix_prefix, sizeof posix_prefix - 1)
910 == 0)
911 time_style += sizeof posix_prefix - 1;
915 if (*time_style == '+')
916 time_format = time_style + 1;
917 else
919 switch (XARGMATCH ("time style", time_style,
920 time_style_args, time_style_types))
922 case full_iso_time_style:
923 time_format = "%Y-%m-%d %H:%M:%S.%N %z";
924 break;
926 case long_iso_time_style:
927 time_format = "%Y-%m-%d %H:%M";
928 break;
930 case iso_time_style:
931 time_format = "%Y-%m-%d";
932 break;
937 if (files_from)
939 /* When using --files0-from=F, you may not specify any files
940 on the command-line. */
941 if (optind < argc)
943 error (0, 0, _("extra operand %s"), quote (argv[optind]));
944 fprintf (stderr, "%s\n",
945 _("File operands cannot be combined with --files0-from."));
946 usage (EXIT_FAILURE);
949 if (! (STREQ (files_from, "-") || freopen (files_from, "r", stdin)))
950 error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
951 quote (files_from));
953 readtokens0_init (&tok);
955 if (! readtokens0 (stdin, &tok) || fclose (stdin) != 0)
956 error (EXIT_FAILURE, 0, _("cannot read file names from %s"),
957 quote (files_from));
959 files = tok.tok;
961 else
963 files = (optind < argc ? argv + optind : cwd_only);
966 /* Initialize the hash structure for inode numbers. */
967 hash_init ();
969 /* Report and filter out any empty file names before invoking fts.
970 This works around a glitch in fts, which fails immediately
971 (without looking at the other file names) when given an empty
972 file name. */
974 size_t i = 0;
975 size_t j;
977 for (j = 0; ; j++)
979 if (i != j)
980 files[i] = files[j];
982 if ( ! files[i])
983 break;
985 if (files[i][0])
986 i++;
987 else
989 if (files_from)
991 /* Using the standard `filename:line-number:' prefix here is
992 not totally appropriate, since NUL is the separator, not NL,
993 but it might be better than nothing. */
994 unsigned long int file_number = j + 1;
995 error (0, 0, "%s:%lu: %s", quotearg_colon (files_from),
996 file_number, _("invalid zero-length file name"));
998 else
999 error (0, 0, "%s", _("invalid zero-length file name"));
1003 ok = (i == j);
1006 bit_flags |= symlink_deref_bits;
1007 ok &= du_files (files, bit_flags);
1009 /* This isn't really necessary, but it does ensure we
1010 exercise this function. */
1011 if (files_from)
1012 readtokens0_free (&tok);
1014 hash_free (htab);
1016 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);