1 /* du -- summarize disk usage
2 Copyright (C) 1988-2013 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Differences from the Unix du:
18 * Doesn't simply ignore the names of regular files given as arguments
21 By tege@sics.se, Torbjorn Granlund,
22 and djm@ai.mit.edu, David MacKenzie.
23 Variable blocks added by lm@sgi.com and eggert@twinsun.com.
24 Rewritten to use nftw, then to use fts by Jim Meyering. */
28 #include <sys/types.h>
32 #include "argv-iter.h"
36 #include "fprintftime.h"
38 #include "mountlist.h"
41 #include "stat-size.h"
42 #include "stat-time.h"
47 extern bool fts_debug
;
49 /* The official name of this program (e.g., no 'g' prefix). */
50 #define PROGRAM_NAME "du"
53 proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
54 proper_name ("David MacKenzie"), \
55 proper_name ("Paul Eggert"), \
56 proper_name ("Jim Meyering")
59 # define FTS_CROSS_CHECK(Fts) fts_cross_check (Fts)
61 # define FTS_CROSS_CHECK(Fts)
64 /* A set of dev/ino pairs to help identify files and directories
65 whose sizes have already been counted. */
66 static struct di_set
*di_files
;
68 /* A set containing a dev/ino pair for each local mount point directory. */
69 static struct di_set
*di_mnt
;
71 /* Keep track of the preceding "level" (depth in hierarchy)
72 from one call of process_file to the next. */
73 static size_t prev_level
;
75 /* Define a class for collecting directory information. */
78 /* Size of files in directory. */
81 /* Latest time stamp found. If tmax.tv_sec == TYPE_MINIMUM (time_t)
82 && tmax.tv_nsec < 0, no time stamp has been found. */
86 /* Initialize directory data. */
88 duinfo_init (struct duinfo
*a
)
91 a
->tmax
.tv_sec
= TYPE_MINIMUM (time_t);
95 /* Set directory data. */
97 duinfo_set (struct duinfo
*a
, uintmax_t size
, struct timespec tmax
)
103 /* Accumulate directory data. */
105 duinfo_add (struct duinfo
*a
, struct duinfo
const *b
)
107 uintmax_t sum
= a
->size
+ b
->size
;
108 a
->size
= a
->size
<= sum
? sum
: UINTMAX_MAX
;
109 if (timespec_cmp (a
->tmax
, b
->tmax
) < 0)
113 /* A structure for per-directory level information. */
116 /* Entries in this directory. */
119 /* Total for subdirectories. */
120 struct duinfo subdir
;
123 /* If true, display counts for all files, not just directories. */
124 static bool opt_all
= false;
126 /* If true, rather than using the disk usage of each file,
127 use the apparent size (a la stat.st_size). */
128 static bool apparent_size
= false;
130 /* If true, count each hard link of files with multiple links. */
131 static bool opt_count_all
= false;
133 /* If true, hash all files to look for hard links. */
134 static bool hash_all
;
136 /* If true, output the NUL byte instead of a newline at the end of each line. */
137 static bool opt_nul_terminate_output
= false;
139 /* If true, print a grand total at the end. */
140 static bool print_grand_total
= false;
142 /* If nonzero, do not add sizes of subdirectories. */
143 static bool opt_separate_dirs
= false;
145 /* Show the total for each directory (and file if --all) that is at
146 most MAX_DEPTH levels down from the root of the hierarchy. The root
147 is at level 0, so 'du --max-depth=0' is equivalent to 'du -s'. */
148 static size_t max_depth
= SIZE_MAX
;
150 /* Only output entries with at least this SIZE if positive,
151 or at most if negative. See --threshold option. */
152 static intmax_t opt_threshold
= 0;
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. */
164 time_mtime
, /* default */
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) \
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. */
194 APPARENT_SIZE_OPTION
= CHAR_MAX
+ 1,
203 static struct option
const long_options
[] =
205 {"all", no_argument
, NULL
, 'a'},
206 {"apparent-size", no_argument
, NULL
, APPARENT_SIZE_OPTION
},
207 {"block-size", required_argument
, NULL
, 'B'},
208 {"bytes", no_argument
, NULL
, 'b'},
209 {"count-links", no_argument
, NULL
, 'l'},
210 /* {"-debug", no_argument, NULL, FTS_DEBUG}, */
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 {"max-depth", required_argument
, NULL
, 'd'},
219 {"null", no_argument
, NULL
, '0'},
220 {"no-dereference", no_argument
, NULL
, 'P'},
221 {"one-file-system", no_argument
, NULL
, 'x'},
222 {"separate-dirs", no_argument
, NULL
, 'S'},
223 {"summarize", no_argument
, NULL
, 's'},
224 {"total", no_argument
, NULL
, 'c'},
225 {"threshold", required_argument
, NULL
, 't'},
226 {"time", optional_argument
, NULL
, TIME_OPTION
},
227 {"time-style", required_argument
, NULL
, TIME_STYLE_OPTION
},
228 {GETOPT_HELP_OPTION_DECL
},
229 {GETOPT_VERSION_OPTION_DECL
},
233 static char const *const time_args
[] =
235 "atime", "access", "use", "ctime", "status", NULL
237 static enum time_type
const time_types
[] =
239 time_atime
, time_atime
, time_atime
, time_ctime
, time_ctime
241 ARGMATCH_VERIFY (time_args
, time_types
);
243 /* 'full-iso' uses full ISO-style dates and times. 'long-iso' uses longer
244 ISO-style time stamps, though shorter than 'full-iso'. 'iso' uses shorter
245 ISO-style time stamps. */
248 full_iso_time_style
, /* --time-style=full-iso */
249 long_iso_time_style
, /* --time-style=long-iso */
250 iso_time_style
/* --time-style=iso */
253 static char const *const time_style_args
[] =
255 "full-iso", "long-iso", "iso", NULL
257 static enum time_style
const time_style_types
[] =
259 full_iso_time_style
, long_iso_time_style
, iso_time_style
261 ARGMATCH_VERIFY (time_style_args
, time_style_types
);
266 if (status
!= EXIT_SUCCESS
)
271 Usage: %s [OPTION]... [FILE]...\n\
272 or: %s [OPTION]... --files0-from=F\n\
273 "), program_name
, program_name
);
275 Summarize disk usage of each FILE, recursively for directories.\n\
278 emit_mandatory_arg_note ();
281 -0, --null end each output line with 0 byte rather than newline\n\
282 -a, --all write counts for all files, not just directories\n\
283 --apparent-size print apparent sizes, rather than disk usage; although\
285 the apparent size is usually smaller, it may be\n\
286 larger due to holes in ('sparse') files, internal\n\
287 fragmentation, indirect blocks, and the like\n\
290 -B, --block-size=SIZE scale sizes by SIZE before printing them. E.g.,\n\
291 '-BM' prints sizes in units of 1,048,576 bytes.\n\
292 See SIZE format below.\n\
293 -b, --bytes equivalent to '--apparent-size --block-size=1'\n\
294 -c, --total produce a grand total\n\
295 -D, --dereference-args dereference only symlinks that are listed on the\n\
297 -d, --max-depth=N print the total for a directory (or file, with --all)\n\
298 only if it is N or fewer levels below the command\n\
299 line argument; --max-depth=0 is the same as\n\
303 --files0-from=F summarize disk usage of the NUL-terminated file\n\
304 names specified in file F;\n\
305 If F is - then read names from standard input\n\
306 -H equivalent to --dereference-args (-D)\n\
307 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\
311 -k like --block-size=1K\n\
312 -L, --dereference dereference all symbolic links\n\
313 -l, --count-links count sizes many times if hard linked\n\
314 -m like --block-size=1M\n\
317 -P, --no-dereference don't follow any symbolic links (this is the default)\n\
318 -S, --separate-dirs for directories do not include size of subdirectories\n\
319 --si like -h, but use powers of 1000 not 1024\n\
320 -s, --summarize display only a total for each argument\n\
323 -t, --threshold=SIZE exclude entries smaller than SIZE if positive,\n\
324 or entries greater than SIZE if negative\n\
325 --time show time of the last modification of any file in the\n\
326 directory, or any of its subdirectories\n\
327 --time=WORD show time as WORD instead of modification time:\n\
328 atime, access, use, ctime or status\n\
329 --time-style=STYLE show times using style STYLE:\n\
330 full-iso, long-iso, iso, +FORMAT\n\
331 FORMAT is interpreted like 'date'\n\
334 -X, --exclude-from=FILE exclude files that match any pattern in FILE\n\
335 --exclude=PATTERN exclude files that match PATTERN\n\
336 -x, --one-file-system skip directories on different file systems\n\
338 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
339 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
340 emit_blocksize_note ("DU");
342 emit_ancillary_info ();
347 /* Try to insert the INO/DEV pair into DI_SET.
348 Return true if the pair is successfully inserted,
349 false if the pair was already there. */
351 hash_ins (struct di_set
*di_set
, ino_t ino
, dev_t dev
)
353 int inserted
= di_set_insert (di_set
, dev
, ino
);
359 /* FIXME: this code is nearly identical to code in date.c */
360 /* Display the date and time in WHEN according to the format specified
364 show_date (const char *format
, struct timespec when
)
366 struct tm
*tm
= localtime (&when
.tv_sec
);
369 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
370 char *when_str
= timetostr (when
.tv_sec
, buf
);
371 error (0, 0, _("time %s is out of range"), when_str
);
372 fputs (when_str
, stdout
);
376 fprintftime (stdout
, format
, tm
, 0, when
.tv_nsec
);
379 /* Print N_BYTES. Convert it to a readable value before printing. */
382 print_only_size (uintmax_t n_bytes
)
384 char buf
[LONGEST_HUMAN_READABLE
+ 1];
385 fputs ((n_bytes
== UINTMAX_MAX
387 : human_readable (n_bytes
, buf
, human_output_opts
,
388 1, output_block_size
)),
392 /* Print size (and optionally time) indicated by *PDUI, followed by STRING. */
395 print_size (const struct duinfo
*pdui
, const char *string
)
397 print_only_size (pdui
->size
);
401 show_date (time_format
, pdui
->tmax
);
403 printf ("\t%s%c", string
, opt_nul_terminate_output
? '\0' : '\n');
407 /* This function is called once for every file system object that fts
408 encounters. fts does a depth-first traversal. This function knows
409 that and accumulates per-directory totals based on changes in
410 the depth of the current entry. It returns true on success. */
413 process_file (FTS
*fts
, FTSENT
*ent
)
417 struct duinfo dui_to_print
;
419 static size_t n_alloc
;
420 /* First element of the structure contains:
421 The sum of the st_size values of all entries in the single directory
422 at the corresponding level. Although this does include the st_size
423 corresponding to each subdirectory, it does not include the size of
424 any file in a subdirectory. Also corresponding last modified date.
425 Second element of the structure contains:
426 The sum of the sizes of all entries in the hierarchy at or below the
427 directory at the specified level. */
428 static struct dulevel
*dulvl
;
430 const char *file
= ent
->fts_path
;
431 const struct stat
*sb
= ent
->fts_statp
;
432 int info
= ent
->fts_info
;
436 /* An error occurred, but the size is known, so count it. */
437 error (0, ent
->fts_errno
, _("cannot read directory %s"), quote (file
));
440 else if (info
!= FTS_DP
)
442 bool excluded
= excluded_file_name (exclude
, file
);
445 /* Make the stat buffer *SB valid, or fail noisily. */
447 if (info
== FTS_NSOK
)
449 fts_set (fts
, ent
, FTS_AGAIN
);
450 FTSENT
const *e
= fts_read (fts
);
452 info
= ent
->fts_info
;
455 if (info
== FTS_NS
|| info
== FTS_SLNONE
)
457 error (0, ent
->fts_errno
, _("cannot access %s"), quote (file
));
461 /* The --one-file-system (-x) option cannot exclude anything
462 specified on the command-line. By definition, it can exclude
463 a file or directory only when its device number is different
464 from that of its just-processed parent directory, and du does
465 not process the parent of a command-line argument. */
466 if (fts
->fts_options
& FTS_XDEV
467 && FTS_ROOTLEVEL
< ent
->fts_level
468 && fts
->fts_dev
!= sb
->st_dev
)
474 && (hash_all
|| (! S_ISDIR (sb
->st_mode
) && 1 < sb
->st_nlink
))
475 && ! hash_ins (di_files
, sb
->st_ino
, sb
->st_dev
)))
477 /* If ignoring a directory in preorder, skip its children.
478 Ignore the next fts_read output too, as it's a postorder
479 visit to the same directory. */
482 fts_set (fts
, ent
, FTS_SKIP
);
483 FTSENT
const *e
= fts_read (fts
);
496 /* An error occurred, but the size is known, so count it. */
497 error (0, ent
->fts_errno
, "%s", quote (file
));
502 if (cycle_warning_required (fts
, ent
))
504 /* If this is a mount point, then diagnose it and avoid
506 if (di_set_lookup (di_mnt
, sb
->st_dev
, sb
->st_ino
))
507 error (0, 0, _("mount point %s already traversed"),
510 emit_cycle_warning (file
);
519 ? MAX (0, sb
->st_size
)
520 : (uintmax_t) ST_NBLOCKS (*sb
) * ST_NBLOCKSIZE
),
521 (time_type
== time_mtime
? get_stat_mtime (sb
)
522 : time_type
== time_atime
? get_stat_atime (sb
)
523 : get_stat_ctime (sb
)));
525 level
= ent
->fts_level
;
530 n_alloc
= level
+ 10;
531 dulvl
= xcalloc (n_alloc
, sizeof *dulvl
);
535 if (level
== prev_level
)
537 /* This is usually the most common case. Do nothing. */
539 else if (level
> prev_level
)
541 /* Descending the hierarchy.
542 Clear the accumulators for *all* levels between prev_level
543 and the current one. The depth may change dramatically,
544 e.g., from 1 to 10. */
547 if (n_alloc
<= level
)
549 dulvl
= xnrealloc (dulvl
, level
, 2 * sizeof *dulvl
);
553 for (i
= prev_level
+ 1; i
<= level
; i
++)
555 duinfo_init (&dulvl
[i
].ent
);
556 duinfo_init (&dulvl
[i
].subdir
);
559 else /* level < prev_level */
561 /* Ascending the hierarchy.
562 Process a directory only after all entries in that
563 directory have been processed. When the depth decreases,
564 propagate sums from the children (prev_level) to the parent.
565 Here, the current level is always one smaller than the
567 assert (level
== prev_level
- 1);
568 duinfo_add (&dui_to_print
, &dulvl
[prev_level
].ent
);
569 if (!opt_separate_dirs
)
570 duinfo_add (&dui_to_print
, &dulvl
[prev_level
].subdir
);
571 duinfo_add (&dulvl
[level
].subdir
, &dulvl
[prev_level
].ent
);
572 duinfo_add (&dulvl
[level
].subdir
, &dulvl
[prev_level
].subdir
);
578 /* Let the size of a directory entry contribute to the total for the
579 containing directory, unless --separate-dirs (-S) is specified. */
580 if (! (opt_separate_dirs
&& IS_DIR_TYPE (info
)))
581 duinfo_add (&dulvl
[level
].ent
, &dui
);
583 /* Even if this directory is unreadable or we can't chdir into it,
584 do let its size contribute to the total. */
585 duinfo_add (&tot_dui
, &dui
);
587 if ((IS_DIR_TYPE (info
) && level
<= max_depth
)
588 || (opt_all
&& level
<= max_depth
)
591 /* Print or elide this entry according to the --threshold option. */
592 if (opt_threshold
< 0
593 ? dui_to_print
.size
<= -opt_threshold
594 : dui_to_print
.size
>= opt_threshold
)
595 print_size (&dui_to_print
, file
);
601 /* Recursively print the sizes of the directories (and, if selected, files)
602 named in FILES, the last entry of which is NULL.
603 BIT_FLAGS controls how fts works.
604 Return true if successful. */
607 du_files (char **files
, int bit_flags
)
613 FTS
*fts
= xfts_open (files
, bit_flags
, NULL
);
619 ent
= fts_read (fts
);
624 error (0, errno
, _("fts_read failed: %s"),
625 quotearg_colon (fts
->fts_path
));
629 /* When exiting this loop early, be careful to reset the
630 global, prev_level, used in process_file. Otherwise, its
631 (level == prev_level - 1) assertion could fail. */
635 FTS_CROSS_CHECK (fts
);
637 ok
&= process_file (fts
, ent
);
640 if (fts_close (fts
) != 0)
642 error (0, errno
, _("fts_close failed"));
650 /* Fill the di_mnt set with local mount point dev/ino pairs. */
653 fill_mount_table (void)
655 struct mount_entry
*mnt_ent
= read_file_system_list (false);
658 struct mount_entry
*mnt_free
;
659 if (!mnt_ent
->me_remote
&& !mnt_ent
->me_dummy
)
662 if (!stat (mnt_ent
->me_mountdir
, &buf
))
663 hash_ins (di_mnt
, buf
.st_ino
, buf
.st_dev
);
666 /* Ignore stat failure. False positives are too common.
667 E.g., "Permission denied" on /run/user/<name>/gvfs. */
672 mnt_ent
= mnt_ent
->me_next
;
674 free (mnt_free
->me_devname
);
675 free (mnt_free
->me_mountdir
);
676 if (mnt_free
->me_type_malloced
)
677 free (mnt_free
->me_type
);
683 main (int argc
, char **argv
)
686 bool max_depth_specified
= false;
688 char *files_from
= NULL
;
690 /* Bit flags that control how fts works. */
691 int bit_flags
= FTS_NOSTAT
;
693 /* Select one of the three FTS_ options that control if/when
694 to follow a symlink. */
695 int symlink_deref_bits
= FTS_PHYSICAL
;
697 /* If true, display only a total for each argument. */
698 bool opt_summarize_only
= false;
700 cwd_only
[0] = bad_cast (".");
703 initialize_main (&argc
, &argv
);
704 set_program_name (argv
[0]);
705 setlocale (LC_ALL
, "");
706 bindtextdomain (PACKAGE
, LOCALEDIR
);
707 textdomain (PACKAGE
);
709 atexit (close_stdout
);
711 exclude
= new_exclude ();
713 human_options (getenv ("DU_BLOCK_SIZE"),
714 &human_output_opts
, &output_block_size
);
719 int c
= getopt_long (argc
, argv
, "0abd:chHklmst:xB:DLPSX:",
733 opt_nul_terminate_output
= true;
740 case APPARENT_SIZE_OPTION
:
741 apparent_size
= true;
745 apparent_size
= true;
746 human_output_opts
= 0;
747 output_block_size
= 1;
751 print_grand_total
= true;
755 human_output_opts
= human_autoscale
| human_SI
| human_base_1024
;
756 output_block_size
= 1;
759 case HUMAN_SI_OPTION
:
760 human_output_opts
= human_autoscale
| human_SI
;
761 output_block_size
= 1;
765 human_output_opts
= 0;
766 output_block_size
= 1024;
769 case 'd': /* --max-depth=N */
771 unsigned long int tmp_ulong
;
772 if (xstrtoul (optarg
, NULL
, 0, &tmp_ulong
, NULL
) == LONGINT_OK
773 && tmp_ulong
<= SIZE_MAX
)
775 max_depth_specified
= true;
776 max_depth
= tmp_ulong
;
780 error (0, 0, _("invalid maximum depth %s"),
788 human_output_opts
= 0;
789 output_block_size
= 1024 * 1024;
793 opt_count_all
= true;
797 opt_summarize_only
= true;
803 e
= xstrtoimax (optarg
, NULL
, 0, &opt_threshold
, "kKmMGTPEZY0");
805 xstrtol_fatal (e
, oi
, c
, long_options
, optarg
);
806 if (opt_threshold
== 0 && *optarg
== '-')
808 /* Do not allow -0, as this wouldn't make sense anyway. */
809 error (EXIT_FAILURE
, 0, _("invalid --threshold argument '-0'"));
815 bit_flags
|= FTS_XDEV
;
820 enum strtol_error e
= human_options (optarg
, &human_output_opts
,
823 xstrtol_fatal (e
, oi
, c
, long_options
, optarg
);
827 case 'H': /* NOTE: before 2008-12, -H was equivalent to --si. */
829 symlink_deref_bits
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
832 case 'L': /* --dereference */
833 symlink_deref_bits
= FTS_LOGICAL
;
836 case 'P': /* --no-dereference */
837 symlink_deref_bits
= FTS_PHYSICAL
;
841 opt_separate_dirs
= true;
845 if (add_exclude_file (add_exclude
, exclude
, optarg
,
846 EXCLUDE_WILDCARDS
, '\n'))
848 error (0, errno
, "%s", quotearg_colon (optarg
));
853 case FILES0_FROM_OPTION
:
858 add_exclude (exclude
, optarg
, EXCLUDE_WILDCARDS
);
865 ? XARGMATCH ("--time", optarg
, time_args
, time_types
)
869 case TIME_STYLE_OPTION
:
873 case_GETOPT_HELP_CHAR
;
875 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
883 usage (EXIT_FAILURE
);
885 if (opt_all
&& opt_summarize_only
)
887 error (0, 0, _("cannot both summarize and show all entries"));
888 usage (EXIT_FAILURE
);
891 if (opt_summarize_only
&& max_depth_specified
&& max_depth
== 0)
894 _("warning: summarizing is the same as using --max-depth=0"));
897 if (opt_summarize_only
&& max_depth_specified
&& max_depth
!= 0)
899 unsigned long int d
= max_depth
;
900 error (0, 0, _("warning: summarizing conflicts with --max-depth=%lu"), d
);
901 usage (EXIT_FAILURE
);
904 if (opt_summarize_only
)
907 /* Process time style if printing last times. */
912 time_style
= getenv ("TIME_STYLE");
914 /* Ignore TIMESTYLE="locale", for compatibility with ls. */
915 if (! time_style
|| STREQ (time_style
, "locale"))
916 time_style
= "long-iso";
917 else if (*time_style
== '+')
919 /* Ignore anything after a newline, for compatibility
921 char *p
= strchr (time_style
, '\n');
927 /* Ignore "posix-" prefix, for compatibility with ls. */
928 static char const posix_prefix
[] = "posix-";
929 while (strncmp (time_style
, posix_prefix
, sizeof posix_prefix
- 1)
931 time_style
+= sizeof posix_prefix
- 1;
935 if (*time_style
== '+')
936 time_format
= time_style
+ 1;
939 switch (XARGMATCH ("time style", time_style
,
940 time_style_args
, time_style_types
))
942 case full_iso_time_style
:
943 time_format
= "%Y-%m-%d %H:%M:%S.%N %z";
946 case long_iso_time_style
:
947 time_format
= "%Y-%m-%d %H:%M";
951 time_format
= "%Y-%m-%d";
957 struct argv_iterator
*ai
;
960 /* When using --files0-from=F, you may not specify any files
961 on the command-line. */
964 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
965 fprintf (stderr
, "%s\n",
966 _("file operands cannot be combined with --files0-from"));
967 usage (EXIT_FAILURE
);
970 if (! (STREQ (files_from
, "-") || freopen (files_from
, "r", stdin
)))
971 error (EXIT_FAILURE
, errno
, _("cannot open %s for reading"),
974 ai
= argv_iter_init_stream (stdin
);
976 /* It's not easy here to count the arguments, so assume the
982 char **files
= (optind
< argc
? argv
+ optind
: cwd_only
);
983 ai
= argv_iter_init_argv (files
);
985 /* Hash all dev,ino pairs if there are multiple arguments, or if
986 following non-command-line symlinks, because in either case a
987 file with just one hard link might be seen more than once. */
988 hash_all
= (optind
+ 1 < argc
|| symlink_deref_bits
== FTS_LOGICAL
);
994 /* Initialize the set of dev,inode pairs. */
996 di_mnt
= di_set_alloc ();
1000 fill_mount_table ();
1002 di_files
= di_set_alloc ();
1006 /* If not hashing everything, process_file won't find cycles on its
1007 own, so ask fts_read to check for them accurately. */
1008 if (opt_count_all
|| ! hash_all
)
1009 bit_flags
|= FTS_TIGHT_CYCLE_CHECK
;
1011 bit_flags
|= symlink_deref_bits
;
1012 static char *temp_argv
[] = { NULL
, NULL
};
1016 bool skip_file
= false;
1017 enum argv_iter_err ai_err
;
1018 char *file_name
= argv_iter (ai
, &ai_err
);
1024 goto argv_iter_done
;
1026 error (0, errno
, _("%s: read error"),
1027 quotearg_colon (files_from
));
1029 goto argv_iter_done
;
1033 assert (!"unexpected error code from argv_iter");
1036 if (files_from
&& STREQ (files_from
, "-") && STREQ (file_name
, "-"))
1038 /* Give a better diagnostic in an unusual case:
1039 printf - | du --files0-from=- */
1040 error (0, 0, _("when reading file names from stdin, "
1041 "no file name of %s allowed"),
1046 /* Report and skip any empty file names before invoking fts.
1047 This works around a glitch in fts, which fails immediately
1048 (without looking at the other file names) when given an empty
1052 /* Diagnose a zero-length file name. When it's one
1053 among many, knowing the record number may help.
1054 FIXME: currently print the record number only with
1055 --files0-from=FILE. Maybe do it for argv, too? */
1056 if (files_from
== NULL
)
1057 error (0, 0, "%s", _("invalid zero-length file name"));
1060 /* Using the standard 'filename:line-number:' prefix here is
1061 not totally appropriate, since NUL is the separator, not NL,
1062 but it might be better than nothing. */
1063 unsigned long int file_number
= argv_iter_n_args (ai
);
1064 error (0, 0, "%s:%lu: %s", quotearg_colon (files_from
),
1065 file_number
, _("invalid zero-length file name"));
1074 temp_argv
[0] = file_name
;
1075 ok
&= du_files (temp_argv
, bit_flags
);
1080 argv_iter_free (ai
);
1081 di_set_free (di_files
);
1082 di_set_free (di_mnt
);
1084 if (files_from
&& (ferror (stdin
) || fclose (stdin
) != 0) && ok
)
1085 error (EXIT_FAILURE
, 0, _("error reading %s"), quote (files_from
));
1087 if (print_grand_total
)
1088 print_size (&tot_dui
, _("total"));
1090 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);