blob: e5788c63d4a64383d54853ef208283bdc369cde6 [file] [log] [blame]
John Beppu0f5e1ab1999-12-09 18:23:54 +00001/*
2 * Mini du implementation for busybox
3 *
4 *
5 * Copyright (C) 1999 by Lineo, inc.
Eric Andersen70e2f0b1999-12-10 06:45:42 +00006 * Written by John Beppu <beppu@lineo.com>
John Beppu0f5e1ab1999-12-09 18:23:54 +00007 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include "internal.h"
25#include <sys/types.h>
26#include <fcntl.h>
27#include <dirent.h>
28#include <stdio.h>
John Beppu98355411999-12-10 07:40:08 +000029#include <errno.h>
John Beppu14c82b61999-12-10 06:15:27 +000030#if 0
John Beppu0f5e1ab1999-12-09 18:23:54 +000031#include <unistd.h>
32#include <sys/stat.h>
John Beppu14c82b61999-12-10 06:15:27 +000033#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +000034
John Beppu0f5e1ab1999-12-09 18:23:54 +000035typedef void (Display)(size_t, char *);
36
John Beppue1618e41999-12-15 18:52:17 +000037static const char du_usage[] =
38"Usage: du [OPTION]... [FILE]...\n\n"
39"\t-s\tdisplay only a total for each argument\n"
40;
41
42static int du_depth = 0;
43
44static Display *print;
45
John Beppu0f5e1ab1999-12-09 18:23:54 +000046static void
John Beppue1618e41999-12-15 18:52:17 +000047print_null(size_t size, char *filename) { }
48
49static void
50print_normal(size_t size, char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000051{
52 fprintf(stdout, "%-7d %s\n", (size >> 1), filename);
53}
54
John Beppue1618e41999-12-15 18:52:17 +000055static void
56print_summary(size_t size, char *filename)
57{
58 if (du_depth == 1) {
59 print_normal(size, filename);
60 }
61}
62
63
John Beppu0f5e1ab1999-12-09 18:23:54 +000064/* tiny recursive du */
65static size_t
John Beppu14c82b61999-12-10 06:15:27 +000066du(char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000067{
68 struct stat statbuf;
69 size_t sum;
70
John Beppu98355411999-12-10 07:40:08 +000071 if ((lstat(filename, &statbuf)) != 0) {
72 fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
73 return 0;
74 }
John Beppue1618e41999-12-15 18:52:17 +000075
76 du_depth++;
John Beppu0f5e1ab1999-12-09 18:23:54 +000077 sum = statbuf.st_blocks;
78
79 if (S_ISDIR(statbuf.st_mode)) {
80 DIR *dir;
81 struct dirent *entry;
82
83 dir = opendir(filename);
84 if (!dir) { return 0; }
85 while ((entry = readdir(dir))) {
86 char newfile[512];
87 char *name = entry->d_name;
88
89 if ( (strcmp(name, "..") == 0)
90 || (strcmp(name, ".") == 0))
91 { continue; }
92
93 sprintf(newfile, "%s/%s", filename, name);
John Beppu14c82b61999-12-10 06:15:27 +000094 sum += du(newfile);
John Beppu0f5e1ab1999-12-09 18:23:54 +000095 }
96 closedir(dir);
97 print(sum, filename);
98 }
John Beppue1618e41999-12-15 18:52:17 +000099 du_depth--;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000100 return sum;
101}
102
103int
104du_main(int argc, char **argv)
105{
John Beppu14c82b61999-12-10 06:15:27 +0000106 int i;
107 char opt;
108
John Beppue1618e41999-12-15 18:52:17 +0000109 /* default behaviour */
110 print = print_normal;
111
John Beppu14c82b61999-12-10 06:15:27 +0000112 /* parse argv[] */
113 for (i = 1; i < argc; i++) {
114 if (argv[i][0] == '-') {
115 opt = argv[i][1];
116 switch (opt) {
117 case 's':
John Beppue1618e41999-12-15 18:52:17 +0000118 print = print_summary;
John Beppu14c82b61999-12-10 06:15:27 +0000119 break;
120 case 'h':
121 usage(du_usage);
122 break;
123 default:
124 fprintf(stderr, "du: invalid option -- %c\n", opt);
125 usage(du_usage);
126 }
127 } else {
128 break;
129 }
130 }
131
132 /* go through remaining args (if any) */
133 if (i >= argc) {
134 du(".");
135 } else {
John Beppu98355411999-12-10 07:40:08 +0000136 int sum;
John Beppu14c82b61999-12-10 06:15:27 +0000137 for ( ; i < argc; i++) {
John Beppue1618e41999-12-15 18:52:17 +0000138 sum = du(argv[i]);
139 if (sum) { print_normal(sum, argv[i]); }
John Beppu14c82b61999-12-10 06:15:27 +0000140 }
141 }
142
John Beppu0f5e1ab1999-12-09 18:23:54 +0000143 exit(0);
144}
145
John Beppue1618e41999-12-15 18:52:17 +0000146/* $Id: du.c,v 1.6 1999/12/15 18:52:17 beppu Exp $ */