Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <unistd.h>
- #include <linux/types.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- void inthandler(int dummy)
- {
- signal(SIGINT, inthandler);
- }
- int main ()
- {
- //char PATH="/bin";
- char komenda[1000];
- char *wyraz;
- char *wyrazy[100];
- while(1)
- {
- signal(SIGINT,inthandler);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGTTOU, SIG_IGN);
- signal(SIGCHLD, SIG_IGN);
- if(fgets(komenda,1000,stdin)==NULL)
- exit(0);
- size_t length = strlen(komenda);
- if(komenda[length-1] == '\n')
- komenda[length -1] = '\0' ;
- int k=0;
- int i=0;
- wyraz=strtok(komenda, " ");
- while(wyraz!=NULL)
- {
- if(strcmp(wyraz,"|")==0)
- {
- wyrazy[i] = "NULL";
- wyraz=strtok(NULL, " ");
- i++;
- k++;
- continue;
- }
- wyrazy[i]=wyraz;
- wyraz=strtok(NULL, " ");
- i++;
- }
- i--;
- //wyrazy[i] ="NULL";
- if(strncmp(komenda,"exit",4) == 0)
- exit(0);
- else if(strncmp(komenda,"cd",2) == 0 )
- {int l; l=chdir(wyrazy[1]);if(l!=0) perror("bÅÄ d:"); }
- else if (k==0)
- {
- int t1;
- pid_t pid = fork();
- if(pid==0)
- {
- if((execvp(wyrazy[0], wyrazy)==-1))
- {
- perror("blad ");
- }
- }
- else if(pid>0)
- {
- if(wyrazy[i-1]=="&")
- wait(&t1);
- }
- else waitpid(pid,0,0);
- }
- else if (k>0)
- {
- int we=0;
- int wy=0;
- char *wyjscie[100];
- char *wejscie[100];
- int j=0;
- while(strcmp(wyrazy[j],"NULL")!=0)
- {
- wyjscie[wy]=wyrazy[j];
- j++;
- wy++;
- }
- //wyjscie[wy]="NULL";
- j++;
- //while(strcmp(wyrazy[j],"NULL")!=0)
- while(we<i)
- {
- wejscie[we]=wyrazy[j];
- we++;
- j++;
- }
- // if(j<i)
- // j++;
- // if(j>i) break;
- // }
- // wejscie[we]="NULL";
- for(int y=0; y<we; y++)
- printf("%s", wejscie[y]);
- int pipefd[2];
- int t1;
- int t2;
- int p;
- p = pipe(pipefd);
- if(p<0)
- {perror("e"); exit(1); }
- if(fork() == 0)
- {
- close(STDOUT_FILENO);
- //if(dup2(pipefd[1],1)!=1)
- //{}
- dup(pipefd[1]);
- close(pipefd[1]);
- close(pipefd[0]);
- execvp(wyjscie[0], wyjscie);
- perror("e"); exit(1);
- }
- else if (fork()==0)
- {
- close(STDIN_FILENO);
- //if(dup2(pipefd[0],0)!=0)
- //{}
- dup(pipefd[0]);
- close(pipefd[1]);
- close(pipefd[0]);
- execvp(wejscie[0], wejscie);
- perror("e"); exit(1);
- }
- //else
- //{
- close(pipefd[0]);
- close(pipefd[1]);
- wait(0);
- wait(0);
- // if(WEXITSTATUS(t1) || WEXITSTATUS(t2)) {}
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement