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
|
char *argv[MAXARGS]; // Save flags for process
|
||||||
int bg = parseline(cmdline, argv); // See whether cmd is back or foreground and parse cmdline
|
int bg = parseline(cmdline, argv); // See whether cmd is back or foreground and parse cmdline
|
||||||
struct job_t *job;
|
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
|
if (!builtin_cmd(argv)) { // if cmdline is not a builtin
|
||||||
int pid = fork(); // fork process
|
int pid = fork(); // fork process
|
||||||
if (pid == 0) { // Child Process
|
if (pid == 0) { // Child Process
|
||||||
if (execve(argv[0], argv, environ) != 0) { // if exec failed
|
if (execve(argv[0], argv, environ) != 0) { // if exec failed
|
||||||
printf("Command not found.\n");
|
printf("%s Command not found.\n", argv[0]);
|
||||||
exit(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
exit(0);
|
|
||||||
} else { // Parent Process
|
} else { // Parent Process
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
|
addjob(jobs, pid, bg ? BG : FG, cmdline);
|
||||||
if (bg) {
|
if (bg) {
|
||||||
addjob(jobs, pid, BG, cmdline);
|
|
||||||
job = getjobpid(jobs, pid);
|
job = getjobpid(jobs, pid);
|
||||||
printf("[%d] (%d) %s", job->jid, pid, cmdline);
|
printf("[%d] (%d) %s", job->jid, pid, cmdline);
|
||||||
} else {
|
} else {
|
||||||
addjob(jobs, pid, FG, cmdline);
|
// waitfg(pid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,9 +300,9 @@ void do_bgfg(char **argv)
|
||||||
*/
|
*/
|
||||||
void waitfg(pid_t pid)
|
void waitfg(pid_t pid)
|
||||||
{
|
{
|
||||||
// while (pid == fgpid(jobs)) {
|
while (pid == fgpid(jobs)) {
|
||||||
// sleep(1);
|
sleep(1);
|
||||||
// }
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue