From b362c49120f3ecb1a7244f0d1229046004b8ad2b Mon Sep 17 00:00:00 2001 From: CGH0S7 <776459475@qq.com> Date: Mon, 3 Mar 2025 16:47:03 +0800 Subject: [PATCH] find finished --- .idea/.gitignore | 3 ++ .idea/codeStyles/Project.xml | 7 ++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ .idea/xv6-labs.iml | 9 +++++ user/find.c | 60 ++++++++++++++++++++++++++-- user/ulib.c | 21 ++++++++++ user/user.h | 2 + 10 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/xv6-labs.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f03c948 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..04cdb68 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/xv6-labs.iml b/.idea/xv6-labs.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/xv6-labs.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/user/find.c b/user/find.c index fe1cbc9..1e00f93 100644 --- a/user/find.c +++ b/user/find.c @@ -1,12 +1,64 @@ -#include "kernel/fcntl.h" #include "kernel/fs.h" #include "kernel/stat.h" #include "kernel/types.h" #include "user/user.h" -int main(int argc, char *argv[]) { - if (argc < 3) { - printf("Usage: find path pattern\n"); +void find(char *path, char *filename) { + int fd = open(path, 0); + char buf[512], *p; + struct dirent de; + struct stat st; + + if (fd < 0 || strlen(path) + 1 + DIRSIZ + 1 >= sizeof(buf)) { + fprintf(2, "find: Invalid path: %s\n", path); + // close(fd); + return; } + + if (fstat(fd, &st) < 0) { + fprintf(2, "find: Failed to stat %s\n", path); + close(fd); + return; + } + + if (st.type != T_DIR) { + fprintf(2, "find: %s is not a directory\n", path); + close(fd); + return; + } + + strncpy(buf, path, strlen(path) + 1); + p = buf + strlen(path); + *p++ = '/'; + while (read(fd, &de, sizeof(de)) == sizeof(de)) { + if (de.inum == 0) + continue; + if (!strcmp(de.name, ".") || !strcmp(de.name, "..")) + continue; + + memmove(p, de.name, DIRSIZ); + p[DIRSIZ] = 0; + + if (stat(buf, &st) < 0) { + fprintf(2, "find: Failed to stat %s\n", buf); + continue; + } + + if (st.type == T_DIR) { + find(buf, filename); + } else if (st.type == T_FILE) { + if (!strcmp(filename, "*") || !strcmp(filename, de.name)) { + printf("%s\n", buf); + } + } + } +} + +int main(int argc, char *argv[]) { + if (argc != 3) { + printf("Usage: find path filename\n"); + exit(1); + } + find(argv[1], argv[2]); exit(0); } \ No newline at end of file diff --git a/user/ulib.c b/user/ulib.c index da36559..0b8ae71 100644 --- a/user/ulib.c +++ b/user/ulib.c @@ -21,12 +21,33 @@ char *strcpy(char *s, const char *t) { return os; } +char *strncpy(char *s, const char *t, int n) { + char *os; + + os = s; + if (n <= 0) + return os; + while (--n > 0 && (*s++ = *t++) != 0) + ; + while (n-- > 0) + *s++ = 0; + return os; +} + int strcmp(const char *p, const char *q) { while (*p && *p == *q) p++, q++; return (uchar)*p - (uchar)*q; } +int strncmp(const char *p, const char *q, int n) { + while (n > 0 && *p && *p == *q) + p++, q++, n--; + if (n == 0) + return 0; + return (uchar)*p - (uchar)*q; +} + uint strlen(const char *s) { int n; diff --git a/user/user.h b/user/user.h index f16a610..075769d 100644 --- a/user/user.h +++ b/user/user.h @@ -27,9 +27,11 @@ int uptime(void); // ulib.c int stat(const char *, struct stat *); char *strcpy(char *, const char *); +char *strncpy(char *, const char *, int); void *memmove(void *, const void *, int); char *strchr(const char *, char c); int strcmp(const char *, const char *); +int strncmp(const char *, const char *, int); void fprintf(int, const char *, ...); void printf(const char *, ...); char *gets(char *, int max);