case closed
This commit is contained in:
5
Makefile
5
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
|
||||
|
||||
59
user/xargs.c
Normal file
59
user/xargs.c
Normal file
@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user