1 /* vi: set sw=4 ts=4: */
3 * Mini du implementation for busybox
6 * Copyright (C) 1999,2000,2001 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
36 #ifdef BB_FEATURE_HUMAN_READABLE
37 static unsigned long disp_hr = KILOBYTE;
40 typedef void (Display) (long, char *);
42 static int du_depth = 0;
43 static int count_hardlinks = 0;
45 static Display *print;
47 static void print_normal(long size, char *filename)
50 #ifdef BB_FEATURE_HUMAN_READABLE
61 printf("%s\t%s\n", make_human_readable_str(size, base), filename);
63 printf("%ld\t%s\n", size, filename);
67 static void print_summary(long size, char *filename)
70 print_normal(size, filename);
74 #define HASH_SIZE 311 /* Should be prime */
75 #define hash_inode(i) ((i) % HASH_SIZE)
77 typedef struct ino_dev_hash_bucket_struct {
78 struct ino_dev_hash_bucket_struct *next;
82 } ino_dev_hashtable_bucket_t;
84 static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
87 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
88 * `ino_dev_hashtable', else return 0
90 * If NAME is a non-NULL pointer to a character pointer, and there is
91 * a match, then set *NAME to the value of the name slot in that
94 static int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
96 ino_dev_hashtable_bucket_t *bucket;
98 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
99 while (bucket != NULL) {
100 if ((bucket->ino == statbuf->st_ino) &&
101 (bucket->dev == statbuf->st_dev))
103 if (name) *name = bucket->name;
106 bucket = bucket->next;
111 /* Add statbuf to statbuf hash table */
112 static void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
116 ino_dev_hashtable_bucket_t *bucket;
118 i = hash_inode(statbuf->st_ino);
119 s = name ? strlen(name) : 0;
120 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
121 bucket->ino = statbuf->st_ino;
122 bucket->dev = statbuf->st_dev;
124 strcpy(bucket->name, name);
126 bucket->name[0] = '\0';
127 bucket->next = ino_dev_hashtable[i];
128 ino_dev_hashtable[i] = bucket;
131 /* Clear statbuf hash table */
132 static void reset_ino_dev_hashtable(void)
135 ino_dev_hashtable_bucket_t *bucket;
137 for (i = 0; i < HASH_SIZE; i++) {
138 while (ino_dev_hashtable[i] != NULL) {
139 bucket = ino_dev_hashtable[i]->next;
140 free(ino_dev_hashtable[i]);
141 ino_dev_hashtable[i] = bucket;
146 /* tiny recursive du */
147 static long du(char *filename)
152 if ((lstat(filename, &statbuf)) != 0) {
153 perror_msg("%s", filename);
158 sum = (statbuf.st_blocks >> 1);
160 /* Don't add in stuff pointed to by symbolic links */
161 if (S_ISLNK(statbuf.st_mode)) {
164 print(sum, filename);
166 if (S_ISDIR(statbuf.st_mode)) {
168 struct dirent *entry;
171 dir = opendir(filename);
177 newfile = last_char_is(filename, '/');
181 while ((entry = readdir(dir))) {
182 char *name = entry->d_name;
184 if ((strcmp(name, "..") == 0)
185 || (strcmp(name, ".") == 0)) {
188 newfile = concat_path_file(filename, name);
193 print(sum, filename);
195 else if (statbuf.st_nlink > 1 && !count_hardlinks) {
196 /* Add files with hard links only once */
197 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
200 print(sum, filename);
203 add_to_ino_dev_hashtable(&statbuf, NULL);
210 int du_main(int argc, char **argv)
212 int status = EXIT_SUCCESS;
216 /* default behaviour */
217 print = print_normal;
220 while ((c = getopt(argc, argv, "sl"
221 #ifdef BB_FEATURE_HUMAN_READABLE
227 print = print_summary;
232 #ifdef BB_FEATURE_HUMAN_READABLE
233 case 'h': disp_hr = 0; break;
234 case 'm': disp_hr = MEGABYTE; break;
242 /* go through remaining args (if any) */
243 if (optind >= argc) {
245 status = EXIT_FAILURE;
249 for (i=optind; i < argc; i++) {
250 if ((sum = du(argv[i])) == 0)
251 status = EXIT_FAILURE;
252 if(is_directory(argv[i], FALSE, NULL)==FALSE) {
253 print_normal(sum, argv[i]);
255 reset_ino_dev_hashtable();
262 /* $Id: du.c,v 1.48 2001/06/01 21:47:15 andersen Exp $ */
265 c-file-style: "linux"