diff --git a/tsh.c b/tsh.c index 5cadbac..8166269 100644 --- a/tsh.c +++ b/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 (!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); - } - exit(0); - } else { // Parent Process - wait(NULL); - 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); + + 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("%s Command not found.\n", argv[0]); + exit(1); + } + } else { // Parent Process + wait(NULL); + addjob(jobs, pid, bg ? BG : FG, cmdline); + if (bg) { + job = getjobpid(jobs, pid); + printf("[%d] (%d) %s", job->jid, pid, cmdline); + } else { + // 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; }