Commit | Line | Data |
---|---|---|
82492b51 KB |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
3 | * All rights reserved. | |
39feba54 | 4 | * |
ad787160 C |
5 | * This module is believed to contain source code proprietary to AT&T. |
6 | * Use and redistribution is subject to the Berkeley Software License | |
7 | * Agreement and your Software Agreement with AT&T (Western Electric). | |
161423a6 RE |
8 | */ |
9 | ||
82492b51 | 10 | #ifndef lint |
ad787160 | 11 | static char sccsid[] = "@(#)system_.c 5.4 (Berkeley) 4/12/91"; |
82492b51 KB |
12 | #endif /* not lint */ |
13 | ||
161423a6 | 14 | /* |
39feba54 DW |
15 | * execute a unix command |
16 | * | |
17 | * calling sequence: | |
18 | * iexit = system(command) | |
19 | * where: | |
20 | * iexit will return the exit status of the command | |
21 | * command is a character string containing the command to be executed | |
22 | */ | |
23 | ||
1ab95451 | 24 | #include "../libI77/fiodefs.h" |
a05d0553 | 25 | #include "../libI77/f_errno.h" |
94880913 DW |
26 | #include <sys/param.h> |
27 | #ifndef NCARGS | |
28 | #define NCARGS 256 | |
29 | #endif | |
1ab95451 DW |
30 | |
31 | ||
39feba54 DW |
32 | long system_(s, n) |
33 | char *s; | |
34 | long n; | |
35 | { | |
94880913 | 36 | char buf[NCARGS - 50]; |
f1a9026b | 37 | long i; |
1ab95451 | 38 | |
a05d0553 DW |
39 | if (n >= sizeof buf) |
40 | return(-(long)(errno=F_ERARG)); | |
f1a9026b DW |
41 | for (i = 0; i < MXUNIT; i++) |
42 | flush_(&i); | |
a05d0553 DW |
43 | g_char(s, n, buf); |
44 | return((long)system(buf)); | |
39feba54 | 45 | } |
94880913 DW |
46 | |
47 | /* | |
4d65594d | 48 | * this is a sane version of the libc routine. |
94880913 DW |
49 | */ |
50 | ||
4d65594d KB |
51 | #include <sys/signal.h> |
52 | #include <stdlib.h> | |
53 | #include <string.h> | |
94880913 DW |
54 | |
55 | system(s) | |
56 | char *s; | |
57 | { | |
999013f3 | 58 | register sig_t istat, qstat; |
94880913 | 59 | int status, pid, w; |
4d65594d | 60 | char *shname, *shell; |
94880913 DW |
61 | |
62 | if ((shell = getenv("SHELL")) == NULL) | |
63 | shell = "/bin/sh"; | |
64 | ||
65 | if (shname = rindex(shell, '/')) | |
66 | shname++; | |
67 | else | |
68 | shname = shell; | |
69 | ||
4d65594d KB |
70 | if ((pid = vfork()) == 0) { |
71 | execl(shell, shname, "-c", s, (char *)0); | |
94880913 DW |
72 | _exit(127); |
73 | } | |
4d65594d KB |
74 | if (pid == -1) |
75 | return(-1); | |
76 | ||
94880913 DW |
77 | istat = signal(SIGINT, SIG_IGN); |
78 | qstat = signal(SIGQUIT, SIG_IGN); | |
79 | while ((w = wait(&status)) != pid && w != -1) | |
80 | ; | |
81 | if (w == -1) | |
82 | status = -1; | |
999013f3 KB |
83 | (void)signal(SIGINT, istat); |
84 | (void)signal(SIGQUIT, qstat); | |
94880913 DW |
85 | return(status); |
86 | } |