Home try to run ls - l | sort -u > output.txt
Reply: 0

try to run ls - l | sort -u > output.txt

user3094
1#
user3094 Published in April 26, 2018, 5:37 am

First i call execArgsSh4(). The second child it will execute the second part. But the problem is that the k.txt is null until i press control+D (EOF). If i run the second part by the parent and not with p2, it will be executed perfect but my program stops. Any help with with will be appreciated.

void execArgSh4(char * cmd[]){
       int i=0;
       int j=0; 
       char flag='n';
        int k=0;
        char * buf[MAXCOM];
        char * buf1[MAXCOM];
       while(cmd[i]!=NULL){

    if(strcmp(cmd[i],"|")==0){
        i++;
        flag='y';
        continue;
    }
    if(flag=='n'){ //read for first buffer
        buf[j]=cmd[i];  
        j++;
    }else{       //read for seccond buffer
        buf1[k]=cmd[i];
        k++;
    }
    i++;
}


 buf[j]=NULL;
    buf1[k]=NULL;
// 0 is read end, 1 is write end


int pipefd[2]; 
pid_t p1;

if (pipe(pipefd)==-1) {
//die("pipe()");
    printf("\nPipe could not be initialized");
    return;
}
p1 = fork(); //forking 1st child for the first process
if (p1==-1) {
    printf("\nCould not fork");
    return;
}


if (p1 == 0) {
    // Child 1 executing..
    // It only needs to write at the write end
    close(pipefd[0]);
    dup2(pipefd[1], STDOUT_FILENO); //redirect standard output to pipefd[1]
    close(pipefd[1]);

    if (execvp(buf[0],buf) < 0) {
        printf("\nCould not execute command 1..\n");
        exit(0);
    }
} else {
       p2 =fork();
       if(p2<0){
          printf("\nCould not fork");
          return;}
       else if(p2==0){
            i=0;
            j=0; 
            char flag='n';
            k=0;
           char * buff[MAXCOM];  //first 
           char * buff1[MAXCOM];  // second 
           while(buf1[i]!=NULL){

                  if(strcmp(buf1[i],">")==0){
                    i++;
                    flag='y';
                     continue;
                }else if( strcmp(buf1[i],"<")==0){
                        i++;    
                        continue;
                }else{
                    if(flag=='n'){
                       buff[j]=buf1[i];    
                       j++;
                  }else{
                       buff1[k]=buf1[i];

                        k++;
                      }
                 }
                i++;
  }


    buff[j]=NULL;
   ` buff1[k]=NULL;

     `  close(pipefd[1]);
        dup2(pipefd[0], STDIN_FILENO);
        close(pipefd[0]);

int file;

FILE * fp = fopen(buff1[0],"wb"); //creates new file with buf1[0] name
file = open( buff1[0], O_APPEND | O_WRONLY, 0777 );//open file as new                                   standar                                     input
if( file == -1 ) {//fail to open file
    perror(buff1[0]);
    return EXIT_FAILURE;
}

fclose(stdout);
dup2( file, STDOUT_FILENO);//now we redirect standar output to the                  file    using dup2
if (execvp(buff[0],buff)<0){
    printf("\nCould not execute command3");
    exit(0);
}

return;

}else {

        wait(NULL);
}

     }
  }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314011 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO