blob: 14e0bdf243cb341a53c804a0d47780b579db0cf5 [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 Beppu14c82b61999-12-10 06:15:27 +000035static const char du_usage[] =
36"Usage: du [OPTION]... [FILE]...\n";
John Beppu0f5e1ab1999-12-09 18:23:54 +000037
38typedef void (Display)(size_t, char *);
39
40static void
41print(size_t size, char *filename)
42{
43 fprintf(stdout, "%-7d %s\n", (size >> 1), filename);
44}
45
46/* tiny recursive du */
47static size_t
John Beppu14c82b61999-12-10 06:15:27 +000048du(char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000049{
50 struct stat statbuf;
51 size_t sum;
52
John Beppu98355411999-12-10 07:40:08 +000053 if ((lstat(filename, &statbuf)) != 0) {
54 fprintf(stdout, "du: %s: %s\n", filename, strerror(errno));
55 return 0;
56 }
John Beppu0f5e1ab1999-12-09 18:23:54 +000057 sum = statbuf.st_blocks;
58
59 if (S_ISDIR(statbuf.st_mode)) {
60 DIR *dir;
61 struct dirent *entry;
62
63 dir = opendir(filename);
64 if (!dir) { return 0; }
65 while ((entry = readdir(dir))) {
66 char newfile[512];
67 char *name = entry->d_name;
68
69 if ( (strcmp(name, "..") == 0)
70 || (strcmp(name, ".") == 0))
71 { continue; }
72
73 sprintf(newfile, "%s/%s", filename, name);
John Beppu14c82b61999-12-10 06:15:27 +000074 sum += du(newfile);
John Beppu0f5e1ab1999-12-09 18:23:54 +000075 }
76 closedir(dir);
77 print(sum, filename);
78 }
79 return sum;
80}
81
82int
83du_main(int argc, char **argv)
84{
John Beppu14c82b61999-12-10 06:15:27 +000085 int i;
86 char opt;
87
88 /* parse argv[] */
89 for (i = 1; i < argc; i++) {
90 if (argv[i][0] == '-') {
91 opt = argv[i][1];
92 switch (opt) {
93 case 's':
94 break;
95 case 'h':
96 usage(du_usage);
97 break;
98 default:
99 fprintf(stderr, "du: invalid option -- %c\n", opt);
100 usage(du_usage);
101 }
102 } else {
103 break;
104 }
105 }
106
107 /* go through remaining args (if any) */
108 if (i >= argc) {
109 du(".");
110 } else {
John Beppu98355411999-12-10 07:40:08 +0000111 int sum;
John Beppu14c82b61999-12-10 06:15:27 +0000112 for ( ; i < argc; i++) {
John Beppu98355411999-12-10 07:40:08 +0000113 sum = du(argv[i]) >> 1;
John Beppub52a2181999-12-10 15:23:47 +0000114 if (sum) printf("%-7d %s\n", sum, argv[i]);
John Beppu14c82b61999-12-10 06:15:27 +0000115 }
116 }
117
John Beppu0f5e1ab1999-12-09 18:23:54 +0000118 exit(0);
119}
120
John Beppub52a2181999-12-10 15:23:47 +0000121/* $Id: du.c,v 1.5 1999/12/10 15:23:47 beppu Exp $ */