Added error exit on wrong command and prepared waitfg

main
TuDatTr 2021-02-04 17:33:59 +01:00
parent 1540d090d7
commit 5f0ea2601b
1 changed files with 21 additions and 19 deletions

40
tsh.c
View File

@ -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 (!builtin_cmd(argv)) { // if cmdline is not a builtin
int pid = fork(); // fork process if (argv[0] != NULL) { // Do nothing, but don't crash on empty line
if (pid == 0) { // Child Process if (!builtin_cmd(argv)) { // if cmdline is not a builtin
if (execve(argv[0], argv, environ) != 0) { // if exec failed int pid = fork(); // fork process
printf("Command not found.\n"); if (pid == 0) { // Child Process
exit(0); if (execve(argv[0], argv, environ) != 0) { // if exec failed
} printf("%s Command not found.\n", argv[0]);
exit(0); exit(1);
} else { // Parent Process }
wait(NULL); } else { // Parent Process
if (bg) { wait(NULL);
addjob(jobs, pid, BG, cmdline); addjob(jobs, pid, bg ? BG : FG, cmdline);
job = getjobpid(jobs, pid); if (bg) {
printf("[%d] (%d) %s", job->jid, pid, cmdline); job = getjobpid(jobs, pid);
} else { printf("[%d] (%d) %s", job->jid, pid, cmdline);
addjob(jobs, pid, FG, cmdline); } else {
// 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;
} }