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
| #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h" #include "kernel/fs.h"
void find(char* dirName, char* fileName) { int fd; struct dirent de; struct stat st; char buf[512]; memset(buf, 0, sizeof(buf)); memmove(buf, dirName, strlen(dirName)); char *buf_ptr = buf + strlen(dirName); *buf_ptr++ = '/'; if((fd = open(dirName, 0)) < 0){ fprintf(2, "find: cannot open %s\n", dirName); return; }
if(fstat(fd, &st) < 0){ fprintf(2, "find: cannot stat %s\n", dirName); close(fd); return; }
while(read(fd, &de, sizeof(de)) == sizeof(de)) { if(de.inum == 0) continue; if(!strcmp(".", de.name) || !strcmp("..", de.name)) continue; memmove(buf_ptr, de.name, DIRSIZ); *(buf_ptr + DIRSIZ) = 0; stat(buf, &st); if(st.type == T_FILE) { if(!strcmp(fileName, de.name)) { fprintf(1, "%s\n", buf); } } else if(st.type == T_DIR) { find(buf, fileName); } } return; }
int main(int argc, char** argv) { if(argc != 3) { fprintf(2, "invalid args!"); exit(1); } char* dirName = argv[1]; char* fileName = argv[2]; find(dirName, fileName); exit(0); }
|