1 /* du -- summarize disk usage
2 Copyright (C) 1988, 1989, 1990, 1991 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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Differences from the Unix du:
19 * Doesn't simply ignore the names of regular files given as arguments
22 -l Count the size of all files, even if they have appeared
23 already in another hard link.
24 -x Do not cross file-system boundaries during the recursion.
25 -c Write a grand total of all of the arguments after all
26 arguments have been processed. This can be used to find
27 out the disk usage of a directory, with some files excluded.
28 -k Print sizes in kilobytes instead of 512 byte blocks
29 (the default required by POSIX).
30 -b Print sizes in bytes.
31 -S Count the size of each directory separately, not including
32 the sizes of subdirectories.
33 -D Dereference only symbolic links given on the command line.
34 -L Dereference all symbolic links.
36 By tege@sics.se, Torbjorn Granlund,
37 and djm@ai.mit.edu, David MacKenzie. */
46 #include <sys/types.h>
49 #include "safe-stat.h"
50 #include "safe-lstat.h"
52 /* Initial number of entries in each hash table entry's table of inodes. */
53 #define INITIAL_HASH_MODULE 100
55 /* Initial number of entries in the inode hash table. */
56 #define INITIAL_ENTRY_TAB_SIZE 70
58 /* Initial size to allocate for `path'. */
59 #define INITIAL_PATH_SIZE 100
61 /* Hash structure for inode and device numbers. The separate entry
62 structure makes it easier to rehash "in place". */
68 struct entry *coll_link;
71 /* Structure for a hash table for inode numbers. */
75 unsigned modulus; /* Size of the `hash' pointer vector. */
76 struct entry *entry_tab; /* Pointer to dynamically growing vector. */
77 unsigned entry_tab_size; /* Size of current `entry_tab' allocation. */
78 unsigned first_free_entry; /* Index in `entry_tab'. */
79 struct entry *hash[1]; /* Vector of pointers in `entry_tab'. */
83 /* Structure for dynamically resizable strings. */
87 unsigned alloc; /* Size of allocation for the text. */
88 unsigned length; /* Length of the text currently. */
89 char *text; /* Pointer to the text. */
90 } *string, stringstruct;
100 static int hash_insert ();
101 static int hash_insert2 ();
102 static long count_entry ();
103 static void du_files ();
104 static void hash_init ();
105 static void hash_reset ();
106 static void str_concatc ();
107 static void str_copyc ();
108 static void str_init ();
109 static void str_trunc ();
111 /* Name under which this program was invoked. */
114 /* If nonzero, display only a total for each argument. */
115 static int opt_summarize_only = 0;
117 /* If nonzero, display counts for all files, not just directories. */
118 static int opt_all = 0;
120 /* If nonzero, count each hard link of files with multiple links. */
121 static int opt_count_all = 0;
123 /* If nonzero, do not cross file-system boundaries. */
124 static int opt_one_file_system = 0;
126 /* If nonzero, print a grand total at the end. */
127 static int opt_combined_arguments = 0;
129 /* If nonzero, do not add sizes of subdirectories. */
130 static int opt_separate_dirs = 0;
132 /* If nonzero, dereference symlinks that are command line arguments. */
133 static int opt_dereference_arguments = 0;
137 size_blocks, /* 512-byte blocks. */
138 size_kilobytes, /* 1K blocks. */
139 size_bytes /* 1-byte blocks. */
142 /* The units to count in. */
143 static enum output_size output_size;
145 /* Accumulated path for file or directory being processed. */
148 /* Pointer to hash structure, used by the hash routines. */
149 static struct htab *htab;
151 /* Globally used stat buffer. */
152 static struct stat stat_buf;
154 /* A pointer to either lstat or stat, depending on whether
155 dereferencing of all symbolic links is to be done. */
156 static int (*xstat) ();
158 /* The exit status to use if we don't get any fatal errors. */
159 static int exit_status;
161 /* If non-zero, display usage information and exit. */
162 static int show_help;
164 /* If non-zero, print the version on standard output and exit. */
165 static int show_version;
167 /* Grand total size of all args. */
168 static long tot_size = 0L;
170 static struct option const long_options[] =
172 {"all", no_argument, &opt_all, 1},
173 {"bytes", no_argument, NULL, 'b'},
174 {"count-links", no_argument, &opt_count_all, 1},
175 {"dereference", no_argument, NULL, 'L'},
176 {"dereference-args", no_argument, &opt_dereference_arguments, 1},
177 {"kilobytes", no_argument, NULL, 'k'},
178 {"one-file-system", no_argument, &opt_one_file_system, 1},
179 {"separate-dirs", no_argument, &opt_separate_dirs, 1},
180 {"summarize", no_argument, &opt_summarize_only, 1},
181 {"total", no_argument, &opt_combined_arguments, 1},
182 {"help", no_argument, &show_help, 1},
183 {"version", no_argument, &show_version, 1},
188 usage (status, reason)
193 fprintf (status == 0 ? stdout : stderr, "%s: %s\n",
194 program_name, reason);
197 fprintf (stderr, "Try `%s --help' for more information.\n",
201 printf ("Usage: %s [OPTION]... [PATH]...\n", program_name);
204 -a, --all write counts for all files, not just directories\n\
205 -b, --bytes print size in bytes\n\
206 -c, --total produce a grand total\n\
207 -k, --kilobytes use 1024 blocks, not 512 despite POSIXLY_CORRECT\n\
208 -l, --count-links count sizes many times if hard linked\n\
209 -s, --summarize display only a total for each argument\n\
210 -x, --one-file-system skip directories on different filesystems\n\
211 -D, --dereference-args dereference PATHs when symbolic link\n\
212 -L, --dereference dereference all symbolic links\n\
213 -S, --separate-dirs do not include size of subdirectories\n\
214 --help display this help and exit\n\
215 --version output version information and exit\n");
231 program_name = argv[0];
233 output_size = getenv ("POSIXLY_CORRECT") ? size_blocks : size_kilobytes;
235 while ((c = getopt_long (argc, argv, "abcklsxDLS", long_options, (int *) 0))
240 case 0: /* Long option. */
248 output_size = size_bytes;
252 opt_combined_arguments = 1;
256 output_size = size_kilobytes;
264 opt_summarize_only = 1;
268 opt_one_file_system = 1;
272 opt_dereference_arguments = 1;
280 opt_separate_dirs = 1;
284 usage (2, (char *) 0);
290 printf ("%s\n", version_string);
297 if (opt_all && opt_summarize_only)
298 usage (2, "cannot both summarize and show all entries");
300 /* Initialize the hash structure for inode numbers. */
301 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
303 str_init (&path, INITIAL_PATH_SIZE);
305 du_files (optind == argc ? cwd_only : argv + optind);
310 /* Recursively print the sizes of the directories (and, if selected, files)
311 named in FILES, the last entry of which is NULL. */
322 ino_t initial_ino; /* Initial directory's inode. */
323 dev_t initial_dev; /* Initial directory's device. */
324 int i; /* Index in FILES. */
327 wd_desc = open (".", O_RDONLY);
329 error (1, errno, "cannot open current directory");
333 error (1, errno, "cannot get current directory");
336 /* Remember the inode and device number of the current directory. */
337 if (SAFE_STAT (".", &stat_buf))
338 error (1, errno, "current directory");
339 initial_ino = stat_buf.st_ino;
340 initial_dev = stat_buf.st_dev;
342 for (i = 0; files[i]; i++)
349 /* Delete final slash in the argument, unless the slash is alone. */
350 s = strlen (arg) - 1;
356 str_copyc (path, arg);
358 else if (arg[0] == '/')
359 str_trunc (path, 0); /* Null path for root directory. */
361 str_copyc (path, arg);
363 if (!opt_combined_arguments)
366 count_entry (arg, 1, 0);
368 /* chdir if `count_entry' has changed the working directory. */
369 if (SAFE_STAT (".", &stat_buf))
370 error (1, errno, ".");
371 if ((stat_buf.st_ino != initial_ino || stat_buf.st_dev != initial_dev)
373 && fchdir (wd_desc) < 0)
374 error (1, errno, "cannot return to original directory");
377 error (1, errno, "cannot change to directory %s", wd);
381 if (opt_combined_arguments)
383 printf ("%ld\ttotal\n", output_size == size_bytes ? tot_size
384 : convert_blocks (tot_size, output_size == size_kilobytes));
393 /* Print (if appropriate) and return the size
394 (in units determined by `output_size') of file or directory ENT.
395 TOP is one for external calls, zero for recursive calls.
396 LAST_DEV is the device that the parent directory of ENT is on. */
399 count_entry (ent, top, last_dev)
406 if (((top && opt_dereference_arguments)
407 ? SAFE_STAT (ent, &stat_buf)
408 : (*xstat) (ent, &stat_buf)) < 0)
410 error (0, errno, "%s", path->text);
416 && stat_buf.st_nlink > 1
417 && hash_insert (stat_buf.st_ino, stat_buf.st_dev))
418 return 0; /* Have counted this already. */
420 if (output_size == size_bytes)
421 size = stat_buf.st_size;
423 size = ST_NBLOCKS (stat_buf);
427 if (S_ISDIR (stat_buf.st_mode))
434 dir_dev = stat_buf.st_dev;
436 if (opt_one_file_system && !top && last_dev != dir_dev)
437 return 0; /* Don't enter a new file system. */
441 error (0, errno, "cannot change to directory %s", path->text);
447 name_space = savedir (".", stat_buf.st_size);
448 if (name_space == NULL)
452 error (0, errno, "%s", path->text);
453 if (chdir ("..") < 0) /* Try to return to previous dir. */
454 error (1, errno, "cannot change to `..' from directory %s",
460 error (1, 0, "virtual memory exhausted");
463 /* Remember the current path. */
465 str_concatc (path, "/");
466 pathlen = path->length;
471 str_concatc (path, namep);
473 size += count_entry (namep, 0, dir_dev);
475 str_trunc (path, pathlen);
476 namep += strlen (namep) + 1;
479 if (chdir ("..") < 0)
480 error (1, errno, "cannot change to `..' from directory %s", path->text);
482 str_trunc (path, pathlen - 1); /* Remove the "/" we added. */
483 if (!opt_summarize_only || top)
485 printf ("%ld\t%s\n", output_size == size_bytes ? size
486 : convert_blocks (size, output_size == size_kilobytes),
487 path->length > 0 ? path->text : "/");
490 return opt_separate_dirs ? 0 : size;
492 else if (opt_all || top)
494 /* FIXME: make this an option. */
495 int print_only_dir_size = 0;
496 if (!print_only_dir_size)
498 printf ("%ld\t%s\n", output_size == size_bytes ? size
499 : convert_blocks (size, output_size == size_kilobytes),
508 /* Allocate space for the hash structures, and set the global
509 variable `htab' to point to it. The initial hash module is specified in
510 MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE. (The
511 hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
512 inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
516 hash_init (modulus, entry_tab_size)
518 unsigned entry_tab_size;
522 htab_r = (struct htab *)
523 xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
525 htab_r->entry_tab = (struct entry *)
526 xmalloc (sizeof (struct entry) * entry_tab_size);
528 htab_r->modulus = modulus;
529 htab_r->entry_tab_size = entry_tab_size;
535 /* Reset the hash structure in the global variable `htab' to
536 contain no entries. */
544 htab->first_free_entry = 0;
547 for (i = htab->modulus; i > 0; i--)
551 /* Insert an item (inode INO and device DEV) in the hash
552 structure in the global variable `htab', if an entry with the same data
553 was not found already. Return zero if the item was inserted and non-zero
557 hash_insert (ino, dev)
561 struct htab *htab_r = htab; /* Initially a copy of the global `htab'. */
563 if (htab_r->first_free_entry >= htab_r->entry_tab_size)
568 unsigned entry_tab_size;
570 /* Increase the number of hash entries, and re-hash the data.
571 The method of shrimping and increasing is made to compactify
572 the heap. If twice as much data would be allocated
573 straightforwardly, we would never re-use a byte of memory. */
575 /* Let `htab' shrimp. Keep only the header, not the pointer vector. */
577 htab_r = (struct htab *)
578 xrealloc ((char *) htab_r, sizeof (struct htab));
580 modulus = 2 * htab_r->modulus;
581 entry_tab_size = 2 * htab_r->entry_tab_size;
583 /* Increase the number of possible entries. */
585 htab_r->entry_tab = (struct entry *)
586 xrealloc ((char *) htab_r->entry_tab,
587 sizeof (struct entry) * entry_tab_size);
589 /* Increase the size of htab again. */
591 htab_r = (struct htab *)
592 xrealloc ((char *) htab_r,
593 sizeof (struct htab) + sizeof (struct entry *) * modulus);
595 htab_r->modulus = modulus;
596 htab_r->entry_tab_size = entry_tab_size;
599 i = htab_r->first_free_entry;
601 /* Make the increased hash table empty. The entries are still
602 available in htab->entry_tab. */
606 /* Go through the entries and install them in the pointer vector
607 htab->hash. The items are actually inserted in htab->entry_tab at
608 the position where they already are. The htab->coll_link need
609 however be updated. Could be made a little more efficient. */
611 for (ep = htab_r->entry_tab; i > 0; i--)
613 hash_insert2 (htab_r, ep->ino, ep->dev);
618 return hash_insert2 (htab_r, ino, dev);
621 /* Insert INO and DEV in the hash structure HTAB, if not
622 already present. Return zero if inserted and non-zero if it
626 hash_insert2 (htab, ino, dev)
631 struct entry **hp, *ep2, *ep;
632 hp = &htab->hash[ino % htab->modulus];
641 /* Search for an entry with the same data. */
645 if (ep->ino == ino && ep->dev == dev)
646 return 1; /* Found an entry with the same data. */
651 /* Did not find it. */
655 ep = *hp = &htab->entry_tab[htab->first_free_entry++];
658 ep->coll_link = ep2; /* `ep2' is NULL if no collision. */
663 /* Initialize the struct string S1 for holding SIZE characters. */
672 s = (string) xmalloc (sizeof (stringstruct));
673 s->text = xmalloc (size + 1);
680 ensure_space (s, size)
686 s->text = xrealloc (s->text, size + 1);
691 /* Assign the null-terminated C-string CSTR to S1. */
698 unsigned l = strlen (cstr);
699 ensure_space (s1, l);
700 strcpy (s1->text, cstr);
705 str_concatc (s1, cstr)
709 unsigned l1 = s1->length;
710 unsigned l2 = strlen (cstr);
711 unsigned l = l1 + l2;
713 ensure_space (s1, l);
714 strcpy (s1->text + l1, cstr);
718 /* Truncate the string S1 to have length LENGTH. */
721 str_trunc (s1, length)
725 if (s1->length > length)
727 s1->text[length] = 0;