xv6-cs450/sh.c

56 lines
858 B
C
Raw Normal View History

#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
char *args[100];
void parse(char buf[]);
int
main(void)
{
char buf[128];
int pid;
while(1){
puts("$ ");
memset (buf, '\0', sizeof(buf));
gets(buf, sizeof(buf));
if(buf[0] == '\0')
continue;
pid = fork();
if(pid == 0){
parse(buf);
if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == '\0') { // cd
chdir(&buf[3]);
} else {
exec(buf, args);
printf(1, "%s: not found\n", buf);
exit();
}
}
if(pid > 0)
wait();
}
}
void
parse(char buf[])
{
int j = 1;
int i;
args[0] = buf;
for (i = 0; buf[i] != '\0'; i++) {
if (buf[i] == ' ') {
buf[i] = '\0';
args[j++] = buf + i + 1;
if (j >= 100) {
printf(2, "too many args\n");
exit();
}
}
}
args[j] = '\0';
}