-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.c
70 lines (59 loc) · 1.55 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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
void
find(char *path, char *file){
char buf[512], *p;
int fd;//file directory
struct dirent de;//struct preserve diretor
struct stat sa;//judge is file or directory
if((fd = open(path, 0))<0){
fprintf(2, "File can not open %s\n", path);
return;
}
if(fstat(fd, &sa)<0 || T_DIR!=sa.type){
fprintf(2, "path %s must be dir\n", path);
close(fd);
return;
}
//diama renwei yiding shidir,yinci kaishi split
while(read(fd, &de, sizeof(de))==sizeof(de)){
if(de.inum==0){
continue;
}
strcpy(buf, path);
p=buf+strlen(buf);
*p++ = '/';
memmove(p, de.name, DIRSIZ);
p[DIRSIZ]=0;
if(stat(buf, &sa)<0){
printf("can not stat %s\n", buf);
continue;
}
//buf limian yonglebaocunzhenggestring zheli yong de.name
// compare de.name with file
switch(sa.type){
case T_FILE:
if(strcmp(de.name, file)==0){
printf("%s\n", buf);
}
break;
case T_DIR:
if(strcmp(de.name, ".")!=0 && strcmp(de.name, "..")!=0){
find(buf, file);
}
break;
}
}
close(fd);
}
int
main(int argc, char *argv[]){
if(argc<3){
fprintf(2, "Usage find need two params\n");
exit(-1);
}
find(argv[1], argv[2]);
exit(0);
}