home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 10 / Fresh_Fish_10_2352.bin / new / dev / lang / sgmls / src / unixproc.c < prev    next >
C/C++ Source or Header  |  1994-07-10  |  2KB  |  99 lines

  1. /* unixproc.c -
  2.  
  3.    Unix implementation of run_process().
  4.  
  5.      Written by James Clark (jjc@jclark.com).
  6. */
  7.  
  8. #include "config.h"
  9.  
  10. #ifdef SUPPORT_SUBDOC
  11.  
  12. #ifdef POSIX
  13.  
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17.  
  18. #endif /* POSIX */
  19.  
  20. #include "std.h"
  21. #include "entity.h"
  22. #include "appl.h"
  23.  
  24. #ifndef POSIX
  25.  
  26. #define WIFSTOPPED(s) (((s) & 0377) == 0177)
  27. #define WIFSIGNALED(s) (((s) & 0377) != 0 && ((s) & 0377 != 0177))
  28. #define WIFEXITED(s) (((s) & 0377) == 0)
  29. #define WEXITSTATUS(s) (((s) >> 8) & 0377)
  30. #define WTERMSIG(s) ((s) & 0177)
  31. #define WSTOPSIG(s) (((s) >> 8) & 0377)
  32. #define _SC_OPEN_MAX 0
  33. #define sysconf(name) (20)
  34. typedef int pid_t;
  35.  
  36. #endif /* not POSIX */
  37.  
  38. #ifndef HAVE_VFORK
  39. #define vfork() fork()
  40. #endif /* not HAVE_VFORK */
  41.  
  42. #ifdef HAVE_VFORK_H
  43. #include <vfork.h>
  44. #endif /* HAVE_VFORK_H */
  45.  
  46. int run_process(argv)
  47. char **argv;
  48. {
  49.      pid_t pid;
  50.      int status;
  51.      int ret;
  52.  
  53.      /* Can't trust Unix implementations to support fflush(NULL). */
  54.      fflush(stderr);
  55.      fflush(stdout);
  56.  
  57.      pid = vfork();
  58.      if (pid == 0) {
  59.       /* child */
  60.       int i;
  61.       int open_max = (int)sysconf(_SC_OPEN_MAX);
  62.  
  63.       for (i = 3; i < open_max; i++)
  64.            (void)close(i);
  65.       execvp(argv[0], argv);
  66.       appl_error(E_EXEC, argv[0], strerror(errno));
  67.       fflush(stderr);
  68.       _exit(127);
  69.      }
  70.      if (pid < 0) {
  71.       appl_error(E_FORK, strerror(errno));
  72.       return -1;
  73.      }
  74.      /* parent */
  75.      while ((ret = wait(&status)) != pid)
  76.       if (ret < 0) {
  77.            appl_error(E_WAIT, strerror(errno));
  78.            return -1;
  79.       }
  80.      if (WIFSIGNALED(status)) {
  81.       appl_error(E_SIGNAL, argv[0], WTERMSIG(status));
  82.       return -1;
  83.      }
  84.      /* Must have exited normally. */
  85.      return WEXITSTATUS(status);
  86. }
  87.  
  88. #endif /* SUPPORT_SUBDOC */
  89.  
  90. /*
  91. Local Variables:
  92. c-indent-level: 5
  93. c-continued-statement-offset: 5
  94. c-brace-offset: -5
  95. c-argdecl-indent: 0
  96. c-label-offset: -5
  97. End:
  98. */
  99.