Guest User

Untitled

a guest
May 29th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. //I create a pipe in which i switch stdout by its writing side so i can get the result of execvp.
  2. if(pipe(exec2reduce) == -1) {
  3.   perror("pipe");
  4.   return -1;
  5. }
  6.  
  7. if(dup2(exec2reduce[1],1) == -1) {
  8.   perror("dup2");
  9.   return -1;
  10. }
  11.  
  12. //I create a new process using fork to run execvp but the parent waits idefinitely.
  13. pid = fork();
  14.    
  15. if(pid == -1) {
  16.   perror("fork");
  17.   return -1;
  18. }
  19.    
  20. if(pid == 0) {        
  21.   if(execvp(command,args) == -1) {
  22.     perror("execvp");
  23.     _exit(EXIT_FAILURE);
  24.   }
  25.     _exit(EXIT_SUCCESS);
  26. }
  27.  
  28. wait(&retvar);
  29. if(!WIFEXITED(retvar)) {
  30.   return -1;
  31. }
  32. else {
  33.   if(WEXITSTATUS(retvar) == EXIT_FAILURE) {
  34.     return -1;
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment