Fix beppu's email address.
[oweals/busybox.git] / du.c
1 /*
2  * Mini du implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by John Beppu <beppu@lineo.com>
7  *
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>
29 #if 0
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #endif
33
34 static const char du_usage[] =
35 "Usage: du [OPTION]... [FILE]...\n";
36
37 typedef void (Display)(size_t, char *);
38
39 static void
40 print(size_t size, char *filename)
41 {
42     fprintf(stdout, "%-7d %s\n", (size >> 1), filename);
43 }
44
45 /* tiny recursive du */
46 static size_t
47 du(char *filename)
48 {
49     struct stat statbuf;
50     size_t      sum;
51
52     if ((lstat(filename, &statbuf)) != 0) { return 0; }
53     sum = statbuf.st_blocks;
54
55     if (S_ISDIR(statbuf.st_mode)) {
56         DIR             *dir;
57         struct dirent   *entry;
58
59         dir = opendir(filename);
60         if (!dir) { return 0; }
61         while ((entry = readdir(dir))) {
62             char newfile[512];
63             char *name = entry->d_name;
64
65             if (  (strcmp(name, "..") == 0)
66                || (strcmp(name, ".")  == 0)) 
67             { continue; }
68
69             sprintf(newfile, "%s/%s", filename, name);
70             sum += du(newfile);
71         }
72         closedir(dir);
73         print(sum, filename);
74     }
75     return sum;
76 }
77
78 int 
79 du_main(int argc, char **argv)
80 {
81     int i;
82     char opt;
83
84     /* parse argv[] */
85     for (i = 1; i < argc; i++) {
86         if (argv[i][0] == '-') {
87             opt = argv[i][1];
88             switch (opt) {
89                 case 's':
90                     break;
91                 case 'h':
92                     usage(du_usage);
93                     break;
94                 default:
95                     fprintf(stderr, "du: invalid option -- %c\n", opt);
96                     usage(du_usage);
97             }
98         } else {
99             break;
100         }
101     }
102
103     /* go through remaining args (if any) */
104     if (i >= argc) {
105         du(".");
106     } else {
107         for ( ; i < argc; i++) {
108             printf("%-7d %s\n", du(argv[i]) >> 1, argv[i]);
109         }
110     }
111
112     exit(0);
113 }
114
115 /* $Id: du.c,v 1.3 1999/12/10 06:45:42 andersen Exp $ */