[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / lib / cbits / system.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[system.lc]{system Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_WAIT_H
12 #include <sys/wait.h>
13 #endif
14
15 #ifdef HAVE_VFORK_H
16 #include <vfork.h>
17 #endif
18
19 #ifdef HAVE_VFORK
20 #define fork vfork
21 #endif
22
23 StgInt
24 systemCmd(cmd)
25 StgByteArray cmd;
26 {
27     int pid;
28     int wstat;
29
30     switch(pid = fork()) {
31     case -1:
32         if (errno != EINTR) {
33             cvtErrno();
34             stdErrno();
35             return -1;
36         }
37     case 0:
38         /* the child */
39         execl("/bin/sh", "sh", "-c", cmd, NULL);
40         _exit(127);
41     }
42
43     while (waitpid(pid, &wstat, 0) < 0) {
44         if (errno != EINTR) {
45             cvtErrno();
46             stdErrno();
47             return -1;
48         }
49     }
50
51     if (WIFEXITED(wstat))
52         return WEXITSTATUS(wstat);
53     else if (WIFSIGNALED(wstat)) {
54         ghc_errtype = ERR_INTERRUPTED;
55         ghc_errstr = "system command interrupted";
56     }
57     else {
58         /* This should never happen */
59         ghc_errtype = ERR_OTHERERROR;
60         ghc_errstr = "internal error (process neither exited nor signalled)";
61     }
62     return -1;
63 }
64
65 \end{code}