Fix header file usage -- there were many unnecessary header files included in
[platform/upstream/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,2000 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 "busybox.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 <stdlib.h>
35 #include <getopt.h>
36 #include <errno.h>
37
38 #ifdef BB_FEATURE_HUMAN_READABLE
39 unsigned long du_disp_hr = KILOBYTE;
40 #endif
41
42 typedef void (Display) (long, char *);
43
44 static int du_depth = 0;
45 static int count_hardlinks = 0;
46
47 static Display *print;
48
49 static void print_normal(long size, char *filename)
50 {
51 #ifdef BB_FEATURE_HUMAN_READABLE
52         printf("%s\t%s\n", format((size * KILOBYTE), du_disp_hr), filename);
53 #else
54         printf("%ld\t%s\n", size, filename);
55 #endif
56 }
57
58 static void print_summary(long size, char *filename)
59 {
60         if (du_depth == 1) {
61 printf("summary\n");
62                 print_normal(size, filename);
63         }
64 }
65
66 /* tiny recursive du */
67 static long du(char *filename)
68 {
69         struct stat statbuf;
70         long sum;
71         int len;
72
73         if ((lstat(filename, &statbuf)) != 0) {
74                 perror_msg_and_die("%s", filename);
75         }
76
77         du_depth++;
78         sum = (statbuf.st_blocks >> 1);
79
80         /* Don't add in stuff pointed to by symbolic links */
81         if (S_ISLNK(statbuf.st_mode)) {
82                 sum = 0L;
83                 if (du_depth == 1)
84                         print(sum, filename);
85         }
86         if (S_ISDIR(statbuf.st_mode)) {
87                 DIR *dir;
88                 struct dirent *entry;
89
90                 dir = opendir(filename);
91                 if (!dir) {
92                         du_depth--;
93                         return 0;
94                 }
95
96                 len = strlen(filename);
97                 if (filename[len - 1] == '/')
98                         filename[--len] = '\0';
99
100                 while ((entry = readdir(dir))) {
101                         char newfile[BUFSIZ + 1];
102                         char *name = entry->d_name;
103
104                         if ((strcmp(name, "..") == 0)
105                                 || (strcmp(name, ".") == 0)) {
106                                 continue;
107                         }
108
109                         if (len + strlen(name) + 1 > BUFSIZ) {
110                                 error_msg(name_too_long);
111                                 du_depth--;
112                                 return 0;
113                         }
114                         sprintf(newfile, "%s/%s", filename, name);
115
116                         sum += du(newfile);
117                 }
118                 closedir(dir);
119                 print(sum, filename);
120         }
121         else if (statbuf.st_nlink > 1 && !count_hardlinks) {
122                 /* Add files with hard links only once */
123                 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
124                         sum = 0L;
125                         if (du_depth == 1)
126                                 print(sum, filename);
127                 }
128                 else {
129                         add_to_ino_dev_hashtable(&statbuf, NULL);
130                 }
131         }
132         du_depth--;
133         return sum;
134 }
135
136 int du_main(int argc, char **argv)
137 {
138         int status = EXIT_SUCCESS;
139         int i;
140         int c;
141
142         /* default behaviour */
143         print = print_normal;
144
145         /* parse argv[] */
146         while ((c = getopt(argc, argv, "sl"
147 #ifdef BB_FEATURE_HUMAN_READABLE
148 "hm"
149 #endif
150 "k")) != EOF) {
151                         switch (c) {
152                         case 's':
153                                         print = print_summary;
154                                         break;
155                         case 'l':
156                                         count_hardlinks = 1;
157                                         break;
158 #ifdef BB_FEATURE_HUMAN_READABLE
159                         case 'h': du_disp_hr = 0;        break;
160                         case 'm': du_disp_hr = MEGABYTE; break;
161                         case 'k': du_disp_hr = KILOBYTE; break;
162 #else
163                         case 'k': break;
164 #endif
165                         default:
166                                         usage(du_usage);
167                         }
168         }
169
170         /* go through remaining args (if any) */
171         if (optind >= argc) {
172                 if (du(".") == 0)
173                         status = EXIT_FAILURE;
174         } else {
175                 long sum;
176
177                 for (i=optind; i < argc; i++) {
178                         if ((sum = du(argv[i])) == 0)
179                                 status = EXIT_FAILURE;
180                         if(is_directory(argv[i], FALSE, NULL)==FALSE) {
181                                 print_normal(sum, argv[i]);
182                         }
183                         reset_ino_dev_hashtable();
184                 }
185         }
186
187         return status;
188 }
189
190 /* $Id: du.c,v 1.35 2001/01/27 08:24:37 andersen Exp $ */
191 /*
192 Local Variables:
193 c-file-style: "linux"
194 c-basic-offset: 4
195 tab-width: 4
196 End:
197 */