From 7dbcd957afba9d69c46e499e33e4c231a52f9f5e Mon Sep 17 00:00:00 2001 From: CGH0S7 <776459475@qq.com> Date: Mon, 3 Mar 2025 17:46:22 +0800 Subject: [PATCH] case closed --- Makefile | 5 ++++- time.txt | 1 + user/xargs.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 time.txt create mode 100644 user/xargs.c diff --git a/Makefile b/Makefile index 80183fa..b6d2c19 100644 --- a/Makefile +++ b/Makefile @@ -192,6 +192,7 @@ UPROGS=\ $U/_pingpong\ $U/_primes\ $U/_find\ + $U/_xargs\ @@ -339,7 +340,9 @@ grade: @echo $(MAKE) clean @$(MAKE) clean || \ (echo "'make clean' failed. HINT: Do you have another running instance of xv6?" && exit 1) - ./grade-lab-$(LAB) $(GRADEFLAGS) + # ./grade-lab-$(LAB) $(GRADEFLAGS) + python grade-lab-$(LAB) + ## ## FOR submissions diff --git a/time.txt b/time.txt new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/time.txt @@ -0,0 +1 @@ +3 diff --git a/user/xargs.c b/user/xargs.c new file mode 100644 index 0000000..f5b849f --- /dev/null +++ b/user/xargs.c @@ -0,0 +1,59 @@ +#include "kernel/param.h" +#include "kernel/stat.h" +#include "kernel/types.h" +#include "user/user.h" + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(2, "Usage: xargs command\n"); + exit(1); + } + + char *cmd[argc + 1]; + int index = 0, data = 0; + for (int i = 1; i < argc; ++i) { + cmd[index++] = argv[i]; + } + + char buffer[MAXARG]; + char line[MAXARG] = {0}; + int line_pos = 0; + + while ((data = read(0, buffer, MAXARG)) > 0) { + for (int i = 0; i < data; ++i) { + if (buffer[i] == '\n' || buffer[i] == ' ') { + line[line_pos] = 0; + if (line_pos > 0) { + char *arg = malloc(line_pos + 1); + strcpy(arg, line); + cmd[index++] = arg; + } + line_pos = 0; + + if (buffer[i] == '\n') { + cmd[index] = 0; + if (fork() == 0) { + exec(cmd[0], cmd); + exit(1); + } + wait(0); + index = argc - 1; + } + } else { + line[line_pos++] = buffer[i]; + } + } + } + + // bug fixed + if (line_pos > 0) { + line[line_pos] = 0; + cmd[index++] = line; + cmd[index] = 0; + if (fork() == 0) { + exec(cmd[0], cmd); + } + wait(0); + } + exit(0); +} \ No newline at end of file