Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <linux/types.h>
  8. #include <sys/wait.h>
  9. #include <fcntl.h>
  10. void inthandler(int dummy)
  11. {
  12. signal(SIGINT, inthandler);  
  13. }
  14.  
  15. int main ()
  16. {
  17. //char PATH="/bin";
  18. char komenda[1000];
  19. char *wyraz;
  20. char *wyrazy[100];  
  21.     while(1)
  22.     {
  23.  
  24.  
  25.         signal(SIGINT,inthandler);
  26.         signal(SIGQUIT, SIG_IGN);
  27.         signal(SIGTSTP, SIG_IGN);
  28.         signal(SIGTTIN, SIG_IGN);
  29.         signal(SIGTTOU, SIG_IGN);
  30.         signal(SIGCHLD, SIG_IGN);
  31.  
  32.          
  33.         if(fgets(komenda,1000,stdin)==NULL)
  34.         exit(0);
  35.  
  36.         size_t length = strlen(komenda);
  37.         if(komenda[length-1] == '\n')
  38.         komenda[length -1] = '\0' ;
  39.        
  40.         int k=0;
  41.         int i=0;
  42.         wyraz=strtok(komenda, " ");
  43.         while(wyraz!=NULL)
  44.         {
  45.  
  46.             if(strcmp(wyraz,"|")==0)
  47.             {
  48.                 wyrazy[i] = "NULL";
  49.                 wyraz=strtok(NULL, " ");
  50.                 i++;
  51.                 k++;
  52.                 continue;  
  53.                  
  54.             }
  55.  
  56.             wyrazy[i]=wyraz;
  57.             wyraz=strtok(NULL, " ");
  58.             i++;  
  59.             }
  60. i--;
  61.  
  62.  
  63.         //wyrazy[i] ="NULL";
  64.  
  65.    
  66.              
  67.         if(strncmp(komenda,"exit",4) == 0)
  68.             exit(0);
  69.  
  70.         else if(strncmp(komenda,"cd",2) == 0 )
  71.             {int l; l=chdir(wyrazy[1]);if(l!=0) perror("błąd:"); }
  72.  
  73.         else if (k==0)
  74.         {
  75.             int t1;
  76.             pid_t pid = fork();
  77.             if(pid==0)
  78.             {
  79.                 if((execvp(wyrazy[0], wyrazy)==-1))
  80.                 {
  81.                     perror("blad ");
  82.                 }
  83.             }
  84.             else if(pid>0)
  85.             {
  86.                 if(wyrazy[i-1]=="&")
  87.                 wait(&t1);
  88.             }
  89.             else waitpid(pid,0,0);
  90.         }
  91.  
  92.         else if (k>0)
  93.         {
  94.             int we=0;
  95.             int wy=0;
  96.             char *wyjscie[100];
  97.             char *wejscie[100];
  98.             int j=0;
  99.  
  100.             while(strcmp(wyrazy[j],"NULL")!=0)
  101.             {  
  102.                 wyjscie[wy]=wyrazy[j];
  103.                 j++;
  104.                 wy++;
  105.             }
  106.  
  107.             //wyjscie[wy]="NULL";
  108.             j++;
  109.  
  110.  
  111.             //while(strcmp(wyrazy[j],"NULL")!=0)
  112.             while(we<i)                
  113.             {
  114.                 wejscie[we]=wyrazy[j];
  115.                 we++;
  116.                 j++;
  117.             }  
  118.             //  if(j<i)            
  119.             //  j++;
  120.             //  if(j>i) break;                     
  121.         //  }
  122.            
  123.         //  wejscie[we]="NULL";
  124.            
  125.                        for(int y=0; y<we; y++)
  126.             printf("%s", wejscie[y]);
  127.            
  128.             int pipefd[2];
  129.             int t1;
  130.             int t2;
  131.             int p;
  132.            
  133.             p = pipe(pipefd);
  134.             if(p<0)
  135.             {perror("e"); exit(1); }
  136.            
  137.             if(fork() == 0)
  138.             {
  139.  
  140.                 close(STDOUT_FILENO);
  141.                 //if(dup2(pipefd[1],1)!=1)
  142.                 //{}
  143.                 dup(pipefd[1]);
  144.                 close(pipefd[1]);
  145.                 close(pipefd[0]);
  146.            
  147.                 execvp(wyjscie[0], wyjscie);
  148.                 perror("e"); exit(1);
  149.  
  150.             }
  151.             else if (fork()==0)
  152.             {
  153.                 close(STDIN_FILENO);
  154.                 //if(dup2(pipefd[0],0)!=0)
  155.                 //{}
  156.                 dup(pipefd[0]);  
  157.                 close(pipefd[1]);
  158.                 close(pipefd[0]);
  159.            
  160.                 execvp(wejscie[0], wejscie);
  161.                 perror("e"); exit(1);
  162.  
  163.             }
  164.             //else
  165.             //{
  166.                 close(pipefd[0]);
  167.                 close(pipefd[1]);
  168.                 wait(0);
  169.                 wait(0);
  170.  
  171.             //  if(WEXITSTATUS(t1) || WEXITSTATUS(t2)) {}
  172.             //}
  173.  
  174.  
  175.  
  176.  
  177.                
  178.         }
  179.  
  180.  
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement