Added error exit on wrong command and prepared waitfg
parent
1540d090d7
commit
5f0ea2601b
18
tsh.c
18
tsh.c
|
@ -168,22 +168,24 @@ void eval(char *cmdline)
|
|||
char *argv[MAXARGS]; // Save flags for process
|
||||
int bg = parseline(cmdline, argv); // See whether cmd is back or foreground and parse cmdline
|
||||
struct job_t *job;
|
||||
|
||||
if (argv[0] != NULL) { // Do nothing, but don't crash on empty line
|
||||
if (!builtin_cmd(argv)) { // if cmdline is not a builtin
|
||||
int pid = fork(); // fork process
|
||||
if (pid == 0) { // Child Process
|
||||
if (execve(argv[0], argv, environ) != 0) { // if exec failed
|
||||
printf("Command not found.\n");
|
||||
exit(0);
|
||||
printf("%s Command not found.\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
exit(0);
|
||||
} else { // Parent Process
|
||||
wait(NULL);
|
||||
addjob(jobs, pid, bg ? BG : FG, cmdline);
|
||||
if (bg) {
|
||||
addjob(jobs, pid, BG, cmdline);
|
||||
job = getjobpid(jobs, pid);
|
||||
printf("[%d] (%d) %s", job->jid, pid, cmdline);
|
||||
} else {
|
||||
addjob(jobs, pid, FG, cmdline);
|
||||
// waitfg(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -298,9 +300,9 @@ void do_bgfg(char **argv)
|
|||
*/
|
||||
void waitfg(pid_t pid)
|
||||
{
|
||||
// while (pid == fgpid(jobs)) {
|
||||
// sleep(1);
|
||||
// }
|
||||
while (pid == fgpid(jobs)) {
|
||||
sleep(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue