system.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. #include <stdlib.h>
  7. #include <signal.h>
  8. extern int fork(void);
  9. extern int wait(int *);
  10. extern void _exit(int);
  11. extern void execl(char *, ...);
  12. extern void close(int);
  13. #define FAIL 127
  14. int
  15. system(const char *str)
  16. {
  17. int pid, exitstatus, waitval;
  18. int i;
  19. if ((pid = fork()) < 0) return str ? -1 : 0;
  20. if (pid == 0) {
  21. for (i = 3; i <= 20; i++)
  22. close(i);
  23. if (!str) str = "cd ."; /* just testing for a shell */
  24. execl("/bin/sh", "sh", "-c", str, (char *) NULL);
  25. /* get here if execl fails ... */
  26. _exit(FAIL); /* see manual page */
  27. }
  28. while ((waitval = wait(&exitstatus)) != pid) {
  29. if (waitval == -1) break;
  30. }
  31. if (waitval == -1) {
  32. /* no child ??? or maybe interrupted ??? */
  33. exitstatus = -1;
  34. }
  35. if (!str) {
  36. if (exitstatus == FAIL << 8) /* execl() failed */
  37. exitstatus = 0;
  38. else exitstatus = 1; /* /bin/sh exists */
  39. }
  40. return exitstatus;
  41. }