Put in GPL v2 or later copyright notice
[oweals/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 static int du_depth /*= 0*/;
41 static int count_hardlinks /*= 0*/;
42 static int one_file_system /*= 0*/;
43 static dev_t dir_dev;
44
45 static void (*print) (long, char *);
46
47 static void print_normal(long size, char *filename)
48 {
49 #ifdef CONFIG_FEATURE_HUMAN_READABLE
50         printf("%s\t%s\n", make_human_readable_str(size << 10, 1, disp_hr),
51                    filename);
52 #else
53         printf("%ld\t%s\n", size, filename);
54 #endif
55 }
56
57 static void print_summary(long size, char *filename)
58 {
59         if (du_depth == 1) {
60                 print_normal(size, filename);
61         }
62 }
63
64 /* tiny recursive du */
65 static long du(char *filename)
66 {
67         struct stat statbuf;
68         long sum;
69
70         if ((lstat(filename, &statbuf)) != 0) {
71                 perror_msg("%s", filename);
72                 return 0;
73         }
74         if (du_depth == 0)
75                 dir_dev = statbuf.st_dev;
76         else if (one_file_system && dir_dev != statbuf.st_dev)
77                 return 0;
78
79         du_depth++;
80         sum = (statbuf.st_blocks >> 1);
81
82         /* Don't add in stuff pointed to by symbolic links */
83         if (S_ISLNK(statbuf.st_mode)) {
84                 sum = 0L;
85                 if (du_depth == 1) {
86                 }
87         }
88         if (S_ISDIR(statbuf.st_mode)) {
89                 DIR *dir;
90                 struct dirent *entry;
91                 char *newfile;
92
93                 dir = opendir(filename);
94                 if (!dir) {
95                         du_depth--;
96                         return 0;
97                 }
98
99                 newfile = last_char_is(filename, '/');
100                 if (newfile)
101                         *newfile = '\0';
102
103                 while ((entry = readdir(dir))) {
104                         char *name = entry->d_name;
105
106                         if ((strcmp(name, "..") == 0)
107                                 || (strcmp(name, ".") == 0)) {
108                                 continue;
109                         }
110                         newfile = concat_path_file(filename, name);
111                         sum += du(newfile);
112                         free(newfile);
113                 }
114                 closedir(dir);
115                 print(sum, filename);
116         } else if (statbuf.st_nlink > 1 && !count_hardlinks) {
117                 /* Add files with hard links only once */
118                 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
119                         sum = 0L;
120                         if (du_depth == 1)
121                                 print(sum, filename);
122                 } else {
123                         add_to_ino_dev_hashtable(&statbuf, NULL);
124                 }
125         }
126         du_depth--;
127         return sum;
128 }
129
130 int du_main(int argc, char **argv)
131 {
132         int status = EXIT_SUCCESS;
133         int i;
134         int c;
135
136         /* default behaviour */
137         print = print_normal;
138
139         /* parse argv[] */
140         while ((c = getopt(argc, argv, "slx"
141 #ifdef CONFIG_FEATURE_HUMAN_READABLE
142                                            "hm"
143 #endif
144                                            "k")) != EOF) {
145                 switch (c) {
146                 case 's':
147                         print = print_summary;
148                         break;
149                 case 'l':
150                         count_hardlinks = 1;
151                         break;
152                 case 'x':
153                         one_file_system = 1;
154                         break;
155 #ifdef CONFIG_FEATURE_HUMAN_READABLE
156                 case 'h':
157                         disp_hr = 0;
158                         break;
159                 case 'm':
160                         disp_hr = MEGABYTE;
161                         break;
162 #endif
163                 case 'k':
164                         break;
165                 default:
166                         show_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                         sum = du(argv[i]);
179                         if (is_directory(argv[i], FALSE, NULL) == FALSE) {
180                                 print_normal(sum, argv[i]);
181                         }
182                         reset_ino_dev_hashtable();
183                 }
184         }
185
186         return status;
187 }
188
189 /* $Id: du.c,v 1.55 2002/08/23 07:28:45 aaronl Exp $ */
190 /*
191 Local Variables:
192 c-file-style: "linux"
193 c-basic-offset: 4
194 tab-width: 4
195 End:
196 */