The_Law

Untitled

Nov 20th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4.  
  5. enum
  6. {
  7.     EXIT_CODE = 128
  8. };
  9.  
  10. int
  11. mysys(const char *str)
  12. {
  13.     int pid;
  14.     if ((pid = fork()) < 0) {
  15.         return -1;
  16.     } else if (!pid) {
  17.         execlp("bin/sh", "sh", "-c", str, NULL);
  18.         _exit(EXIT_CODE - 1);
  19.     } else {
  20.         int sg;
  21.         waitpid(pid, &sg, 0);
  22.         _Bool fl = sg == EXIT_CODE - 1;
  23.         if (fl) {
  24.             return sg;
  25.         }
  26.         fl = WIFSIGNALED(sg) != 0;
  27.         if (fl) {
  28.             return sg + EXIT_CODE;
  29.         }
  30.         return WEXITSTATUS(sg);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment