-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxv6_du.c
More file actions
138 lines (125 loc) · 3.24 KB
/
xv6_du.c
File metadata and controls
138 lines (125 loc) · 3.24 KB
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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user.h"
#include "kernel/fs.h"
char * fmtname(char * path) {
static char buf[DIRSIZ + 1];
char * p;
// Find first character after last slash.
for (p = path + strlen(path); p >= path && * p != '/'; p--);
p++;
// Return blank-padded name.
if (strlen(p) >= DIRSIZ)
return p;
memmove(buf, p, strlen(p));
memset(buf + strlen(p), ' ', DIRSIZ - strlen(p));
return buf;
}
void du(char * path, int k, int threshold) {
char buf[512], * p;
int fd;
struct dirent de;
struct stat st;
if(k==0)
k=1;
else
k=512;
if ((fd = open(path, 0)) < 0) {
printf(2, "du: cannot open %s\n", path);
return;
}
if (fstat(fd, & st) < 0) {
printf(2, "du: cannot stat %s\n", path);
close(fd);
return;
}
switch (st.type) {
case T_FILE:
printf(1, "%d %s\n", (st.size/k), fmtname(path));
printf(1, "%d %s\n", (st.size/k), fmtname(path));
break;
case T_DIR:
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
printf(1, "ls: path too long\n");
break;
}
strcpy(buf, path);
p = buf + strlen(buf);
* p++ = '/';
int summarySize=0;
while (read(fd, & de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0)
continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if (stat(buf, & st) < 0) {
printf(1, "ls: cannot stat %s\n", buf);
continue;
}
if(st.type==T_FILE && st.size>threshold){
summarySize+=(st.size/k);
printf(1, "%d %s\n", (st.size/k), fmtname(buf));
}
}
printf(1, "%d %s\n", summarySize, path);
break;
}
close(fd);
}
int strncmp(const char * p,
const char * q, uint n) {
while (n > 0 && * p && * p == * q)
n--, p++, q++;
if (n == 0)
return 0;
return (uchar) * p - (uchar) * q;
}
int isNumber(char * s) {
int len = strlen(s);
while (len > 0) {
if (strncmp(s, "0123456789", 1) >= 0 && strncmp(s, "0123456789", 1) <= 10) {
len--;
s++;
} else {
return 0;
}
}
return 1;
}
int main(int argc, char * argv[]) {
int i, t, k;
int threshold=0;
char* fileOrDir = ".";
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "-",1) != 0) {
if(i-1>=0 && strcmp(argv[i-1], "-t") != 0)
fileOrDir = argv[i];
}
else {
if (strcmp(argv[i], "-k") == 0 && k==0) {
k = 1;
// printf(1, "we got k\n");
}
else if (strcmp(argv[i], "-t") == 0 && t==0) {
// printf(1, "we got t\n");
if ((i + 1 < argc) && isNumber(argv[i + 1])) {
t = 1;
threshold=atoi(argv[i + 1]);
// printf(1, "\"%s\" valid t param\n", argv[i + 1]);
}
else {
// printf(1, "\"%s\" invalid t param\n", argv[i + 1]);
printf(1,"check usage.\n");
exit();
}
}
else {
printf(1,"check usage.\n");
exit();
}
}
}
// printf(1, "%d %d %d %s\n", t,threshold, k, fileOrDir);
du(fileOrDir,k,threshold);
exit();
}