-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdu.c
167 lines (138 loc) · 3.71 KB
/
mdu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int opt_follow_links = 0;
static int opt_apparent_size = 0;
static int opt_count_total = 0;
/* prototypes these two functions because of cross recursion */
static size_t expand_directory(const char *pathname, int depth);
static size_t process_file(const char *pathname, int depth);
/* call process_file() on each file of a directory, computing the sum of the sizes */
static size_t expand_directory(const char *pathname, int depth){
DIR *dir;
struct dirent *dirent;
size_t size = 0;
char *entry_path = NULL;
size_t dir_path_len, entry_path_len;
/* open directory */
dir = opendir(pathname);
if (dir == NULL){
perror(pathname);
return 0;
}
/* compute the length of pathname */
dir_path_len = strlen(pathname);
/* foreach file in directory */
while ((dirent = readdir(dir)) != NULL){
/* skip '.' and '..' */
if (strcmp(dirent->d_name, ".") == 0 ||
strcmp(dirent->d_name, "..") == 0){
continue;
}
/* compute the length of the current entry path */
entry_path_len = dir_path_len + 1 + strlen(dirent->d_name); /*without '\0'*/
/* allocate the mem for the current entry path,
using realloc for better perf */
entry_path = realloc(entry_path, (entry_path_len + 1) * sizeof(char));
if (entry_path == NULL){
perror("malloc");
exit(EXIT_FAILURE);
}
/* build the current entry path */
strcpy(entry_path, pathname);
if (dir_path_len > 0 && entry_path[dir_path_len - 1] != '/'){
strcat(entry_path, "/");
}
strcat(entry_path, dirent->d_name);
/* recursive call to process_file() */
size += process_file(entry_path, depth + 1);
}
closedir(dir);
/* return the computed sum of size */
return size;
}
/* compute the size of a file recursively */
static size_t process_file(const char *pathname, int depth){
struct stat sb;
size_t size;
int stat_ret;
/* stat() or lstat() the file */
stat_ret = (opt_follow_links ? stat : lstat)(pathname, &sb);
if (stat_ret == -1){
perror(pathname);
return 0;
}
/* get the size */
if (opt_apparent_size){
size = sb.st_size;
}
else {
size = sb.st_blocks;
}
/* recursive call if file is a directory */
if (S_ISDIR(sb.st_mode)){
size += expand_directory(pathname, depth);
/* show information if depth > 0 */
if (depth > 0){
printf("%lu\t%s\n", size, pathname);
}
}
return size;
}
static size_t du_file(const char *pathname){
size_t size = process_file(pathname, 0);
printf("%lu\t%s\n", size, pathname);
return size;
}
static void print_usage(const char *progname){
printf("usage: %s [-b] [-L] [FILE]...\n", progname);
}
/* return -1 if an error is detected */
static int parse_options(int ac, char **av){
int opt;
while ((opt = getopt(ac, av, "bLc")) != -1){
switch (opt){
case 'b':
opt_apparent_size = 1;
break;
case 'L':
opt_follow_links = 1;
break;
case 'c':
opt_count_total = 1;
break;
default:
return -1;
}
}
return 0;
}
int main(int ac, char **av){
size_t total = 0;
/* parse opt */
if (parse_options(ac, av) == -1){
print_usage(av[0]);
exit(EXIT_FAILURE);
}
/* if no file provided, process current directory */
if (optind >= ac){
total += du_file(".");
}
else {
/* process the list of files provided */
while (optind < ac){
total += du_file(av[optind]);
optind += 1;
}
}
/* print the total if opt 'c' has been set */
if (opt_count_total){
printf("total\t%lu\n", total);
}
return EXIT_SUCCESS;
}