02d1d97376a93c336f1a489a8e253605fe5b0840
[oweals/busybox.git] / coreutils / 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 /* tiny recursive du */
106 static long du(char *filename)
107 {
108         struct stat statbuf;
109         long sum;
110
111         if ((lstat(filename, &statbuf)) != 0) {
112                 fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
113                 return 0;
114         }
115
116         du_depth++;
117         sum = (statbuf.st_blocks >> 1);
118
119         /* Don't add in stuff pointed to by symbolic links */
120         if (S_ISLNK(statbuf.st_mode)) {
121                 return 0;
122         }
123         if (S_ISDIR(statbuf.st_mode)) {
124                 DIR *dir;
125                 struct dirent *entry;
126
127                 dir = opendir(filename);
128                 if (!dir) {
129                         return 0;
130                 }
131                 while ((entry = readdir(dir))) {
132                         char newfile[PATH_MAX + 1];
133                         char *name = entry->d_name;
134
135                         if ((strcmp(name, "..") == 0)
136                                 || (strcmp(name, ".") == 0)) {
137                                 continue;
138                         }
139
140                         if (strlen(filename) + strlen(name) + 1 > PATH_MAX) {
141                                 fprintf(stderr, name_too_long, "du");
142                                 return 0;
143                         }
144                         sprintf(newfile, "%s/%s", filename, name);
145
146                         sum += du(newfile);
147                 }
148                 closedir(dir);
149                 print(sum, filename);
150         }
151         else if (statbuf.st_nlink > 1 && !count_hardlinks) {
152                 /* Add files with hard links only once */
153                 if (is_in_list(statbuf.st_ino))
154                         return 0;
155                 add_inode(statbuf.st_ino);
156         }
157         du_depth--;
158         return sum;
159 }
160
161 int du_main(int argc, char **argv)
162 {
163         int i;
164         char opt;
165
166         /* default behaviour */
167         print = print_normal;
168
169         /* parse argv[] */
170         for (i = 1; i < argc; i++) {
171                 if (argv[i][0] == '-') {
172                         opt = argv[i][1];
173                         switch (opt) {
174                         case 's':
175                                 print = print_summary;
176                                 break;
177                         case 'l':
178                                 count_hardlinks = 1;
179                                 break;
180                         case 'h':
181                         case '-':
182                                 usage(du_usage);
183                                 break;
184                         default:
185                                 fprintf(stderr, "du: invalid option -- %c\n", opt);
186                                 usage(du_usage);
187                         }
188                 } else {
189                         break;
190                 }
191         }
192
193         /* go through remaining args (if any) */
194         if (i >= argc) {
195                 du(".");
196         } else {
197                 long sum;
198
199                 for (; i < argc; i++) {
200                         sum = du(argv[i]);
201                         if ((sum) && (isDirectory(argv[i], FALSE, NULL))) {
202                                 print_normal(sum, argv[i]);
203                         }
204                 }
205         }
206
207         exit(0);
208 }
209
210 /* $Id: du.c,v 1.14 2000/02/19 18:16:49 erik Exp $ */