From bc791fe9272172d3d3d19a5bf59d5abf77f533d3 Mon Sep 17 00:00:00 2001
From: Jung June Pyo <forjune@ajou.ac.kr>
Date: Tue, 8 May 2018 21:12:14 +0900
Subject: [PATCH] update

---
 src/commands.c        | 40 +++++++++++++++++++++++++++++++++++++++-
 src/main.c            |  6 +++++-
 src/signal_handlers.c |  2 ++
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/commands.c b/src/commands.c
index 13e9c33..6ae328d 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -50,9 +50,46 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
     } else if (strcmp(com->argv[0], "exit") == 0) {
       return 1;
     } else {
+	int back=0;
+	if(strcmp(com->argv[com->argc-1],"&")==0)
+	{	
+		back=1;
+		com->argv[com->argc-1]=NULL;
+		(com->argc)--;
+	}
+
+	pid_t pid;
+	pid=fork();
+	char temp[100];
+	char pr[5][30]={{"/usr/local/bin/"},{"/usr/bin/"},{"/bin/"},{"/usr/sbin 	"},{"/sbin/"}};
+	
+	if(pid<0)
+	{
+		fprintf(stderr,"fork failed!");
+		return -1;
+	}
+	else if(pid==0)
+	{
+		if(execv(com->argv[0],com->argv)==-1){
+		for(int i=0;i<5;i++)
+		{
+			strcpy(temp,pr[i]);
+			strcat(temp,com->argv[0]);
+			execv(temp,com->argv);
+		}
+	}
+
       fprintf(stderr, "%s: command not found\n", com->argv[0]);
-      return -1;
+      exit(0);
     }
+	else{
+		if(back==1){}
+		else{
+			wait(NULL);
+			return 0;
+		}
+}
+}
   }
 
   return 0;
@@ -73,4 +110,5 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
   }
 
   memset((*commands), 0, sizeof(struct single_command) * n_commands);
+
 }
diff --git a/src/main.c b/src/main.c
index 77c7804..a0d37b1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,16 +1,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
+#include <signal.h>
 #include "commands.h"
 #include "built_in.h"
 #include "utils.h"
+#include "signal_handlers.h"
 
 int main()
 {
   char buf[8096];
+	signal(SIGINT,(void*)catch_sigint);
+	signal(SIGTSTP,(void*)catch_sigtstp);
 
   while (1) {
+	buf[0]=0;
     fgets(buf, 8096, stdin);
 
     struct single_command commands[512];
diff --git a/src/signal_handlers.c b/src/signal_handlers.c
index 4b6fe2e..8222096 100644
--- a/src/signal_handlers.c
+++ b/src/signal_handlers.c
@@ -3,9 +3,11 @@
 void catch_sigint(int signalNo)
 {
   // TODO: File this!
+	signal(SIGINT,SIG_IGN);
 }
 
 void catch_sigtstp(int signalNo)
 {
   // TODO: File this!
+	signal(SIGTSTP,SIG_IGN);
 }
-- 
GitLab