4dc7ea13ad3703f2910b4d1b0f634e082d1b2270
[oweals/busybox.git] / du.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini du implementation for busybox
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by John Beppu <beppu@lineo.com>
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #include "internal.h"
26 #define BB_DECLARE_EXTERN
27 #define bb_need_name_too_long
28 #include "messages.c"
29
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <sys/param.h>                  /* for PATH_MAX */
36
37 typedef void (Display) (long, char *);
38
39 typedef struct inode_type {
40         struct inode_type *next;
41         ino_t ino;
42 } INODETYPE;
43
44 #define HASH_SIZE       311             /* Should be prime */
45 #define hash_inode(i)   ((i) % HASH_SIZE)
46
47 static INODETYPE *inode_hash_list[HASH_SIZE];
48
49 static const char du_usage[] =
50         "du [OPTION]... [FILE]...\n\n"
51         "Summarize disk space used for each FILE and/or directory.\n"
52         "Disk space is printed in units of 1024 bytes.\n\n"
53         "Options:\n"
54         "\t-l\tcount sizes many times if hard linked\n"
55         "\t-s\tdisplay only a total for each argument\n";
56
57 static int du_depth = 0;
58 static int count_hardlinks = 0;
59
60 static Display *print;
61
62 static void print_normal(long size, char *filename)
63 {
64         fprintf(stdout, "%ld\t%s\n", size, filename);
65 }
66
67 static void print_summary(long size, char *filename)
68 {
69         if (du_depth == 1) {
70                 print_normal(size, filename);
71         }
72 }
73
74 /* Return 1 if inode is in inode hash list, else return 0 */
75 static int is_in_list(const ino_t ino)
76 {
77         INODETYPE *inode;
78
79         inode = inode_hash_list[hash_inode(ino)];
80         while (inode != NULL) {
81                 if (inode->ino == ino)
82                         return 1;
83                 inode = inode->next;
84         }
85
86         return 0;
87 }
88
89 /* Add inode to inode hash list */
90 static void add_inode(const ino_t ino)
91 {
92         int i;
93         INODETYPE *inode;
94     
95         i = hash_inode(ino);
96         inode = malloc(sizeof(INODETYPE));
97         if (inode == NULL)
98                 fatalError("du: Not enough memory.");
99
100         inode->ino = ino;
101         inode->next = inode_hash_list[i];
102         inode_hash_list[i] = inode;
103 }
104
105 /* Clear inode hash list */
106 static void reset_inode_list(void)
107 {
108         int i;
109         INODETYPE *inode;
110
111         for (i = 0; i < HASH_SIZE; i++) {
112                 while (inode_hash_list[i] != NULL) {
113                         inode = inode_hash_list[i]->next;
114                         free(inode_hash_list[i]);
115                         inode_hash_list[i] = inode;
116                 }
117         }
118 }
119
120 /* tiny recursive du */
121 static long du(char *filename)
122 {
123         struct stat statbuf;
124         long sum;
125         int len;
126
127         if ((lstat(filename, &statbuf)) != 0) {
128                 printf("du: %s: %s\n", filename, strerror(errno));
129                 return 0;
130         }
131
132         du_depth++;
133         sum = (statbuf.st_blocks >> 1);
134
135         /* Don't add in stuff pointed to by symbolic links */
136         if (S_ISLNK(statbuf.st_mode)) {
137                 sum = 0L;
138                 if (du_depth == 1)
139                         print(sum, filename);
140         }
141         if (S_ISDIR(statbuf.st_mode)) {
142                 DIR *dir;
143                 struct dirent *entry;
144
145                 dir = opendir(filename);
146                 if (!dir) {
147                         du_depth--;
148                         return 0;
149                 }
150
151                 len = strlen(filename);
152                 if (filename[len - 1] == '/')
153                         filename[--len] = '\0';
154
155                 while ((entry = readdir(dir))) {
156                         char newfile[PATH_MAX + 1];
157                         char *name = entry->d_name;
158
159                         if ((strcmp(name, "..") == 0)
160                                 || (strcmp(name, ".") == 0)) {
161                                 continue;
162                         }
163
164                         if (len + strlen(name) + 1 > PATH_MAX) {
165                                 fprintf(stderr, name_too_long, "du");
166                                 du_depth--;
167                                 return 0;
168                         }
169                         sprintf(newfile, "%s/%s", filename, name);
170
171                         sum += du(newfile);
172                 }
173                 closedir(dir);
174                 print(sum, filename);
175         }
176         else if (statbuf.st_nlink > 1 && !count_hardlinks) {
177                 /* Add files with hard links only once */
178                 if (is_in_list(statbuf.st_ino)) {
179                         sum = 0L;
180                         if (du_depth == 1)
181                                 print(sum, filename);
182                 }
183                 else {
184                         add_inode(statbuf.st_ino);
185                 }
186         }
187         du_depth--;
188         return sum;
189 }
190
191 int du_main(int argc, char **argv)
192 {
193         int i;
194         char opt;
195
196         /* default behaviour */
197         print = print_normal;
198
199         /* parse argv[] */
200         for (i = 1; i < argc; i++) {
201                 if (argv[i][0] == '-') {
202                         opt = argv[i][1];
203                         switch (opt) {
204                         case 's':
205                                 print = print_summary;
206                                 break;
207                         case 'l':
208                                 count_hardlinks = 1;
209                                 break;
210                         case 'h':
211                         case '-':
212                                 usage(du_usage);
213                                 break;
214                         default:
215                                 fprintf(stderr, "du: invalid option -- %c\n", opt);
216                                 usage(du_usage);
217                         }
218                 } else {
219                         break;
220                 }
221         }
222
223         /* go through remaining args (if any) */
224         if (i >= argc) {
225                 du(".");
226         } else {
227                 long sum;
228
229                 for (; i < argc; i++) {
230                         sum = du(argv[i]);
231                         if (sum && isDirectory(argv[i], FALSE, NULL)) {
232                                 print_normal(sum, argv[i]);
233                         }
234                         reset_inode_list();
235                 }
236         }
237
238         exit(0);
239 }
240
241 /* $Id: du.c,v 1.15 2000/02/21 17:27:17 erik Exp $ */