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 static int du_depth /*= 0*/;
41 static int count_hardlinks /*= 0*/;
42 static int one_file_system /*= 0*/;
45 static void (*print) (long, char *);
47 static void print_normal(long size, char *filename)
49 #ifdef CONFIG_FEATURE_HUMAN_READABLE
50 printf("%s\t%s\n", make_human_readable_str(size << 10, 1, disp_hr),
53 printf("%ld\t%s\n", size, filename);
57 static void print_summary(long size, char *filename)
60 print_normal(size, filename);
64 /* tiny recursive du */
65 static long du(char *filename)
70 if ((lstat(filename, &statbuf)) != 0) {
71 perror_msg("%s", filename);
75 dir_dev = statbuf.st_dev;
76 else if (one_file_system && dir_dev != statbuf.st_dev)
80 sum = (statbuf.st_blocks >> 1);
82 /* Don't add in stuff pointed to by symbolic links */
83 if (S_ISLNK(statbuf.st_mode)) {
88 if (S_ISDIR(statbuf.st_mode)) {
93 dir = opendir(filename);
99 newfile = last_char_is(filename, '/');
103 while ((entry = readdir(dir))) {
104 char *name = entry->d_name;
106 if ((strcmp(name, "..") == 0)
107 || (strcmp(name, ".") == 0)) {
110 newfile = concat_path_file(filename, name);
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)) {
121 print(sum, filename);
123 add_to_ino_dev_hashtable(&statbuf, NULL);
130 int du_main(int argc, char **argv)
132 int status = EXIT_SUCCESS;
136 /* default behaviour */
137 print = print_normal;
140 while ((c = getopt(argc, argv, "slx"
141 #ifdef CONFIG_FEATURE_HUMAN_READABLE
147 print = print_summary;
155 #ifdef CONFIG_FEATURE_HUMAN_READABLE
170 /* go through remaining args (if any) */
171 if (optind >= argc) {
173 status = EXIT_FAILURE;
177 for (i = optind; i < argc; i++) {
179 if (is_directory(argv[i], FALSE, NULL) == FALSE) {
180 print_normal(sum, argv[i]);
182 reset_ino_dev_hashtable();
189 /* $Id: du.c,v 1.55 2002/08/23 07:28:45 aaronl Exp $ */
192 c-file-style: "linux"