Shell Lab: eval template and trace01

This commit is contained in:
cyp0633 2022-05-23 23:43:42 +08:00
parent fe307afa69
commit c081d56e4a
Signed by: cyp0633
GPG Key ID: E1BC508A994A5138
1 changed files with 37 additions and 1 deletions

View File

@ -163,8 +163,44 @@ int main(int argc, char **argv)
* background children don't receive SIGINT (SIGTSTP) from the kernel
* when we type ctrl-c (ctrl-z) at the keyboard.
*/
void eval(char *cmdline)
void eval(char *cmdline)
{
char *argv[MAXARGS];
char buf[MAXLINE]; // command will be parsed and modified?
int bg; // whether it runs in background
pid_t pid;
// preprocess cmd line
strcpy(buf, cmdline);
bg = parseline(buf, argv); // convert the command into argv
if (argv[0] == NULL) // the line is empty, return
{
return;
}
// run external command
if (!builtin_cmd(argv))
{
if ((pid = fork()) == 0) // this is child
{
if (execve(argv[0], argv, environ) < 0) // execute command failed
{
printf("%s: Command not found\n", argv[0]);
exit(0); // here only child exited
}
}
if (!bg) // run the process in foreground:
// wait for foreground job to terminate
{
int status;
if (waitpid(pid, &status, 0) < 0) // if return -1, then waiting failed
{
unix_error("waitfg: waitpid error");
}
}
else
{
printf("%d %s", pid, cmdline);
}
}
return;
}