08d29d241f7b894baae8234993d77062bd01d29d
[platform/upstream/coreutils.git] / src / du.c
1 /* du -- summarize disk usage
2    Copyright (C) 1988-1991, 1995-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Differences from the Unix du:
19    * Doesn't simply ignore the names of regular files given as arguments
20      when -a is given.
21
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.  */
26
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 "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 "strftime.h"
45 #include "xanstrftime.h"
46 #include "xfts.h"
47 #include "xstrtol.h"
48
49 extern bool fts_debug;
50
51 /* The official name of this program (e.g., no `g' prefix).  */
52 #define PROGRAM_NAME "du"
53
54 #define AUTHORS \
55   "Torbjorn Granlund", "David MacKenzie, Paul Eggert", "Jim Meyering"
56
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
64
65 /* Initial size of the hash table.  */
66 #define INITIAL_TABLE_SIZE 103
67
68 /* Hash structure for inode and device numbers.  The separate entry
69    structure makes it easier to rehash "in place".  */
70
71 struct entry
72 {
73   ino_t st_ino;
74   dev_t st_dev;
75 };
76
77 /* A set of dev/ino pairs.  */
78 static Hash_table *htab;
79
80 /* Define a class for collecting directory information. */
81
82 struct duinfo
83 {
84   /* Size of files in directory.  */
85   uintmax_t size;
86
87   /* Latest time stamp found.  If dmax == TYPE_MINIMUM (time_t) && nsec < 0,
88      no time stamp has been found.  */
89   time_t dmax;
90   int nsec;
91 };
92
93 /* Initialize directory data.  */
94 static inline void
95 duinfo_init (struct duinfo *a)
96 {
97   a->size = 0;
98   a->dmax = TYPE_MINIMUM (time_t);
99   a->nsec = -1;
100 }
101
102 /* Set directory data.  */
103 static inline void
104 duinfo_set (struct duinfo *a, uintmax_t size, time_t dmax, int nsec)
105 {
106   a->size = size;
107   a->dmax = dmax;
108   a->nsec = nsec;
109 }
110
111 /* Accumulate directory data.  */
112 static inline void
113 duinfo_add (struct duinfo *a, struct duinfo const *b)
114 {
115   a->size += b->size;
116   if (a->dmax < b->dmax
117       || (a->dmax == b->dmax && a->nsec < b->nsec))
118     {
119       a->dmax = b->dmax;
120       a->nsec = b->nsec;
121     }
122 }
123
124 /* A structure for per-directory level information.  */
125 struct dulevel
126 {
127   /* Entries in this directory.  */
128   struct duinfo ent;
129
130   /* Total for subdirectories.  */
131   struct duinfo subdir;
132 };
133
134 /* Name under which this program was invoked.  */
135 char *program_name;
136
137 /* If true, display counts for all files, not just directories.  */
138 static bool opt_all = false;
139
140 /* If true, rather than using the disk usage of each file,
141    use the apparent size (a la stat.st_size).  */
142 static bool apparent_size = false;
143
144 /* If true, count each hard link of files with multiple links.  */
145 static bool opt_count_all = false;
146
147 /* If true, output the NUL byte instead of a newline at the end of each line. */
148 static bool opt_nul_terminate_output = false;
149
150 /* If true, print a grand total at the end.  */
151 static bool print_grand_total = false;
152
153 /* If nonzero, do not add sizes of subdirectories.  */
154 static bool opt_separate_dirs = false;
155
156 /* Show the total for each directory (and file if --all) that is at
157    most MAX_DEPTH levels down from the root of the hierarchy.  The root
158    is at level 0, so `du --max-depth=0' is equivalent to `du -s'.  */
159 static size_t max_depth = SIZE_MAX;
160
161 /* Human-readable options for output.  */
162 static int human_output_opts;
163
164 /* If true, print most recently modified date, using the specified format.  */
165 static bool opt_time = false;
166
167 /* Type of time to display. controlled by --time.  */
168
169 enum time_type
170   {
171     time_mtime,                 /* default */
172     time_ctime,
173     time_atime
174   };
175
176 static enum time_type time_type = time_mtime;
177
178 /* User specified date / time style */
179 static char const *time_style = NULL;
180
181 /* Format used to display date / time. Controlled by --time-style */
182 static char const *time_format = NULL;
183
184 /* The units to use when printing sizes.  */
185 static uintmax_t output_block_size;
186
187 /* File name patterns to exclude.  */
188 static struct exclude *exclude;
189
190 /* Grand total size of all args, in bytes. Also latest modified date. */
191 static struct duinfo tot_dui;
192
193 #define IS_DIR_TYPE(Type)       \
194   ((Type) == FTS_DP             \
195    || (Type) == FTS_DNR)
196
197 /* For long options that have no equivalent short option, use a
198    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
199 enum
200 {
201   APPARENT_SIZE_OPTION = CHAR_MAX + 1,
202   EXCLUDE_OPTION,
203   FILES0_FROM_OPTION,
204   HUMAN_SI_OPTION,
205   MAX_DEPTH_OPTION,
206   TIME_OPTION,
207   TIME_STYLE_OPTION
208 };
209
210 static struct option const long_options[] =
211 {
212   {"all", no_argument, NULL, 'a'},
213   {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
214   {"block-size", required_argument, NULL, 'B'},
215   {"bytes", no_argument, NULL, 'b'},
216   {"count-links", no_argument, NULL, 'l'},
217   {"dereference", no_argument, NULL, 'L'},
218   {"dereference-args", no_argument, NULL, 'D'},
219   {"exclude", required_argument, NULL, EXCLUDE_OPTION},
220   {"exclude-from", required_argument, NULL, 'X'},
221   {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
222   {"human-readable", no_argument, NULL, 'h'},
223   {"si", no_argument, NULL, HUMAN_SI_OPTION},
224   {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */
225   {"max-depth", required_argument, NULL, MAX_DEPTH_OPTION},
226   {"null", no_argument, NULL, '0'},
227   {"megabytes", no_argument, NULL, 'm'}, /* obsolescent */
228   {"no-dereference", no_argument, NULL, 'P'},
229   {"one-file-system", no_argument, NULL, 'x'},
230   {"separate-dirs", no_argument, NULL, 'S'},
231   {"summarize", no_argument, NULL, 's'},
232   {"total", no_argument, NULL, 'c'},
233   {"time", optional_argument, NULL, TIME_OPTION},
234   {"time-style", required_argument, NULL, TIME_STYLE_OPTION},
235   {GETOPT_HELP_OPTION_DECL},
236   {GETOPT_VERSION_OPTION_DECL},
237   {NULL, 0, NULL, 0}
238 };
239
240 static char const *const time_args[] =
241 {
242   "atime", "access", "use", "ctime", "status", NULL
243 };
244 static enum time_type const time_types[] =
245 {
246   time_atime, time_atime, time_atime, time_ctime, time_ctime
247 };
248 ARGMATCH_VERIFY (time_args, time_types);
249
250 /* `full-iso' uses full ISO-style dates and times.  `long-iso' uses longer
251    ISO-style time stamps, though shorter than `full-iso'.  `iso' uses shorter
252    ISO-style time stamps.  */
253 enum time_style
254   {
255     full_iso_time_style,       /* --time-style=full-iso */
256     long_iso_time_style,       /* --time-style=long-iso */
257     iso_time_style             /* --time-style=iso */
258   };
259
260 static char const *const time_style_args[] =
261 {
262   "full-iso", "long-iso", "iso", NULL
263 };
264 static enum time_style const time_style_types[] =
265 {
266   full_iso_time_style, long_iso_time_style, iso_time_style
267 };
268 ARGMATCH_VERIFY (time_style_args, time_style_types);
269
270 void
271 usage (int status)
272 {
273   if (status != EXIT_SUCCESS)
274     fprintf (stderr, _("Try `%s --help' for more information.\n"),
275              program_name);
276   else
277     {
278       printf (_("\
279 Usage: %s [OPTION]... [FILE]...\n\
280   or:  %s [OPTION]... --files0-from=F\n\
281 "), program_name, program_name);
282       fputs (_("\
283 Summarize disk usage of each FILE, recursively for directories.\n\
284 \n\
285 "), stdout);
286       fputs (_("\
287 Mandatory arguments to long options are mandatory for short options too.\n\
288 "), stdout);
289       fputs (_("\
290   -a, --all             write counts for all files, not just directories\n\
291       --apparent-size   print apparent sizes, rather than disk usage; although\n\
292                           the apparent size is usually smaller, it may be\n\
293                           larger due to holes in (`sparse') files, internal\n\
294                           fragmentation, indirect blocks, and the like\n\
295   -B, --block-size=SIZE use SIZE-byte blocks\n\
296   -b, --bytes           equivalent to `--apparent-size --block-size=1'\n\
297   -c, --total           produce a grand total\n\
298   -D, --dereference-args  dereference FILEs that are symbolic links\n\
299 "), stdout);
300       fputs (_("\
301       --files0-from=F   summarize disk usage of the NUL-terminated file\n\
302                           names specified in file F\n\
303   -H                    like --si, but also evokes a warning; will soon\n\
304                           change to be equivalent to --dereference-args (-D)\n\
305   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)\n\
306       --si              like -h, but use powers of 1000 not 1024\n\
307   -k                    like --block-size=1K\n\
308   -l, --count-links     count sizes many times if hard linked\n\
309 "), stdout);
310       fputs (_("\
311   -L, --dereference     dereference all symbolic links\n\
312   -P, --no-dereference  don't follow any symbolic links (this is the default)\n\
313   -0, --null            end each output line with 0 byte rather than newline\n\
314   -S, --separate-dirs   do not include size of subdirectories\n\
315   -s, --summarize       display only a total for each argument\n\
316 "), stdout);
317       fputs (_("\
318   -x, --one-file-system  skip directories on different file systems\n\
319   -X FILE, --exclude-from=FILE  Exclude files that match any pattern in FILE.\n\
320       --exclude=PATTERN Exclude files that match PATTERN.\n\
321       --max-depth=N     print the total for a directory (or file, with --all)\n\
322                           only if it is N or fewer levels below the command\n\
323                           line argument;  --max-depth=0 is the same as\n\
324                           --summarize\n\
325 "), stdout);
326       fputs (_("\
327       --time            show time of the last modification of any file in the\n\
328                           directory, or any of its subdirectories\n\
329       --time=WORD       show time as WORD instead of modification time:\n\
330                           atime, access, use, ctime or status\n\
331       --time-style=STYLE show times using style STYLE:\n\
332                           full-iso, long-iso, iso, +FORMAT\n\
333                           FORMAT is interpreted like `date'\n\
334 "), stdout);
335       fputs (HELP_OPTION_DESCRIPTION, stdout);
336       fputs (VERSION_OPTION_DESCRIPTION, stdout);
337       fputs (_("\n\
338 SIZE may be (or may be an integer optionally followed by) one of following:\n\
339 kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
340 "), stdout);
341       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
342     }
343   exit (status);
344 }
345
346 static size_t
347 entry_hash (void const *x, size_t table_size)
348 {
349   struct entry const *p = x;
350
351   /* Ignoring the device number here should be fine.  */
352   /* The cast to uintmax_t prevents negative remainders
353      if st_ino is negative.  */
354   return (uintmax_t) p->st_ino % table_size;
355 }
356
357 /* Compare two dev/ino pairs.  Return true if they are the same.  */
358 static bool
359 entry_compare (void const *x, void const *y)
360 {
361   struct entry const *a = x;
362   struct entry const *b = y;
363   return SAME_INODE (*a, *b) ? true : false;
364 }
365
366 /* Try to insert the INO/DEV pair into the global table, HTAB.
367    Return true if the pair is successfully inserted,
368    false if the pair is already in the table.  */
369 static bool
370 hash_ins (ino_t ino, dev_t dev)
371 {
372   struct entry *ent;
373   struct entry *ent_from_table;
374
375   ent = xmalloc (sizeof *ent);
376   ent->st_ino = ino;
377   ent->st_dev = dev;
378
379   ent_from_table = hash_insert (htab, ent);
380   if (ent_from_table == NULL)
381     {
382       /* Insertion failed due to lack of memory.  */
383       xalloc_die ();
384     }
385
386   if (ent_from_table == ent)
387     {
388       /* Insertion succeeded.  */
389       return true;
390     }
391
392   /* That pair is already in the table, so ENT was not inserted.  Free it.  */
393   free (ent);
394
395   return false;
396 }
397
398 /* Initialize the hash table.  */
399 static void
400 hash_init (void)
401 {
402   htab = hash_initialize (INITIAL_TABLE_SIZE, NULL,
403                           entry_hash, entry_compare, free);
404   if (htab == NULL)
405     xalloc_die ();
406 }
407
408 /* FIXME: this code is nearly identical to code in date.c  */
409 /* Display the date and time in WHEN/NSEC according to the format specified
410    in TIME_FORMAT.  If TIME_FORMAT is NULL, use the standard output format.
411    Return zero if successful.  */
412
413 static void
414 show_date (const char *format, time_t when, int nsec)
415 {
416   char *out;
417   struct tm *tm = localtime (&when);
418   if (! tm)
419     {
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, buf)
424               : umaxtostr (when, buf)));
425       fputs (buf, stdout);
426       return;
427     }
428
429   out = xanstrftime (format, tm, 0, nsec);
430   fputs (out, stdout);
431   free (out);
432 }
433
434 /* Print N_BYTES.  Convert it to a readable value before printing.  */
435
436 static void
437 print_only_size (uintmax_t n_bytes)
438 {
439   char buf[LONGEST_HUMAN_READABLE + 1];
440   fputs (human_readable (n_bytes, buf, human_output_opts,
441                          1, output_block_size), stdout);
442 }
443
444 /* Print size (and optionally time) indicated by *PDUI, followed by STRING.  */
445
446 static void
447 print_size (const struct duinfo *pdui, const char *string)
448 {
449   print_only_size (pdui->size);
450   if (opt_time)
451     {
452       putchar ('\t');
453       show_date (time_format, pdui->dmax, pdui->nsec);
454     }
455   printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n');
456   fflush (stdout);
457 }
458
459 /* This function is called once for every file system object that fts
460    encounters.  fts does a depth-first traversal.  This function knows
461    that and accumulates per-directory totals based on changes in
462    the depth of the current entry.  It returns true on success.  */
463
464 static bool
465 process_file (FTS *fts, FTSENT *ent)
466 {
467   bool ok;
468   struct duinfo dui;
469   struct duinfo dui_to_print;
470   size_t level;
471   static size_t prev_level;
472   static size_t n_alloc;
473   /* First element of the structure contains:
474      The sum of the st_size values of all entries in the single directory
475      at the corresponding level.  Although this does include the st_size
476      corresponding to each subdirectory, it does not include the size of
477      any file in a subdirectory. Also corresponding last modified date.
478      Second element of the structure contains:
479      The sum of the sizes of all entries in the hierarchy at or below the
480      directory at the specified level.  */
481   static struct dulevel *dulvl;
482   bool print = true;
483
484   const char *file = ent->fts_path;
485   const struct stat *sb = ent->fts_statp;
486   bool skip;
487
488   /* If necessary, set FTS_SKIP before returning.  */
489   skip = excluded_file_name (exclude, ent->fts_path);
490   if (skip)
491     fts_set (fts, ent, FTS_SKIP);
492
493   switch (ent->fts_info)
494     {
495     case FTS_NS:
496       error (0, ent->fts_errno, _("cannot access %s"), quote (file));
497       return false;
498
499     case FTS_ERR:
500       /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
501       error (0, ent->fts_errno, _("%s"), quote (file));
502       return false;
503
504     case FTS_DNR:
505       /* Don't return just yet, since although the directory is not readable,
506          we were able to stat it, so we do have a size.  */
507       error (0, ent->fts_errno, _("cannot read directory %s"), quote (file));
508       ok = false;
509       break;
510
511     default:
512       ok = true;
513       break;
514     }
515
516   /* If this is the first (pre-order) encounter with a directory,
517      or if it's the second encounter for a skipped directory, then
518      return right away.  */
519   if (ent->fts_info == FTS_D || skip)
520     return ok;
521
522   /* If the file is being excluded or if it has already been counted
523      via a hard link, then don't let it contribute to the sums.  */
524   if (skip
525       || (!opt_count_all
526           && ! S_ISDIR (sb->st_mode)
527           && 1 < sb->st_nlink
528           && ! hash_ins (sb->st_ino, sb->st_dev)))
529     {
530       /* Note that we must not simply return here.
531          We still have to update prev_level and maybe propagate
532          some sums up the hierarchy.  */
533       duinfo_init (&dui);
534       print = false;
535     }
536   else
537     {
538       duinfo_set (&dui,
539                   (apparent_size
540                    ? sb->st_size
541                    : ST_NBLOCKS (*sb) * ST_NBLOCKSIZE),
542                   (time_type == time_ctime ? sb->st_ctime
543                    : time_type == time_atime ? sb->st_atime
544                    : sb->st_mtime),
545                   (time_type == time_ctime ? TIMESPEC_NS (sb->st_ctim)
546                    : time_type == time_atime ? TIMESPEC_NS (sb->st_atim)
547                    : TIMESPEC_NS (sb->st_mtim)));
548     }
549
550   level = ent->fts_level;
551   dui_to_print = dui;
552
553   if (n_alloc == 0)
554     {
555       n_alloc = level + 10;
556       dulvl = xcalloc (n_alloc, sizeof *dulvl);
557     }
558   else
559     {
560       if (level == prev_level)
561         {
562           /* This is usually the most common case.  Do nothing.  */
563         }
564       else if (level > prev_level)
565         {
566           /* Descending the hierarchy.
567              Clear the accumulators for *all* levels between prev_level
568              and the current one.  The depth may change dramatically,
569              e.g., from 1 to 10.  */
570           size_t i;
571
572           if (n_alloc <= level)
573             {
574               dulvl = xnrealloc (dulvl, level, 2 * sizeof *dulvl);
575               n_alloc = level * 2;
576             }
577
578           for (i = prev_level + 1; i <= level; i++)
579             {
580               duinfo_init (&dulvl[i].ent);
581               duinfo_init (&dulvl[i].subdir);
582             }
583         }
584       else /* level < prev_level */
585         {
586           /* Ascending the hierarchy.
587              Process a directory only after all entries in that
588              directory have been processed.  When the depth decreases,
589              propagate sums from the children (prev_level) to the parent.
590              Here, the current level is always one smaller than the
591              previous one.  */
592           assert (level == prev_level - 1);
593           duinfo_add (&dui_to_print, &dulvl[prev_level].ent);
594           if (!opt_separate_dirs)
595             duinfo_add (&dui_to_print, &dulvl[prev_level].subdir);
596           duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].ent);
597           duinfo_add (&dulvl[level].subdir, &dulvl[prev_level].subdir);
598         }
599     }
600
601   prev_level = level;
602
603   /* Let the size of a directory entry contribute to the total for the
604      containing directory, unless --separate-dirs (-S) is specified.  */
605   if ( ! (opt_separate_dirs && IS_DIR_TYPE (ent->fts_info)))
606     duinfo_add (&dulvl[level].ent, &dui);
607
608   /* Even if this directory is unreadable or we can't chdir into it,
609      do let its size contribute to the total, ... */
610   duinfo_add (&tot_dui, &dui);
611
612   /* ... but don't print out a total for it, since without the size(s)
613      of any potential entries, it could be very misleading.  */
614   if (ent->fts_info == FTS_DNR)
615     return ok;
616
617   /* If we're not counting an entry, e.g., because it's a hard link
618      to a file we've already counted (and --count-links), then don't
619      print a line for it.  */
620   if (!print)
621     return ok;
622
623   if ((IS_DIR_TYPE (ent->fts_info) && level <= max_depth)
624       || ((opt_all && level <= max_depth) || level == 0))
625     print_size (&dui_to_print, file);
626
627   return ok;
628 }
629
630 /* Recursively print the sizes of the directories (and, if selected, files)
631    named in FILES, the last entry of which is NULL.
632    BIT_FLAGS controls how fts works.
633    Return true if successful.  */
634
635 static bool
636 du_files (char **files, int bit_flags)
637 {
638   bool ok = true;
639
640   if (*files)
641     {
642       FTS *fts = xfts_open (files, bit_flags, NULL);
643
644       while (1)
645         {
646           FTSENT *ent;
647
648           ent = fts_read (fts);
649           if (ent == NULL)
650             {
651               if (errno != 0)
652                 {
653                   /* FIXME: try to give a better message  */
654                   error (0, errno, _("fts_read failed"));
655                   ok = false;
656                 }
657               break;
658             }
659           FTS_CROSS_CHECK (fts);
660
661           ok &= process_file (fts, ent);
662         }
663
664       /* Ignore failure, since the only way it can do so is in failing to
665          return to the original directory, and since we're about to exit,
666          that doesn't matter.  */
667       fts_close (fts);
668     }
669
670   if (print_grand_total)
671     print_size (&tot_dui, _("total"));
672
673   return ok;
674 }
675
676 int
677 main (int argc, char **argv)
678 {
679   int c;
680   char *cwd_only[2];
681   bool max_depth_specified = false;
682   char **files;
683   bool ok = true;
684   char *files_from = NULL;
685   struct Tokens tok;
686
687   /* Bit flags that control how fts works.  */
688   int bit_flags = FTS_PHYSICAL | FTS_TIGHT_CYCLE_CHECK;
689
690   /* If true, display only a total for each argument. */
691   bool opt_summarize_only = false;
692
693   cwd_only[0] = ".";
694   cwd_only[1] = NULL;
695
696   initialize_main (&argc, &argv);
697   program_name = argv[0];
698   setlocale (LC_ALL, "");
699   bindtextdomain (PACKAGE, LOCALEDIR);
700   textdomain (PACKAGE);
701
702   atexit (close_stdout);
703
704   exclude = new_exclude ();
705
706   human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
707                                      &output_block_size);
708
709   while ((c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
710                            long_options, NULL)) != -1)
711     {
712       switch (c)
713         {
714 #if DU_DEBUG
715         case 'd':
716           fts_debug = true;
717           break;
718 #endif
719
720         case '0':
721           opt_nul_terminate_output = true;
722           break;
723
724         case 'a':
725           opt_all = true;
726           break;
727
728         case APPARENT_SIZE_OPTION:
729           apparent_size = true;
730           break;
731
732         case 'b':
733           apparent_size = true;
734           human_output_opts = 0;
735           output_block_size = 1;
736           break;
737
738         case 'c':
739           print_grand_total = true;
740           break;
741
742         case 'h':
743           human_output_opts = human_autoscale | human_SI | human_base_1024;
744           output_block_size = 1;
745           break;
746
747         case 'H':
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;
755
756         case 'k':
757           human_output_opts = 0;
758           output_block_size = 1024;
759           break;
760
761         case MAX_DEPTH_OPTION:          /* --max-depth=N */
762           {
763             unsigned long int tmp_ulong;
764             if (xstrtoul (optarg, NULL, 0, &tmp_ulong, NULL) == LONGINT_OK
765                 && tmp_ulong <= SIZE_MAX)
766               {
767                 max_depth_specified = true;
768                 max_depth = tmp_ulong;
769               }
770             else
771               {
772                 error (0, 0, _("invalid maximum depth %s"),
773                        quote (optarg));
774                 ok = false;
775               }
776           }
777           break;
778
779         case 'm': /* obsolescent: FIXME: remove in 2005. */
780           human_output_opts = 0;
781           output_block_size = 1024 * 1024;
782           break;
783
784         case 'l':
785           opt_count_all = true;
786           break;
787
788         case 's':
789           opt_summarize_only = true;
790           break;
791
792         case 'x':
793           bit_flags |= FTS_XDEV;
794           break;
795
796         case 'B':
797           human_output_opts = human_options (optarg, true, &output_block_size);
798           break;
799
800         case 'D': /* This will eventually be 'H' (-H), too.  */
801           bit_flags = FTS_COMFOLLOW;
802           break;
803
804         case 'L': /* --dereference */
805           bit_flags = FTS_LOGICAL;
806           break;
807
808         case 'P': /* --no-dereference */
809           bit_flags = FTS_PHYSICAL;
810           break;
811
812         case 'S':
813           opt_separate_dirs = true;
814           break;
815
816         case 'X':
817           if (add_exclude_file (add_exclude, exclude, optarg,
818                                 EXCLUDE_WILDCARDS, '\n'))
819             {
820               error (0, errno, "%s", quotearg_colon (optarg));
821               ok = false;
822             }
823           break;
824
825         case FILES0_FROM_OPTION:
826           files_from = optarg;
827           break;
828
829         case EXCLUDE_OPTION:
830           add_exclude (exclude, optarg, EXCLUDE_WILDCARDS);
831           break;
832
833         case TIME_OPTION:
834           opt_time = true;
835           time_type =
836             (optarg
837              ? XARGMATCH ("--time", optarg, time_args, time_types)
838              : time_mtime);
839           break;
840
841         case TIME_STYLE_OPTION:
842           time_style = optarg;
843           break;
844
845         case_GETOPT_HELP_CHAR;
846
847         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
848
849         default:
850           ok = false;
851         }
852     }
853
854   if (!ok)
855     usage (EXIT_FAILURE);
856
857   if (opt_all & opt_summarize_only)
858     {
859       error (0, 0, _("cannot both summarize and show all entries"));
860       usage (EXIT_FAILURE);
861     }
862
863   if (opt_summarize_only && max_depth_specified && max_depth == 0)
864     {
865       error (0, 0,
866              _("warning: summarizing is the same as using --max-depth=0"));
867     }
868
869   if (opt_summarize_only && max_depth_specified && max_depth != 0)
870     {
871       unsigned long int d = max_depth;
872       error (0, 0, _("warning: summarizing conflicts with --max-depth=%lu"), d);
873       usage (EXIT_FAILURE);
874     }
875
876   if (opt_summarize_only)
877     max_depth = 0;
878
879   /* Process time style if printing last times.  */
880   if (opt_time)
881     {
882       if (! time_style)
883         {
884           time_style = getenv ("TIME_STYLE");
885
886           /* Ignore TIMESTYLE="locale", for compatibility with ls.  */
887           if (! time_style || STREQ (time_style, "locale"))
888             time_style = "long-iso";
889           else if (*time_style == '+')
890             {
891               /* Ignore anything after a newline, for compatibility
892                  with ls.  */
893               char *p = strchr (time_style, '\n');
894               if (p)
895                 *p = '\0';
896             }
897           else
898             {
899               /* Ignore "posix-" prefix, for compatibility with ls.  */
900               static char const posix_prefix[] = "posix-";
901               while (strncmp (time_style, posix_prefix, sizeof posix_prefix - 1)
902                      == 0)
903                 time_style += sizeof posix_prefix - 1;
904             }
905         }
906
907       if (*time_style == '+')
908         time_format = time_style + 1;
909       else
910         {
911           switch (XARGMATCH ("time style", time_style,
912                              time_style_args, time_style_types))
913             {
914             case full_iso_time_style:
915               time_format = "%Y-%m-%d %H:%M:%S.%N %z";
916               break;
917
918             case long_iso_time_style:
919               time_format = "%Y-%m-%d %H:%M";
920               break;
921
922             case iso_time_style:
923               time_format = "%Y-%m-%d";
924               break;
925             }
926         }
927     }
928
929   if (files_from)
930     {
931       /* When using --files0-from=F, you may not specify any files
932          on the command-line.  */
933       if (optind < argc)
934         {
935           error (0, 0, _("extra operand %s"), quote (argv[optind]));
936           fprintf (stderr, "%s\n",
937                    _("File operands cannot be combined with --files0-from."));
938           usage (EXIT_FAILURE);
939         }
940
941       if (! (STREQ (files_from, "-") || freopen (files_from, "r", stdin)))
942         error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
943                quote (files_from));
944
945       readtokens0_init (&tok);
946
947       if (! readtokens0 (stdin, &tok) || fclose (stdin) != 0)
948         error (EXIT_FAILURE, 0, _("cannot read file names from %s"),
949                quote (files_from));
950
951       files = tok.tok;
952     }
953   else
954     {
955       files = (optind < argc ? argv + optind : cwd_only);
956     }
957
958   /* Initialize the hash structure for inode numbers.  */
959   hash_init ();
960
961   /* Report and filter out any empty file names before invoking fts.
962      This works around a glitch in fts, which fails immediately
963      (without looking at the other file names) when given an empty
964      file name.  */
965   {
966     size_t i = 0;
967     size_t j;
968
969     for (j = 0; ; j++)
970       {
971         if (i != j)
972           files[i] = files[j];
973
974         if ( ! files[i])
975           break;
976
977         if (files[i][0])
978           i++;
979         else
980           {
981             if (files_from)
982               {
983                 /* Using the standard `filename:line-number:' prefix here is
984                    not totally appropriate, since NUL is the separator, not NL,
985                    but it might be better than nothing.  */
986                 unsigned long int file_number = j + 1;
987                 error (0, 0, "%s:%lu: %s", quotearg_colon (files_from),
988                        file_number, _("invalid zero-length file name"));
989               }
990             else
991               error (0, 0, "%s", _("invalid zero-length file name"));
992           }
993       }
994
995     ok = (i == j);
996   }
997
998   ok &= du_files (files, bit_flags);
999
1000   /* This isn't really necessary, but it does ensure we
1001      exercise this function.  */
1002   if (files_from)
1003     readtokens0_free (&tok);
1004
1005   hash_free (htab);
1006
1007   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
1008 }