Patch from Edward Betts <edward@debian.org> to add -x switch to du.c
[platform/upstream/busybox.git] / coreutils / du.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini du implementation for busybox
4  *
5  * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  * Copyright (C) 2002  Edward Betts <edward@debian.org>
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 <sys/types.h>
26 #include <fcntl.h>
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <errno.h>
33 #include "busybox.h"
34
35
36 #ifdef CONFIG_FEATURE_HUMAN_READABLE
37 static unsigned long disp_hr = KILOBYTE;
38 #endif
39
40 typedef void (Display) (long, char *);
41
42 static int du_depth = 0;
43 static int count_hardlinks = 0;
44 static int one_file_system = 0;
45 static dev_t dir_dev = 0;
46
47 static Display *print;
48
49 static void print_normal(long size, char *filename)
50 {
51 #ifdef CONFIG_FEATURE_HUMAN_READABLE
52         printf("%s\t%s\n", make_human_readable_str(size<<10, 1, 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                 print_normal(size, filename);
62         }
63 }
64
65 /* tiny recursive du */
66 static long du(char *filename)
67 {
68         struct stat statbuf;
69         long sum;
70
71         if ((lstat(filename, &statbuf)) != 0) {
72                 perror_msg("%s", filename);
73                 return 0;
74         }
75         if (du_depth == 0)
76                 dir_dev = statbuf.st_dev;
77         else if (one_file_system && dir_dev != statbuf.st_dev)
78                 return 0;
79
80         du_depth++;
81         sum = (statbuf.st_blocks >> 1);
82
83         /* Don't add in stuff pointed to by symbolic links */
84         if (S_ISLNK(statbuf.st_mode)) {
85                 sum = 0L;
86                 if (du_depth == 1) {
87                 }
88         }
89         if (S_ISDIR(statbuf.st_mode)) {
90                 DIR *dir;
91                 struct dirent *entry;
92                 char *newfile;
93
94                 dir = opendir(filename);
95                 if (!dir) {
96                         du_depth--;
97                         return 0;
98                 }
99
100                 newfile = last_char_is(filename, '/');
101                 if (newfile)
102                         *newfile = '\0';
103
104                 while ((entry = readdir(dir))) {
105                         char *name = entry->d_name;
106
107                         if ((strcmp(name, "..") == 0)
108                                 || (strcmp(name, ".") == 0)) {
109                                 continue;
110                         }
111                         newfile = concat_path_file(filename, name);
112                         sum += du(newfile);
113                         free(newfile);
114                 }
115                 closedir(dir);
116                 print(sum, filename);
117         }
118         else if (statbuf.st_nlink > 1 && !count_hardlinks) {
119                 /* Add files with hard links only once */
120                 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
121                         sum = 0L;
122                         if (du_depth == 1)
123                                 print(sum, filename);
124                 }
125                 else {
126                         add_to_ino_dev_hashtable(&statbuf, NULL);
127                 }
128         }
129         du_depth--;
130         return sum;
131 }
132
133 int du_main(int argc, char **argv)
134 {
135         int status = EXIT_SUCCESS;
136         int i;
137         int c;
138
139         /* default behaviour */
140         print = print_normal;
141
142         /* parse argv[] */
143         while ((c = getopt(argc, argv, "slx"
144 #ifdef CONFIG_FEATURE_HUMAN_READABLE
145 "hm"
146 #endif
147 "k")) != EOF) {
148                         switch (c) {
149                         case 's':
150                                         print = print_summary;
151                                         break;
152                         case 'l':
153                                         count_hardlinks = 1;
154                                         break;
155                         case 'x':
156                                         one_file_system = 1;
157                                         break;
158 #ifdef CONFIG_FEATURE_HUMAN_READABLE
159                         case 'h': disp_hr = 0;        break;
160                         case 'm': disp_hr = MEGABYTE; break;
161 #endif
162                         case 'k': break;
163                         default:
164                                         show_usage();
165                         }
166         }
167
168         /* go through remaining args (if any) */
169         if (optind >= argc) {
170                 if (du(".") == 0)
171                         status = EXIT_FAILURE;
172         } else {
173                 long sum;
174
175                 for (i=optind; i < argc; i++) {
176                         sum = du(argv[i]);
177                         if(is_directory(argv[i], FALSE, NULL)==FALSE) {
178                                 print_normal(sum, argv[i]);
179                         }
180                         reset_ino_dev_hashtable();
181                 }
182         }
183
184         return status;
185 }
186
187 /* $Id: du.c,v 1.53 2002/04/06 23:16:44 andersen Exp $ */
188 /*
189 Local Variables:
190 c-file-style: "linux"
191 c-basic-offset: 4
192 tab-width: 4
193 End:
194 */