1 /* vi: set sw=4 ts=4: */
3 * Mini du implementation for busybox
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>
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.
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.
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
25 #include <sys/types.h>
36 #ifdef CONFIG_FEATURE_HUMAN_READABLE
37 static unsigned long disp_hr = KILOBYTE;
40 typedef void (Display) (long, char *);
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;
47 static Display *print;
49 static void print_normal(long size, char *filename)
51 #ifdef CONFIG_FEATURE_HUMAN_READABLE
52 printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
54 printf("%ld\t%s\n", size, filename);
58 static void print_summary(long size, char *filename)
61 print_normal(size, filename);
65 /* tiny recursive du */
66 static long du(char *filename)
71 if ((lstat(filename, &statbuf)) != 0) {
72 perror_msg("%s", filename);
76 dir_dev = statbuf.st_dev;
77 else if (one_file_system && dir_dev != statbuf.st_dev)
81 sum = (statbuf.st_blocks >> 1);
83 /* Don't add in stuff pointed to by symbolic links */
84 if (S_ISLNK(statbuf.st_mode)) {
89 if (S_ISDIR(statbuf.st_mode)) {
94 dir = opendir(filename);
100 newfile = last_char_is(filename, '/');
104 while ((entry = readdir(dir))) {
105 char *name = entry->d_name;
107 if ((strcmp(name, "..") == 0)
108 || (strcmp(name, ".") == 0)) {
111 newfile = concat_path_file(filename, name);
116 print(sum, filename);
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)) {
123 print(sum, filename);
126 add_to_ino_dev_hashtable(&statbuf, NULL);
133 int du_main(int argc, char **argv)
135 int status = EXIT_SUCCESS;
139 /* default behaviour */
140 print = print_normal;
143 while ((c = getopt(argc, argv, "slx"
144 #ifdef CONFIG_FEATURE_HUMAN_READABLE
150 print = print_summary;
158 #ifdef CONFIG_FEATURE_HUMAN_READABLE
159 case 'h': disp_hr = 0; break;
160 case 'm': disp_hr = MEGABYTE; break;
168 /* go through remaining args (if any) */
169 if (optind >= argc) {
171 status = EXIT_FAILURE;
175 for (i=optind; i < argc; i++) {
177 if(is_directory(argv[i], FALSE, NULL)==FALSE) {
178 print_normal(sum, argv[i]);
180 reset_ino_dev_hashtable();
187 /* $Id: du.c,v 1.53 2002/04/06 23:16:44 andersen Exp $ */
190 c-file-style: "linux"