Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <sys/wait.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int main(int argc, char *argv[])
  6. {
  7.  pid_t childpid;
  8.  int status, exit_code, x;
  9.  exit_code = EXIT_SUCCESS;
  10.  if (argc < 2) {
  11.     printf(“Usage: %s command args\n“, argv[0]);
  12.     exit_code = EXIT_FAILURE;
  13.     }else{
  14.         switch (childpid = fork()){
  15.             case -1:
  16.                 perror("Could not fork\n");
  17.                 exit_code = EXIT_FAILURE;
  18.                 break;
  19.             case 0:
  20.                 if (execvp(argv[1], &argv[1]) < 0){
  21.                     perror(“Could not execute the command\n”);
  22.                     exit_code = EXIT_FAILURE;
  23.                     break;
  24.                 } default:
  25.                     if ((x = wait(&status)) != childpid) {
  26.                         perror(“Wait interrupted by a signal\n”);
  27.                         exit_code = EXIT_FAILURE;
  28.                     }
  29.             } // end switch
  30.         } // end else
  31.     exit(exit_code);
  32. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement