1 /* vi: set sw=4 ts=4: */
3 * Mini du implementation for busybox
5 * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
35 #ifdef CONFIG_FEATURE_HUMAN_READABLE
36 static unsigned long disp_hr = KILOBYTE;
39 typedef void (Display) (long, char *);
41 static int du_depth = 0;
42 static int count_hardlinks = 0;
44 static Display *print;
46 static void print_normal(long size, char *filename)
48 #ifdef CONFIG_FEATURE_HUMAN_READABLE
49 printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
51 printf("%ld\t%s\n", size, filename);
55 static void print_summary(long size, char *filename)
58 print_normal(size, filename);
62 #define HASH_SIZE 311 /* Should be prime */
63 #define hash_inode(i) ((i) % HASH_SIZE)
65 typedef struct ino_dev_hash_bucket_struct {
66 struct ino_dev_hash_bucket_struct *next;
70 } ino_dev_hashtable_bucket_t;
72 static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
75 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
76 * `ino_dev_hashtable', else return 0
78 * If NAME is a non-NULL pointer to a character pointer, and there is
79 * a match, then set *NAME to the value of the name slot in that
82 static int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
84 ino_dev_hashtable_bucket_t *bucket;
86 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
87 while (bucket != NULL) {
88 if ((bucket->ino == statbuf->st_ino) &&
89 (bucket->dev == statbuf->st_dev))
91 if (name) *name = bucket->name;
94 bucket = bucket->next;
99 /* Add statbuf to statbuf hash table */
100 static void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
104 ino_dev_hashtable_bucket_t *bucket;
106 i = hash_inode(statbuf->st_ino);
107 s = name ? strlen(name) : 0;
108 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
109 bucket->ino = statbuf->st_ino;
110 bucket->dev = statbuf->st_dev;
112 strcpy(bucket->name, name);
114 bucket->name[0] = '\0';
115 bucket->next = ino_dev_hashtable[i];
116 ino_dev_hashtable[i] = bucket;
119 /* Clear statbuf hash table */
120 static void reset_ino_dev_hashtable(void)
123 ino_dev_hashtable_bucket_t *bucket;
125 for (i = 0; i < HASH_SIZE; i++) {
126 while (ino_dev_hashtable[i] != NULL) {
127 bucket = ino_dev_hashtable[i]->next;
128 free(ino_dev_hashtable[i]);
129 ino_dev_hashtable[i] = bucket;
134 /* tiny recursive du */
135 static long du(char *filename)
140 if ((lstat(filename, &statbuf)) != 0) {
141 perror_msg("%s", filename);
146 sum = (statbuf.st_blocks >> 1);
148 /* Don't add in stuff pointed to by symbolic links */
149 if (S_ISLNK(statbuf.st_mode)) {
154 if (S_ISDIR(statbuf.st_mode)) {
156 struct dirent *entry;
159 dir = opendir(filename);
165 newfile = last_char_is(filename, '/');
169 while ((entry = readdir(dir))) {
170 char *name = entry->d_name;
172 if ((strcmp(name, "..") == 0)
173 || (strcmp(name, ".") == 0)) {
176 newfile = concat_path_file(filename, name);
181 print(sum, filename);
183 else if (statbuf.st_nlink > 1 && !count_hardlinks) {
184 /* Add files with hard links only once */
185 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
188 print(sum, filename);
191 add_to_ino_dev_hashtable(&statbuf, NULL);
198 int du_main(int argc, char **argv)
200 int status = EXIT_SUCCESS;
204 /* default behaviour */
205 print = print_normal;
208 while ((c = getopt(argc, argv, "sl"
209 #ifdef CONFIG_FEATURE_HUMAN_READABLE
215 print = print_summary;
220 #ifdef CONFIG_FEATURE_HUMAN_READABLE
221 case 'h': disp_hr = 0; break;
222 case 'm': disp_hr = MEGABYTE; break;
230 /* go through remaining args (if any) */
231 if (optind >= argc) {
233 status = EXIT_FAILURE;
237 for (i=optind; i < argc; i++) {
239 if(is_directory(argv[i], FALSE, NULL)==FALSE) {
240 print_normal(sum, argv[i]);
242 reset_ino_dev_hashtable();
249 /* $Id: du.c,v 1.51 2001/10/24 04:59:27 andersen Exp $ */
252 c-file-style: "linux"