system.c 595 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "lib.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. int
  7. system(const char *s)
  8. {
  9. int w, status;
  10. pid_t pid;
  11. char cmd[30], *oty;
  12. oty = getenv("objtype");
  13. if(!oty)
  14. return -1;
  15. if(!s)
  16. return 1; /* a command interpreter is available */
  17. pid = fork();
  18. snprintf(cmd, sizeof cmd, "/%s/bin/ape/sh", oty);
  19. if(pid == 0) {
  20. execl(cmd, "sh", "-c", s, 0);
  21. _exit(1);
  22. }
  23. if(pid < 0){
  24. _syserrno();
  25. return -1;
  26. }
  27. for(;;) {
  28. w = wait(&status);
  29. if(w == -1 || w == pid)
  30. break;
  31. }
  32. if(w == -1){
  33. _syserrno();
  34. return w;
  35. }
  36. return status;
  37. }