system.c 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. int system(command)
  7. char *command;
  8. {
  9. int wait_val, pid;
  10. __sighandler_t save_quit, save_int, save_chld;
  11. if (command == 0)
  12. return 1;
  13. save_quit = signal(SIGQUIT, SIG_IGN);
  14. save_int = signal(SIGINT, SIG_IGN);
  15. save_chld = signal(SIGCHLD, SIG_DFL);
  16. if ((pid = vfork()) < 0) {
  17. signal(SIGQUIT, save_quit);
  18. signal(SIGINT, save_int);
  19. signal(SIGCHLD, save_chld);
  20. return -1;
  21. }
  22. if (pid == 0) {
  23. signal(SIGQUIT, SIG_DFL);
  24. signal(SIGINT, SIG_DFL);
  25. signal(SIGCHLD, SIG_DFL);
  26. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  27. _exit(127);
  28. }
  29. /* Signals are not absolutly guarenteed with vfork */
  30. signal(SIGQUIT, SIG_IGN);
  31. signal(SIGINT, SIG_IGN);
  32. #if 0
  33. printf("Waiting for child %d\n", pid);
  34. #endif
  35. if (wait4(pid, &wait_val, 0, 0) == -1)
  36. wait_val = -1;
  37. signal(SIGQUIT, save_quit);
  38. signal(SIGINT, save_int);
  39. signal(SIGCHLD, save_chld);
  40. return wait_val;
  41. }