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){
free(temp);
}
void ioRedirect(char **tokens,int n){
FILE *fp;
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
......@@ -69,29 +68,29 @@ void ioRedirect(char **tokens,int n){
for(int i=0;strcmp(tokens[i],">>") && strcmp(tokens[i],">") && strcmp(tokens[i],"<");i++){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
switch (fork()) {
case -1:
break;
case 0:
if(!strcmp(tokens[n-2],">>"))
fp = freopen(tokens[n-1],"a",stdout);
else if(!strcmp(tokens[n-2],">"))
fp = freopen(tokens[n-1],"w",stdout);
else
fp = freopen(tokens[n-1],"r",stdin);
strcpy(temp,"/bin/");
switch (fork()) {
case -1:
break;
case 0:
if(!strcmp(tokens[n-2],">>"))
fp = freopen(tokens[n-1],"a",stdout);
else if(!strcmp(tokens[n-2],">"))
fp = freopen(tokens[n-1],"w",stdout);
else
fp = freopen(tokens[n-1],"r",stdin);
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
}
free(temp);
}
execv(temp,arglist);
}
free(temp);
}
wait(NULL);
for(int i=0;arglist[i]!=NULL;i++)
......@@ -100,9 +99,26 @@ void ioRedirect(char **tokens,int n){
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){
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0,forkcalls=0;
int j=0;
for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&")!=0 ){
......@@ -110,8 +126,8 @@ void exBackground(char **tokens,int n){
strcpy(arglist[j++],tokens[i++]);
}
arglist[j]=NULL;
forkcalls++;
char *temp = (char *)malloc(sizeof(char)*150);
signal(SIGCHLD, handler);
switch (fork()) {
case -1:
......@@ -283,19 +299,16 @@ int main(int argc, char* argv[]) {
line[strlen(line) - 1] = '\0';
} else { // interactive mode
funcPwd();
// waitpid(-1,NULL,WNOHANG);
scanf("%[^\n]", line);
getchar();
}
if(!strlen(line))
continue;
// printf("Command entered: %s\n", line);
/* END: TAKING INPUT */
line[strlen(line)] = '\n'; //terminate with new line
tokens = tokenize(line);
//do whatever you want with the commands, here we just print them
for(i=0;tokens[i]!=NULL;i++){
if(!parallel)
parallel=strcmp(tokens[i],"&&&")==0;
......@@ -309,7 +322,6 @@ int main(int argc, char* argv[]) {
if(!io)
io=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0;
// printf("found token %s\n", tokens[i]);
}
t1 = clock();
if(parallel)
......@@ -321,7 +333,7 @@ int main(int argc, char* argv[]) {
else if(io)
ioRedirect(tokens,i);
else {
if(!strcmp(tokens[0],"dir")){
if(!strcmp(tokens[0],"dir") && i==2){
funcDir(tokens);
}
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