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 #include <sys/types.h>
35 typedef void (Display)(size_t, char *);
37 static const char du_usage[] =
38 "Usage: du [OPTION]... [FILE]...\n\n"
39 "\t-s\tdisplay only a total for each argument\n"
42 static int du_depth = 0;
44 static Display *print;
47 print_null(size_t size, char *filename) { }
50 print_normal(size_t size, char *filename)
52 fprintf(stdout, "%-7d %s\n", (size >> 1), filename);
56 print_summary(size_t size, char *filename)
59 print_normal(size, filename);
64 /* tiny recursive du */
71 if ((lstat(filename, &statbuf)) != 0) {
72 fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
77 sum = statbuf.st_blocks;
79 if (S_ISDIR(statbuf.st_mode)) {
83 dir = opendir(filename);
84 if (!dir) { return 0; }
85 while ((entry = readdir(dir))) {
87 char *name = entry->d_name;
89 if ( (strcmp(name, "..") == 0)
90 || (strcmp(name, ".") == 0))
93 sprintf(newfile, "%s/%s", filename, name);
104 du_main(int argc, char **argv)
109 /* default behaviour */
110 print = print_normal;
113 for (i = 1; i < argc; i++) {
114 if (argv[i][0] == '-') {
118 print = print_summary;
124 fprintf(stderr, "du: invalid option -- %c\n", opt);
132 /* go through remaining args (if any) */
137 for ( ; i < argc; i++) {
139 if (sum) { print_normal(sum, argv[i]); }
146 /* $Id: du.c,v 1.6 1999/12/15 18:52:17 beppu Exp $ */