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