xref: /original-bsd/usr.bin/f77/libU77/system_.c (revision 4b2c5e10)
1 /*
2 char id_system[] = "@(#)system_.c	1.5";
3  *
4  * execute a unix command
5  *
6  * calling sequence:
7  *	iexit = system(command)
8  * where:
9  *	iexit will return the exit status of the command
10  *	command is a character string containing the command to be executed
11  */
12 
13 #include	"../libI77/fiodefs.h"
14 #include	"../libI77/f_errno.h"
15 #include <sys/param.h>
16 #ifndef	NCARGS
17 #define NCARGS	256
18 #endif
19 
20 
21 long system_(s, n)
22 char *s;
23 long n;
24 {
25 	char buf[NCARGS - 50];
26 	long i;
27 
28 	if (n >= sizeof buf)
29 		return(-(long)(errno=F_ERARG));
30 	for (i = 0; i < MXUNIT; i++)
31 		flush_(&i);
32 	g_char(s, n, buf);
33 	return((long)system(buf));
34 }
35 
36 /*
37  * this is a sane version of the libc/stdio routine.
38  */
39 
40 #include	<signal.h>
41 
42 char	*getenv();
43 char	*rindex();
44 
45 system(s)
46 char *s;
47 {
48 	register int (*istat)(), (*qstat)();
49 	int status, pid, w;
50 	char	*shname, *shell;
51 
52 	if ((shell = getenv("SHELL")) == NULL)
53 		shell = "/bin/sh";
54 
55 	if (shname = rindex(shell, '/'))
56 		shname++;
57 	else
58 		shname = shell;
59 
60 	if ((pid = fork()) == 0) {
61 		execl(shell, shname, "-c", s, 0);
62 		_exit(127);
63 	}
64 	istat = signal(SIGINT, SIG_IGN);
65 	qstat = signal(SIGQUIT, SIG_IGN);
66 	while ((w = wait(&status)) != pid && w != -1)
67 		;
68 	if (w == -1)
69 		status = -1;
70 	signal(SIGINT, istat);
71 	signal(SIGQUIT, qstat);
72 	return(status);
73 }
74