Require that this test be run as non-root.
[coreutils.git] / src / du.c
blob7af3dc167620c1cda27fef79ae5cbf8c1961ee7e
1 /* du -- summarize disk usage
2 Copyright (C) 1988-1991, 1995-2005 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 "dirname.h" /* for strip_trailing_slashes */
35 #include "error.h"
36 #include "exclude.h"
37 #include "fprintftime.h"
38 #include "hash.h"
39 #include "human.h"
40 #include "inttostr.h"
41 #include "quote.h"
42 #include "quotearg.h"
43 #include "readtokens0.h"
44 #include "same.h"
45 #include "stat-time.h"
46 #include "xfts.h"
47 #include "xstrtol.h"
49 extern bool fts_debug;
51 /* The official name of this program (e.g., no `g' prefix). */
52 #define PROGRAM_NAME "du"
54 #define AUTHORS \
55 "Torbjorn Granlund", "David MacKenzie, Paul Eggert", "Jim Meyering"
57 #if DU_DEBUG
58 # define FTS_CROSS_CHECK(Fts) fts_cross_check (Fts)
59 # define DEBUG_OPT "d"
60 #else
61 # define FTS_CROSS_CHECK(Fts)
62 # define DEBUG_OPT
63 #endif
65 /* Initial size of the hash table. */
66 #define INITIAL_TABLE_SIZE 103
68 /* Hash structure for inode and device numbers. The separate entry
69 structure makes it easier to rehash "in place". */
71 struct entry
73 ino_t st_ino;
74 dev_t st_dev;
77 /* A set of dev/ino pairs. */
78 static Hash_table *htab;
80 /* Define a class for collecting directory information. */
82 struct duinfo
84 /* Size of files in directory. */
85 uintmax_t size;
87 /* Latest time stamp found. If tmax.tv_sec == TYPE_MINIMUM (time_t)
88 && tmax.tv_nsec < 0, no time stamp has been found. */
89 struct timespec tmax;
92 /* Initialize directory data. */
93 static inline void
94 duinfo_init (struct duinfo *a)
96 a->size = 0;
97 a->tmax.tv_sec = TYPE_MINIMUM (time_t);
98 a->tmax.tv_nsec = -1;
101 /* Set directory data. */
102 static inline void
103 duinfo_set (struct duinfo *a, uintmax_t size, struct timespec tmax)
105 a->size = size;
106 a->tmax = tmax;
109 /* Accumulate directory data. */
110 static inline void
111 duinfo_add (struct duinfo *a, struct duinfo const *b)
113 a->size += b->size;
114 if (timespec_cmp (a->tmax, b->tmax) < 0)
115 a->tmax = b->tmax;
118 /* A structure for per-directory level information. */
119 struct dulevel
121 /* Entries in this directory. */
122 struct duinfo ent;
124 /* Total for subdirectories. */
125 struct duinfo subdir;
128 /* Name under which this program was invoked. */
129 char *program_name;
131 /* If true, display counts for all files, not just directories. */
132 static bool opt_all = false;
134 /* If true, rather than using the disk usage of each file,
135 use the apparent size (a la stat.st_size). */
136 static bool apparent_size = false;
138 /* If true, count each hard link of files with multiple links. */
139 static bool opt_count_all = false;
141 /* If true, output the NUL byte instead of a newline at the end of each line. */
142 static bool opt_nul_terminate_output = false;
144 /* If true, print a grand total at the end. */
145 static bool print_grand_total = false;
147 /* If nonzero, do not add sizes of subdirectories. */
148 static bool opt_separate_dirs = false;
150 /* Show the total for each directory (and file if --all) that is at
151 most MAX_DEPTH levels down from the root of the hierarchy. The root
152 is at level 0, so `du --max-depth=0' is equivalent to `du -s'. */
153 static size_t max_depth = SIZE_MAX;
155 /* Human-readable options for output. */
156 static int human_output_opts;
158 /* If true, print most recently modified date, using the specified format. */
159 static bool opt_time = false;
161 /* Type of time to display. controlled by --time. */
163 enum time_type
165 time_mtime, /* default */
166 time_ctime,
167 time_atime
170 static enum time_type time_type = time_mtime;
172 /* User specified date / time style */
173 static char const *time_style = NULL;
175 /* Format used to display date / time. Controlled by --time-style */
176 static char const *time_format = NULL;
178 /* The units to use when printing sizes. */
179 static uintmax_t output_block_size;
181 /* File name patterns to exclude. */
182 static struct exclude *exclude;
184 /* Grand total size of all args, in bytes. Also latest modified date. */
185 static struct duinfo tot_dui;
187 #define IS_DIR_TYPE(Type) \
188 ((Type) == FTS_DP \
189 || (Type) == FTS_DNR)
191 /* For long options that have no equivalent short option, use a
192 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
193 enum
195 APPARENT_SIZE_OPTION = CHAR_MAX + 1,
196 EXCLUDE_OPTION,
197 FILES0_FROM_OPTION,
198 HUMAN_SI_OPTION,
199 MAX_DEPTH_OPTION,
200 TIME_OPTION,
201 TIME_STYLE_OPTION
204 static struct option const long_options[] =
206 {"all", no_argument, NULL, 'a'},
207 {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
208 {"block-size", required_argument, NULL, 'B'},
209 {"bytes", no_argument, NULL, 'b'},
210 {"count-links", no_argument, NULL, 'l'},
211 {"dereference", no_argument, NULL, 'L'},
212 {"dereference-args", no_argument, NULL, 'D'},
213 {"exclude", required_argument, NULL, EXCLUDE_OPTION},
214 {"exclude-from", required_argument, NULL, 'X'},
215 {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
216 {"human-readable", no_argument, NULL, 'h'},
217 {"si", no_argument, NULL, HUMAN_SI_OPTION},
218 {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */
219 {"max-depth", required_argument, NULL, MAX_DEPTH_OPTION},
220 {"null", no_argument, NULL, '0'},
221 {"megabytes", no_argument, NULL, 'm'}, /* obsolescent */
222 {"no-dereference", no_argument, NULL, 'P'},
223 {"one-file-system", no_argument, NULL, 'x'},
224 {"separate-dirs", no_argument, NULL, 'S'},
225 {"summarize", no_argument, NULL, 's'},
226 {"total", no_argument, NULL, 'c'},
227 {"time", optional_argument, NULL, TIME_OPTION},
228 {"time-style", required_argument, NULL, TIME_STYLE_OPTION},
229 {GETOPT_HELP_OPTION_DECL},
230 {GETOPT_VERSION_OPTION_DECL},
231 {NULL, 0, NULL, 0}
234 static char const *const time_args[] =
236 "atime", "access", "use", "ctime", "status", NULL
238 static enum time_type const time_types[] =
240 time_atime, time_atime, time_atime, time_ctime, time_ctime
242 ARGMATCH_VERIFY (time_args, time_types);
244 /* `full-iso' uses full ISO-style dates and times. `long-iso' uses longer
245 ISO-style time stamps, though shorter than `full-iso'. `iso' uses shorter
246 ISO-style time stamps. */
247 enum time_style
249 full_iso_time_style, /* --time-style=full-iso */
250 long_iso_time_style, /* --time-style=long-iso */
251 iso_time_style /* --time-style=iso */
254 static char const *const time_style_args[] =
256 "full-iso", "long-iso", "iso", NULL
258 static enum time_style const time_style_types[] =
260 full_iso_time_style, long_iso_time_style, iso_time_style
262 ARGMATCH_VERIFY (time_style_args, time_style_types);
264 void
265 usage (int status)
267 if (status != EXIT_SUCCESS)
268 fprintf (stderr, _("Try `%s --help' for more information.\n"),
269 program_name);
270 else
272 printf (_("\
273 Usage: %s [OPTION]... [FILE]...\n\
274 or: %s [OPTION]... --files0-from=F\n\
275 "), program_name, program_name);
276 fputs (_("\
277 Summarize disk usage of each FILE, recursively for directories.\n\
279 "), stdout);
280 fputs (_("\
281 Mandatory arguments to long options are mandatory for short options too.\n\
282 "), stdout);
283 fputs (_("\
284 -a, --all write counts for all files, not just directories\n\
285 --apparent-size print apparent sizes, rather than disk usage; although\n\
286 the apparent size is usually smaller, it may be\n\
287 larger due to holes in (`sparse') files, internal\n\
288 fragmentation, indirect blocks, and the like\n\
289 -B, --block-size=SIZE use SIZE-byte blocks\n\
290 -b, --bytes equivalent to `--apparent-size --block-size=1'\n\
291 -c, --total produce a grand total\n\
292 -D, --dereference-args dereference FILEs that are symbolic links\n\
293 "), stdout);
294 fputs (_("\
295 --files0-from=F summarize disk usage of the NUL-terminated file\n\
296 names specified in file F\n\
297 -H like --si, but also evokes a warning; will soon\n\
298 change to be equivalent to --dereference-args (-D)\n\
299 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\
300 --si like -h, but use powers of 1000 not 1024\n\
301 -k like --block-size=1K\n\
302 -l, --count-links count sizes many times if hard linked\n\
303 "), stdout);
304 fputs (_("\
305 -L, --dereference dereference all symbolic links\n\
306 -P, --no-dereference don't follow any symbolic links (this is the default)\n\
307 -0, --null end each output line with 0 byte rather than newline\n\
308 -S, --separate-dirs do not include size of subdirectories\n\
309 -s, --summarize display only a total for each argument\n\
310 "), stdout);
311 fputs (_("\
312 -x, --one-file-system skip directories on different file systems\n\
313 -X FILE, --exclude-from=FILE Exclude files that match any pattern in FILE.\n\
314 --exclude=PATTERN Exclude files that match PATTERN.\n\
315 --max-depth=N print the total for a directory (or file, with --all)\n\
316 only if it is N or fewer levels below the command\n\
317 line argument; --max-depth=0 is the same as\n\
318 --summarize\n\
319 "), stdout);
320 fputs (_("\
321 --time show time of the last modification of any file in the\n\
322 directory, or any of its subdirectories\n\
323 --time=WORD show time as WORD instead of modification time:\n\
324 atime, access, use, ctime or status\n\
325 --time-style=STYLE show times using style STYLE:\n\
326 full-iso, long-iso, iso, +FORMAT\n\
327 FORMAT is interpreted like `date'\n\
328 "), stdout);
329 fputs (HELP_OPTION_DESCRIPTION, stdout);
330 fputs (VERSION_OPTION_DESCRIPTION, stdout);
331 fputs (_("\n\
332 SIZE may be (or may be an integer optionally followed by) one of following:\n\
333 kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
334 "), stdout);
335 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
337 exit (status);
340 static size_t
341 entry_hash (void const *x, size_t table_size)
343 struct entry const *p = x;
345 /* Ignoring the device number here should be fine. */
346 /* The cast to uintmax_t prevents negative remainders
347 if st_ino is negative. */
348 return (uintmax_t) p->st_ino % table_size;
351 /* Compare two dev/ino pairs. Return true if they are the same. */
352 static bool
353 entry_compare (void const *x, void const *y)
355 struct entry const *a = x;
356 struct entry const *b = y;
357 return SAME_INODE (*a, *b) ? true : false;
360 /* Try to insert the INO/DEV pair into the global table, HTAB.
361 Return true if the pair is successfully inserted,
362 false if the pair is already in the table. */
363 static bool
364 hash_ins (ino_t ino, dev_t dev)
366 struct entry *ent;
367 struct entry *ent_from_table;
369 ent = xmalloc (sizeof *ent);
370 ent->st_ino = ino;
371 ent->st_dev = dev;
373 ent_from_table = hash_insert (htab, ent);
374 if (ent_from_table == NULL)
376 /* Insertion failed due to lack of memory. */
377 xalloc_die ();
380 if (ent_from_table == ent)
382 /* Insertion succeeded. */
383 return true;
386 /* That pair is already in the table, so ENT was not inserted. Free it. */
387 free (ent);
389 return false;
392 /* Initialize the hash table. */
393 static void
394 hash_init (void)
396 htab = hash_initialize (INITIAL_TABLE_SIZE, NULL,
397 entry_hash, entry_compare, free);
398 if (htab == NULL)
399 xalloc_die ();
402 /* FIXME: this code is nearly identical to code in date.c */
403 /* Display the date and time in WHEN according to the format specified
404 in FORMAT. */
406 static void
407 show_date (const char *format, struct timespec when)
409 struct tm *tm = localtime (&when.tv_sec);
410 if (! tm)
412 char buf[INT_BUFSIZE_BOUND (intmax_t)];
413 error (0, 0, _("time %s is out of range"),
414 (TYPE_SIGNED (time_t)
415 ? imaxtostr (when.tv_sec, buf)
416 : umaxtostr (when.tv_sec, buf)));
417 fputs (buf, stdout);
418 return;
421 fprintftime (stdout, format, tm, 0, when.tv_nsec);
424 /* Print N_BYTES. Convert it to a readable value before printing. */
426 static void
427 print_only_size (uintmax_t n_bytes)
429 char buf[LONGEST_HUMAN_READABLE + 1];
430 fputs (human_readable (n_bytes, buf, human_output_opts,
431 1, output_block_size), stdout);
434 /* Print size (and optionally time) indicated by *PDUI, followed by STRING. */
436 static void
437 print_size (const struct duinfo *pdui, const char *string)
439 print_only_size (pdui->size);
440 if (opt_time)
442 putchar ('\t');
443 show_date (time_format, pdui->tmax);
445 printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n');
446 fflush (stdout);
449 /* This function is called once for every file system object that fts
450 encounters. fts does a depth-first traversal. This function knows
451 that and accumulates per-directory totals based on changes in
452 the depth of the current entry. It returns true on success. */
454 static bool
455 process_file (FTS *fts, FTSENT *ent)
457 bool ok;
458 struct duinfo dui;
459 struct duinfo dui_to_print;
460 size_t level;
461 static size_t prev_level;
462 static size_t n_alloc;
463 /* First element of the structure contains:
464 The sum of the st_size values of all entries in the single directory
465 at the corresponding level. Although this does include the st_size
466 corresponding to each subdirectory, it does not include the size of
467 any file in a subdirectory. Also corresponding last modified date.
468 Second element of the structure contains:
469 The sum of the sizes of all entries in the hierarchy at or below the
470 directory at the specified level. */
471 static struct dulevel *dulvl;
472 bool print = true;
474 const char *file = ent->fts_path;
475 const struct stat *sb = ent->fts_statp;
476 bool skip;
478 /* If necessary, set FTS_SKIP before returning. */
479 skip = excluded_file_name (exclude, ent->fts_path);
480 if (skip)
481 fts_set (fts, ent, FTS_SKIP);
483 switch (ent->fts_info)
485 case FTS_NS:
486 error (0, ent->fts_errno, _("cannot access %s"), quote (file));
487 return false;
489 case FTS_ERR:
490 /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
491 error (0, ent->fts_errno, _("%s"), quote (file));
492 return false;
494 case FTS_DNR:
495 /* Don't return just yet, since although the directory is not readable,
496 we were able to stat it, so we do have a size. */
497 error (0, ent->fts_errno, _("cannot read directory %s"), quote (file));
498 ok = false;
499 break;
501 default:
502 ok = true;
503 break;
506 /* If this is the first (pre-order) encounter with a directory,
507 or if it's the second encounter for a skipped directory, then
508 return right away. */
509 if (ent->fts_info == FTS_D || skip)
510 return ok;
512 /* If the file is being excluded or if it has already been counted
513 via a hard link, then don't let it contribute to the sums. */
514 if (skip
515 || (!opt_count_all
516 && ! S_ISDIR (sb->st_mode)
517 && 1 < sb->st_nlink
518 && ! hash_ins (sb->st_ino, sb->st_dev)))
520 /* Note that we must not simply return here.
521 We still have to update prev_level and maybe propagate
522 some sums up the hierarchy. */
523 duinfo_init (&dui);
524 print = false;
526 else
528 duinfo_set (&dui,
529 (apparent_size
530 ? sb->st_size
531 : ST_NBLOCKS (*sb) * ST_NBLOCKSIZE),
532 (time_type == time_mtime ? get_stat_mtime (sb)
533 : time_type == time_atime ? get_stat_atime (sb)
534 : get_stat_ctime (sb)));
537 level = ent->fts_level;
538 dui_to_print = dui;
540 if (n_alloc == 0)
542 n_alloc = level + 10;
543 dulvl = xcalloc (n_alloc, sizeof *dulvl);
545 else
547 if (level == prev_level)
549 /* This is usually the most common case. Do nothing. */
551 else if (level > prev_level)
553 /* Descending the hierarchy.
554 Clear the accumulators for *all* levels between prev_level
555 and the current one. The depth may change dramatically,
556 e.g., from 1 to 10. */
557 size_t i;
559 if (n_alloc <= level)
561 dulvl = xnrealloc (dulvl, level, 2 * sizeof *dulvl);
562 n_alloc = level * 2;
565 for (i = prev_level + 1; i <= level; i++)
567 duinfo_init (&dulvl[i].ent);
568 duinfo_init (&dulvl[i].subdir);
571 else /* level < prev_level */
573 /* Ascending the hierarchy.
574 Process a directory only after all entries in that
575 directory have been processed. When the depth decreases,
576 propagate sums from the children (prev_level) to the parent.
577 Here, the current level is always one smaller than the
578 previous one. */
579 assert (level == prev_level - 1);
580 duinfo_add (&dui_to_print, &dulvl[prev_level].ent);
581 if (!opt_separate_dirs)
582 duinfo_add (&dui_to_print, &dulvl[prev_level].subdir);
583 duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].ent);
584 duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].subdir);
588 prev_level = level;
590 /* Let the size of a directory entry contribute to the total for the
591 containing directory, unless --separate-dirs (-S) is specified. */
592 if ( ! (opt_separate_dirs && IS_DIR_TYPE (ent->fts_info)))
593 duinfo_add (&dulvl[level].ent, &dui);
595 /* Even if this directory is unreadable or we can't chdir into it,
596 do let its size contribute to the total, ... */
597 duinfo_add (&tot_dui, &dui);
599 /* ... but don't print out a total for it, since without the size(s)
600 of any potential entries, it could be very misleading. */
601 if (ent->fts_info == FTS_DNR)
602 return ok;
604 /* If we're not counting an entry, e.g., because it's a hard link
605 to a file we've already counted (and --count-links), then don't
606 print a line for it. */
607 if (!print)
608 return ok;
610 if ((IS_DIR_TYPE (ent->fts_info) && level <= max_depth)
611 || ((opt_all && level <= max_depth) || level == 0))
612 print_size (&dui_to_print, file);
614 return ok;
617 /* Recursively print the sizes of the directories (and, if selected, files)
618 named in FILES, the last entry of which is NULL.
619 BIT_FLAGS controls how fts works.
620 Return true if successful. */
622 static bool
623 du_files (char **files, int bit_flags)
625 bool ok = true;
627 if (*files)
629 FTS *fts = xfts_open (files, bit_flags, NULL);
631 while (1)
633 FTSENT *ent;
635 ent = fts_read (fts);
636 if (ent == NULL)
638 if (errno != 0)
640 /* FIXME: try to give a better message */
641 error (0, errno, _("fts_read failed"));
642 ok = false;
644 break;
646 FTS_CROSS_CHECK (fts);
648 ok &= process_file (fts, ent);
651 /* Ignore failure, since the only way it can do so is in failing to
652 return to the original directory, and since we're about to exit,
653 that doesn't matter. */
654 fts_close (fts);
657 if (print_grand_total)
658 print_size (&tot_dui, _("total"));
660 return ok;
664 main (int argc, char **argv)
666 int c;
667 char *cwd_only[2];
668 bool max_depth_specified = false;
669 char **files;
670 bool ok = true;
671 char *files_from = NULL;
672 struct Tokens tok;
674 /* Bit flags that control how fts works. */
675 int bit_flags = FTS_PHYSICAL | FTS_TIGHT_CYCLE_CHECK;
677 /* If true, display only a total for each argument. */
678 bool opt_summarize_only = false;
680 cwd_only[0] = ".";
681 cwd_only[1] = NULL;
683 initialize_main (&argc, &argv);
684 program_name = argv[0];
685 setlocale (LC_ALL, "");
686 bindtextdomain (PACKAGE, LOCALEDIR);
687 textdomain (PACKAGE);
689 atexit (close_stdout);
691 exclude = new_exclude ();
693 human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
694 &output_block_size);
696 while ((c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
697 long_options, NULL)) != -1)
699 switch (c)
701 #if DU_DEBUG
702 case 'd':
703 fts_debug = true;
704 break;
705 #endif
707 case '0':
708 opt_nul_terminate_output = true;
709 break;
711 case 'a':
712 opt_all = true;
713 break;
715 case APPARENT_SIZE_OPTION:
716 apparent_size = true;
717 break;
719 case 'b':
720 apparent_size = true;
721 human_output_opts = 0;
722 output_block_size = 1;
723 break;
725 case 'c':
726 print_grand_total = true;
727 break;
729 case 'h':
730 human_output_opts = human_autoscale | human_SI | human_base_1024;
731 output_block_size = 1;
732 break;
734 case 'H':
735 error (0, 0, _("WARNING: use --si, not -H; the meaning of the -H\
736 option will soon\nchange to be the same as that of --dereference-args (-D)"));
737 /* fall through */
738 case HUMAN_SI_OPTION:
739 human_output_opts = human_autoscale | human_SI;
740 output_block_size = 1;
741 break;
743 case 'k':
744 human_output_opts = 0;
745 output_block_size = 1024;
746 break;
748 case MAX_DEPTH_OPTION: /* --max-depth=N */
750 unsigned long int tmp_ulong;
751 if (xstrtoul (optarg, NULL, 0, &tmp_ulong, NULL) == LONGINT_OK
752 && tmp_ulong <= SIZE_MAX)
754 max_depth_specified = true;
755 max_depth = tmp_ulong;
757 else
759 error (0, 0, _("invalid maximum depth %s"),
760 quote (optarg));
761 ok = false;
764 break;
766 case 'm': /* obsolescent: FIXME: remove in 2005. */
767 human_output_opts = 0;
768 output_block_size = 1024 * 1024;
769 break;
771 case 'l':
772 opt_count_all = true;
773 break;
775 case 's':
776 opt_summarize_only = true;
777 break;
779 case 'x':
780 bit_flags |= FTS_XDEV;
781 break;
783 case 'B':
784 human_output_opts = human_options (optarg, true, &output_block_size);
785 break;
787 case 'D': /* This will eventually be 'H' (-H), too. */
788 bit_flags = FTS_COMFOLLOW;
789 break;
791 case 'L': /* --dereference */
792 bit_flags = FTS_LOGICAL;
793 break;
795 case 'P': /* --no-dereference */
796 bit_flags = FTS_PHYSICAL;
797 break;
799 case 'S':
800 opt_separate_dirs = true;
801 break;
803 case 'X':
804 if (add_exclude_file (add_exclude, exclude, optarg,
805 EXCLUDE_WILDCARDS, '\n'))
807 error (0, errno, "%s", quotearg_colon (optarg));
808 ok = false;
810 break;
812 case FILES0_FROM_OPTION:
813 files_from = optarg;
814 break;
816 case EXCLUDE_OPTION:
817 add_exclude (exclude, optarg, EXCLUDE_WILDCARDS);
818 break;
820 case TIME_OPTION:
821 opt_time = true;
822 time_type =
823 (optarg
824 ? XARGMATCH ("--time", optarg, time_args, time_types)
825 : time_mtime);
826 break;
828 case TIME_STYLE_OPTION:
829 time_style = optarg;
830 break;
832 case_GETOPT_HELP_CHAR;
834 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
836 default:
837 ok = false;
841 if (!ok)
842 usage (EXIT_FAILURE);
844 if (opt_all & opt_summarize_only)
846 error (0, 0, _("cannot both summarize and show all entries"));
847 usage (EXIT_FAILURE);
850 if (opt_summarize_only && max_depth_specified && max_depth == 0)
852 error (0, 0,
853 _("warning: summarizing is the same as using --max-depth=0"));
856 if (opt_summarize_only && max_depth_specified && max_depth != 0)
858 unsigned long int d = max_depth;
859 error (0, 0, _("warning: summarizing conflicts with --max-depth=%lu"), d);
860 usage (EXIT_FAILURE);
863 if (opt_summarize_only)
864 max_depth = 0;
866 /* Process time style if printing last times. */
867 if (opt_time)
869 if (! time_style)
871 time_style = getenv ("TIME_STYLE");
873 /* Ignore TIMESTYLE="locale", for compatibility with ls. */
874 if (! time_style || STREQ (time_style, "locale"))
875 time_style = "long-iso";
876 else if (*time_style == '+')
878 /* Ignore anything after a newline, for compatibility
879 with ls. */
880 char *p = strchr (time_style, '\n');
881 if (p)
882 *p = '\0';
884 else
886 /* Ignore "posix-" prefix, for compatibility with ls. */
887 static char const posix_prefix[] = "posix-";
888 while (strncmp (time_style, posix_prefix, sizeof posix_prefix - 1)
889 == 0)
890 time_style += sizeof posix_prefix - 1;
894 if (*time_style == '+')
895 time_format = time_style + 1;
896 else
898 switch (XARGMATCH ("time style", time_style,
899 time_style_args, time_style_types))
901 case full_iso_time_style:
902 time_format = "%Y-%m-%d %H:%M:%S.%N %z";
903 break;
905 case long_iso_time_style:
906 time_format = "%Y-%m-%d %H:%M";
907 break;
909 case iso_time_style:
910 time_format = "%Y-%m-%d";
911 break;
916 if (files_from)
918 /* When using --files0-from=F, you may not specify any files
919 on the command-line. */
920 if (optind < argc)
922 error (0, 0, _("extra operand %s"), quote (argv[optind]));
923 fprintf (stderr, "%s\n",
924 _("File operands cannot be combined with --files0-from."));
925 usage (EXIT_FAILURE);
928 if (! (STREQ (files_from, "-") || freopen (files_from, "r", stdin)))
929 error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
930 quote (files_from));
932 readtokens0_init (&tok);
934 if (! readtokens0 (stdin, &tok) || fclose (stdin) != 0)
935 error (EXIT_FAILURE, 0, _("cannot read file names from %s"),
936 quote (files_from));
938 files = tok.tok;
940 else
942 files = (optind < argc ? argv + optind : cwd_only);
945 /* Initialize the hash structure for inode numbers. */
946 hash_init ();
948 /* Report and filter out any empty file names before invoking fts.
949 This works around a glitch in fts, which fails immediately
950 (without looking at the other file names) when given an empty
951 file name. */
953 size_t i = 0;
954 size_t j;
956 for (j = 0; ; j++)
958 if (i != j)
959 files[i] = files[j];
961 if ( ! files[i])
962 break;
964 if (files[i][0])
965 i++;
966 else
968 if (files_from)
970 /* Using the standard `filename:line-number:' prefix here is
971 not totally appropriate, since NUL is the separator, not NL,
972 but it might be better than nothing. */
973 unsigned long int file_number = j + 1;
974 error (0, 0, "%s:%lu: %s", quotearg_colon (files_from),
975 file_number, _("invalid zero-length file name"));
977 else
978 error (0, 0, "%s", _("invalid zero-length file name"));
982 ok = (i == j);
985 ok &= du_files (files, bit_flags);
987 /* This isn't really necessary, but it does ensure we
988 exercise this function. */
989 if (files_from)
990 readtokens0_free (&tok);
992 hash_free (htab);
994 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);