Commit dda893a2 authored by desiredeveloper's avatar desiredeveloper

almost final

parent 9e2d48fd
No preview for this file type
...@@ -60,7 +60,6 @@ void funcCd(char *fpath){ ...@@ -60,7 +60,6 @@ void funcCd(char *fpath){
free(temp); free(temp);
} }
void ioRedirect(char **tokens,int n){ void ioRedirect(char **tokens,int n){
FILE *fp; FILE *fp;
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *)); char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
...@@ -69,29 +68,29 @@ void ioRedirect(char **tokens,int n){ ...@@ -69,29 +68,29 @@ void ioRedirect(char **tokens,int n){
for(int i=0;strcmp(tokens[i],">>") && strcmp(tokens[i],">") && strcmp(tokens[i],"<");i++){ for(int i=0;strcmp(tokens[i],">>") && strcmp(tokens[i],">") && strcmp(tokens[i],"<");i++){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char)); arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i]); strcpy(arglist[j++],tokens[i]);
} }
arglist[j]=NULL; arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150); char *temp = (char *)malloc(sizeof(char)*150);
switch (fork()) { switch (fork()) {
case -1: case -1:
break; break;
case 0: case 0:
if(!strcmp(tokens[n-2],">>")) if(!strcmp(tokens[n-2],">>"))
fp = freopen(tokens[n-1],"a",stdout); fp = freopen(tokens[n-1],"a",stdout);
else if(!strcmp(tokens[n-2],">")) else if(!strcmp(tokens[n-2],">"))
fp = freopen(tokens[n-1],"w",stdout); fp = freopen(tokens[n-1],"w",stdout);
else else
fp = freopen(tokens[n-1],"r",stdin); fp = freopen(tokens[n-1],"r",stdin);
strcpy(temp,"/bin/"); strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]); strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){ execv(temp,arglist);
strcpy(temp,"/usr/bin/"); }
strcat(temp,arglist[0]); free(temp);
execv(temp,arglist); }
}
free(temp);
}
wait(NULL); wait(NULL);
for(int i=0;arglist[i]!=NULL;i++) for(int i=0;arglist[i]!=NULL;i++)
...@@ -100,9 +99,26 @@ void ioRedirect(char **tokens,int n){ ...@@ -100,9 +99,26 @@ void ioRedirect(char **tokens,int n){
free(arglist); free(arglist);
} }
// http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal-sigchld-266290/
void handler(int sig){
char *buffer = (char *)malloc(sizeof(char)*100);
char *temp = (char *)malloc(sizeof(char)*150);
pid_t pid;
pid = wait(NULL);
printf("\nBackground process [%d] finished.\n", pid);
signal(SIGCHLD, NULL);
getcwd(temp,150);
sprintf(buffer,"%s:$ ",temp);
write(1,buffer,50);
free(temp);
free(buffer);
}
void exBackground(char **tokens,int n){ void exBackground(char **tokens,int n){
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *)); char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0,forkcalls=0; int j=0;
for(int i=0;i<n;i++){ for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&")!=0 ){ while(tokens[i]!=NULL && strcmp(tokens[i],"&")!=0 ){
...@@ -110,8 +126,8 @@ void exBackground(char **tokens,int n){ ...@@ -110,8 +126,8 @@ void exBackground(char **tokens,int n){
strcpy(arglist[j++],tokens[i++]); strcpy(arglist[j++],tokens[i++]);
} }
arglist[j]=NULL; arglist[j]=NULL;
forkcalls++;
char *temp = (char *)malloc(sizeof(char)*150); char *temp = (char *)malloc(sizeof(char)*150);
signal(SIGCHLD, handler);
switch (fork()) { switch (fork()) {
case -1: case -1:
...@@ -283,19 +299,16 @@ int main(int argc, char* argv[]) { ...@@ -283,19 +299,16 @@ int main(int argc, char* argv[]) {
line[strlen(line) - 1] = '\0'; line[strlen(line) - 1] = '\0';
} else { // interactive mode } else { // interactive mode
funcPwd(); funcPwd();
// waitpid(-1,NULL,WNOHANG);
scanf("%[^\n]", line); scanf("%[^\n]", line);
getchar(); getchar();
} }
if(!strlen(line)) if(!strlen(line))
continue; continue;
// printf("Command entered: %s\n", line);
/* END: TAKING INPUT */ /* END: TAKING INPUT */
line[strlen(line)] = '\n'; //terminate with new line line[strlen(line)] = '\n'; //terminate with new line
tokens = tokenize(line); tokens = tokenize(line);
//do whatever you want with the commands, here we just print them
for(i=0;tokens[i]!=NULL;i++){ for(i=0;tokens[i]!=NULL;i++){
if(!parallel) if(!parallel)
parallel=strcmp(tokens[i],"&&&")==0; parallel=strcmp(tokens[i],"&&&")==0;
...@@ -309,7 +322,6 @@ int main(int argc, char* argv[]) { ...@@ -309,7 +322,6 @@ int main(int argc, char* argv[]) {
if(!io) if(!io)
io=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0; io=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0;
// printf("found token %s\n", tokens[i]);
} }
t1 = clock(); t1 = clock();
if(parallel) if(parallel)
...@@ -321,7 +333,7 @@ int main(int argc, char* argv[]) { ...@@ -321,7 +333,7 @@ int main(int argc, char* argv[]) {
else if(io) else if(io)
ioRedirect(tokens,i); ioRedirect(tokens,i);
else { else {
if(!strcmp(tokens[0],"dir")){ if(!strcmp(tokens[0],"dir") && i==2){
funcDir(tokens); funcDir(tokens);
} }
else if(!strcmp(tokens[0],"clear")){ else if(!strcmp(tokens[0],"clear")){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment