2 * Mini du implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@lineo.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define BB_DECLARE_EXTERN
26 #define bb_need_name_too_long
29 #include <sys/types.h>
34 #include <sys/param.h> /* for PATH_MAX */
36 typedef void (Display)(long, char *);
38 static const char du_usage[] =
39 "du [OPTION]... [FILE]...\n\n"
40 "\t-s\tdisplay only a total for each argument\n"
43 static int du_depth = 0;
45 static Display *print;
48 print_normal(long size, char *filename)
50 fprintf(stdout, "%-7ld %s\n", size, filename);
54 print_summary(long size, char *filename)
57 print_normal(size, filename);
62 /* tiny recursive du */
69 if ((lstat(filename, &statbuf)) != 0) {
70 fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
75 sum = statbuf.st_blocks;
77 if (S_ISDIR(statbuf.st_mode)) {
81 dir = opendir(filename);
82 if (!dir) { return 0; }
83 while ((entry = readdir(dir))) {
84 char newfile[PATH_MAX + 1];
85 char *name = entry->d_name;
87 if ( (strcmp(name, "..") == 0)
88 || (strcmp(name, ".") == 0))
91 if (strlen(filename) + strlen(name) + 1 > PATH_MAX) {
92 fprintf(stderr, name_too_long, "du");
95 sprintf(newfile, "%s/%s", filename, name);
100 print(sum, filename);
107 du_main(int argc, char **argv)
112 /* default behaviour */
113 print = print_normal;
116 for (i = 1; i < argc; i++) {
117 if (argv[i][0] == '-') {
121 print = print_summary;
127 fprintf(stderr, "du: invalid option -- %c\n", opt);
135 /* go through remaining args (if any) */
140 for ( ; i < argc; i++) {
142 if ((sum) && (isDirectory(argv[i], FALSE))) { print_normal(sum, argv[i]); }
149 /* $Id: du.c,v 1.10 2000/02/07 05:29:42 erik Exp $ */