diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /du.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'du.c')
-rw-r--r-- | du.c | 185 |
1 files changed, 94 insertions, 91 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini du implementation for busybox | 3 | * Mini du implementation for busybox |
3 | * | 4 | * |
@@ -31,119 +32,121 @@ | |||
31 | #include <dirent.h> | 32 | #include <dirent.h> |
32 | #include <stdio.h> | 33 | #include <stdio.h> |
33 | #include <errno.h> | 34 | #include <errno.h> |
34 | #include <sys/param.h> /* for PATH_MAX */ | 35 | #include <sys/param.h> /* for PATH_MAX */ |
35 | 36 | ||
36 | typedef void (Display)(long, char *); | 37 | typedef void (Display) (long, char *); |
37 | 38 | ||
38 | static const char du_usage[] = | 39 | static const char du_usage[] = |
39 | "du [OPTION]... [FILE]...\n\n" | ||
40 | "\t-s\tdisplay only a total for each argument\n" | ||
41 | ; | ||
42 | 40 | ||
43 | static int du_depth = 0; | 41 | "du [OPTION]... [FILE]...\n\n" |
42 | "\t-s\tdisplay only a total for each argument\n"; | ||
44 | 43 | ||
45 | static Display *print; | 44 | static int du_depth = 0; |
46 | 45 | ||
47 | static void | 46 | static Display *print; |
48 | print_normal(long size, char *filename) | 47 | |
48 | static void print_normal(long size, char *filename) | ||
49 | { | 49 | { |
50 | fprintf(stdout, "%-7ld %s\n", size, filename); | 50 | fprintf(stdout, "%-7ld %s\n", size, filename); |
51 | } | 51 | } |
52 | 52 | ||
53 | static void | 53 | static void print_summary(long size, char *filename) |
54 | print_summary(long size, char *filename) | ||
55 | { | 54 | { |
56 | if (du_depth == 1) { | 55 | if (du_depth == 1) { |
57 | print_normal(size, filename); | 56 | print_normal(size, filename); |
58 | } | 57 | } |
59 | } | 58 | } |
60 | 59 | ||
61 | 60 | ||
62 | /* tiny recursive du */ | 61 | /* tiny recursive du */ |
63 | static long | 62 | static long du(char *filename) |
64 | du(char *filename) | ||
65 | { | 63 | { |
66 | struct stat statbuf; | 64 | struct stat statbuf; |
67 | long sum; | 65 | long sum; |
68 | 66 | ||
69 | if ((lstat(filename, &statbuf)) != 0) { | 67 | if ((lstat(filename, &statbuf)) != 0) { |
70 | fprintf(stdout, "du: %s: %s\n", filename, strerror(errno)); | 68 | fprintf(stdout, "du: %s: %s\n", filename, strerror(errno)); |
71 | return 0; | 69 | return 0; |
72 | } | ||
73 | |||
74 | du_depth++; | ||
75 | sum = statbuf.st_blocks; | ||
76 | |||
77 | if (S_ISDIR(statbuf.st_mode)) { | ||
78 | DIR *dir; | ||
79 | struct dirent *entry; | ||
80 | |||
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; | ||
86 | |||
87 | if ( (strcmp(name, "..") == 0) | ||
88 | || (strcmp(name, ".") == 0)) | ||
89 | { continue; } | ||
90 | |||
91 | if (strlen(filename) + strlen(name) + 1 > PATH_MAX) { | ||
92 | fprintf(stderr, name_too_long, "du"); | ||
93 | return 0; | ||
94 | } | ||
95 | sprintf(newfile, "%s/%s", filename, name); | ||
96 | |||
97 | sum += du(newfile); | ||
98 | } | 70 | } |
99 | closedir(dir); | 71 | |
100 | print(sum, filename); | 72 | du_depth++; |
101 | } | 73 | sum = statbuf.st_blocks; |
102 | du_depth--; | 74 | |
103 | return sum; | 75 | if (S_ISDIR(statbuf.st_mode)) { |
76 | DIR *dir; | ||
77 | struct dirent *entry; | ||
78 | |||
79 | dir = opendir(filename); | ||
80 | if (!dir) { | ||
81 | return 0; | ||
82 | } | ||
83 | while ((entry = readdir(dir))) { | ||
84 | char newfile[PATH_MAX + 1]; | ||
85 | char *name = entry->d_name; | ||
86 | |||
87 | if ((strcmp(name, "..") == 0) | ||
88 | || (strcmp(name, ".") == 0)) { | ||
89 | continue; | ||
90 | } | ||
91 | |||
92 | if (strlen(filename) + strlen(name) + 1 > PATH_MAX) { | ||
93 | fprintf(stderr, name_too_long, "du"); | ||
94 | return 0; | ||
95 | } | ||
96 | sprintf(newfile, "%s/%s", filename, name); | ||
97 | |||
98 | sum += du(newfile); | ||
99 | } | ||
100 | closedir(dir); | ||
101 | print(sum, filename); | ||
102 | } | ||
103 | du_depth--; | ||
104 | return sum; | ||
104 | } | 105 | } |
105 | 106 | ||
106 | int | 107 | int du_main(int argc, char **argv) |
107 | du_main(int argc, char **argv) | ||
108 | { | 108 | { |
109 | int i; | 109 | int i; |
110 | char opt; | 110 | char opt; |
111 | 111 | ||
112 | /* default behaviour */ | 112 | /* default behaviour */ |
113 | print = print_normal; | 113 | print = print_normal; |
114 | 114 | ||
115 | /* parse argv[] */ | 115 | /* parse argv[] */ |
116 | for (i = 1; i < argc; i++) { | 116 | for (i = 1; i < argc; i++) { |
117 | if (argv[i][0] == '-') { | 117 | if (argv[i][0] == '-') { |
118 | opt = argv[i][1]; | 118 | opt = argv[i][1]; |
119 | switch (opt) { | 119 | switch (opt) { |
120 | case 's': | 120 | case 's': |
121 | print = print_summary; | 121 | print = print_summary; |
122 | break; | 122 | break; |
123 | case 'h': | 123 | case 'h': |
124 | usage(du_usage); | 124 | usage(du_usage); |
125 | break; | 125 | break; |
126 | default: | 126 | default: |
127 | fprintf(stderr, "du: invalid option -- %c\n", opt); | 127 | fprintf(stderr, "du: invalid option -- %c\n", opt); |
128 | usage(du_usage); | 128 | usage(du_usage); |
129 | } | 129 | } |
130 | } else { | 130 | } else { |
131 | break; | 131 | break; |
132 | } | ||
132 | } | 133 | } |
133 | } | ||
134 | 134 | ||
135 | /* go through remaining args (if any) */ | 135 | /* go through remaining args (if any) */ |
136 | if (i >= argc) { | 136 | if (i >= argc) { |
137 | du("."); | 137 | du("."); |
138 | } else { | 138 | } else { |
139 | long sum; | 139 | long sum; |
140 | for ( ; i < argc; i++) { | 140 | |
141 | sum = du(argv[i]); | 141 | for (; i < argc; i++) { |
142 | if ((sum) && (isDirectory(argv[i], FALSE))) { print_normal(sum, argv[i]); } | 142 | sum = du(argv[i]); |
143 | if ((sum) && (isDirectory(argv[i], FALSE))) { | ||
144 | print_normal(sum, argv[i]); | ||
145 | } | ||
146 | } | ||
143 | } | 147 | } |
144 | } | ||
145 | 148 | ||
146 | exit(0); | 149 | exit(0); |
147 | } | 150 | } |
148 | 151 | ||
149 | /* $Id: du.c,v 1.10 2000/02/07 05:29:42 erik Exp $ */ | 152 | /* $Id: du.c,v 1.11 2000/02/08 19:58:47 erik Exp $ */ |