John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini du implementation for busybox |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
Eric Andersen | 70e2f0b | 1999-12-10 06:45:42 +0000 | [diff] [blame] | 6 | * Written by John Beppu <beppu@lineo.com> |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 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> |
John Beppu | 9835541 | 1999-12-10 07:40:08 +0000 | [diff] [blame] | 29 | #include <errno.h> |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 30 | #if 0 |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <sys/stat.h> |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 33 | #endif |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 34 | |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 35 | typedef void (Display)(size_t, char *); |
| 36 | |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 37 | static const char du_usage[] = |
| 38 | "Usage: du [OPTION]... [FILE]...\n\n" |
| 39 | "\t-s\tdisplay only a total for each argument\n" |
| 40 | ; |
| 41 | |
| 42 | static int du_depth = 0; |
| 43 | |
| 44 | static Display *print; |
| 45 | |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 46 | static void |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 47 | print_normal(size_t size, char *filename) |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 48 | { |
| 49 | fprintf(stdout, "%-7d %s\n", (size >> 1), filename); |
| 50 | } |
| 51 | |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 52 | static void |
| 53 | print_summary(size_t size, char *filename) |
| 54 | { |
| 55 | if (du_depth == 1) { |
| 56 | print_normal(size, filename); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 61 | /* tiny recursive du */ |
| 62 | static size_t |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 63 | du(char *filename) |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 64 | { |
| 65 | struct stat statbuf; |
| 66 | size_t sum; |
| 67 | |
John Beppu | 9835541 | 1999-12-10 07:40:08 +0000 | [diff] [blame] | 68 | if ((lstat(filename, &statbuf)) != 0) { |
| 69 | fprintf(stdout, "du: %s: %s\n", filename, strerror(errno)); |
| 70 | return 0; |
| 71 | } |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 72 | |
| 73 | du_depth++; |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 74 | sum = statbuf.st_blocks; |
| 75 | |
| 76 | if (S_ISDIR(statbuf.st_mode)) { |
| 77 | DIR *dir; |
| 78 | struct dirent *entry; |
| 79 | |
| 80 | dir = opendir(filename); |
| 81 | if (!dir) { return 0; } |
| 82 | while ((entry = readdir(dir))) { |
| 83 | char newfile[512]; |
| 84 | char *name = entry->d_name; |
| 85 | |
| 86 | if ( (strcmp(name, "..") == 0) |
| 87 | || (strcmp(name, ".") == 0)) |
| 88 | { continue; } |
| 89 | |
| 90 | sprintf(newfile, "%s/%s", filename, name); |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 91 | sum += du(newfile); |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 92 | } |
| 93 | closedir(dir); |
| 94 | print(sum, filename); |
| 95 | } |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 96 | du_depth--; |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 97 | return sum; |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | du_main(int argc, char **argv) |
| 102 | { |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 103 | int i; |
| 104 | char opt; |
| 105 | |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 106 | /* default behaviour */ |
| 107 | print = print_normal; |
| 108 | |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 109 | /* parse argv[] */ |
| 110 | for (i = 1; i < argc; i++) { |
| 111 | if (argv[i][0] == '-') { |
| 112 | opt = argv[i][1]; |
| 113 | switch (opt) { |
| 114 | case 's': |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 115 | print = print_summary; |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 116 | break; |
| 117 | case 'h': |
| 118 | usage(du_usage); |
| 119 | break; |
| 120 | default: |
| 121 | fprintf(stderr, "du: invalid option -- %c\n", opt); |
| 122 | usage(du_usage); |
| 123 | } |
| 124 | } else { |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* go through remaining args (if any) */ |
| 130 | if (i >= argc) { |
| 131 | du("."); |
| 132 | } else { |
John Beppu | 9835541 | 1999-12-10 07:40:08 +0000 | [diff] [blame] | 133 | int sum; |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 134 | for ( ; i < argc; i++) { |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 135 | sum = du(argv[i]); |
John Beppu | 0aa6118 | 1999-12-16 21:16:47 +0000 | [diff] [blame] | 136 | if ((sum) && (isDirectory(argv[i]))) { print_normal(sum, argv[i]); } |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 140 | exit(0); |
| 141 | } |
| 142 | |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 143 | /* $Id: du.c,v 1.8 1999/12/17 18:44:15 erik Exp $ */ |