-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.c
101 lines (82 loc) · 2.03 KB
/
find.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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
//echo > PubxP7FB/p3XBYgjG
static char buf[512];
void find(char *path, char *filename) {
char *p;
char *pathP;
int i, fd;
struct dirent de;
struct stat st;
//printf("1 path=%s flie=%s\n", path, filename);
if ((fd = open(path, 0)) < 0) {
fprintf(2, "find: cannot open %s\n", path);
return;
}
if (fstat(fd, &st) < 0) {
fprintf(2, "find: cannot stat %s\n", path);
close(fd);
return;
}
switch (st.type) {
case T_FILE:
pathP = path;
for(i=strlen(path); i>0; i--)
{
if(path[i] == '/')
{
pathP = &path[i+1];
break;
}
}
if (strcmp(pathP, filename) == 0) {
printf("%s\n", path);
}
else
{
//printf("6 path=%s flie=%s\n", path, filename);
}
break;
case T_DIR:
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
printf("find: path too long\n");
break;
}
strcpy(buf, path);
p = buf + strlen(path);
*p++ = '/';
//printf("2 buf=%s DIRSIZ=%d\n", buf, DIRSIZ);
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0) {
continue;
}
//memmove(p, de.name, DIRSIZ);
//p[DIRSIZ] = 0;
strcpy(p, de.name);
//printf("3 buf=%s\n", buf);
if (stat(buf, &st) < 0) {
printf("find: cannot stat %s\n", buf);
continue;
}
if (strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0) {
continue;
}
//printf("4 buf=%s flie=%s \n", buf, filename);
find(buf, filename);
//printf("5\n");
}
break;
}
close(fd);
}
int main(int argc, char * * argv) {
if (argc != 3) {
fprintf(2, "Usage: find <path> <filename>\n");
exit(1);
}
//printf("argv[2]=%s\n",argv[2]);
find(argv[1], argv[2]);
exit(0);
}