Advertisement
Guest User

subS

a guest
Mar 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.50 KB | None | 0 0
  1. `[^'\n\t][^'\n\t]*` {
  2.         int cmd_pipe[2],out_pipe[2];
  3.         pid_t pid;
  4.          int j=0;  
  5.          char array[strlen(yytext)+7];
  6.         char * cmd = strdup(yytext);
  7.         cmd++;
  8.         cmd[strlen(cmd)-1] = '\0';
  9.             int one = dup(0);
  10.             int two = dup(1);
  11.         if ((pipe(cmd_pipe) <0) || (pipe(out_pipe) <0)) {
  12.         perror("pipe");
  13.         exit(1);
  14.        }    
  15.      
  16.             dup2(cmd_pipe[0],0);
  17.             dup2(out_pipe[1],1);
  18.             write(out_pipe[1],cmd,strlen(cmd));
  19.             write(out_pipe[1],"\nexit\n",6);
  20.             close(out_pipe[1]);
  21.             close(cmd_pipe[1]);
  22.             close(cmd_pipe[0]);
  23.             pid = fork();
  24.             if (pid < 0) {
  25.             perror("fork");
  26.             exit(1);
  27.  
  28.   }
  29.      else if (pid ==0) {  
  30.             execlp("/proc/self/exe","/proc/self/exe",NULL);
  31.            _exit(1);
  32.          }
  33.       else if (pid > 0) {
  34.              waitpid(pid,NULL,0);
  35.             char* str =(char*) malloc((SUB_SIZE+1)*sizeof(char));
  36.            char c;
  37.            int i=0;
  38.           while(read(out_pipe[0],&c,1)) {
  39.             if (c == '\n'){
  40.             str[i++]= ' ' ;
  41.             }
  42.             else {
  43.             str[i++] = c;
  44.             }
  45.          }
  46.          str[i] = '\0';
  47.             while (i-- != 0) {
  48.              unput(str[i]);
  49.          }  
  50.             dup2(one,0);
  51.             dup2(two,0);
  52.             close(one);
  53.             close(two);
  54.             close(out_pipe[0]);              
  55.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement