xref: /csrg-svn/usr.bin/f77/libU77/system_.c (revision 12149)
12401Sdlw /*
2*12149Sdlw char id_system[] = "@(#)system_.c	1.5";
32401Sdlw  *
42401Sdlw  * execute a unix command
52401Sdlw  *
62401Sdlw  * calling sequence:
72401Sdlw  *	iexit = system(command)
82401Sdlw  * where:
92401Sdlw  *	iexit will return the exit status of the command
102401Sdlw  *	command is a character string containing the command to be executed
112401Sdlw  */
122401Sdlw 
132440Sdlw #include	"../libI77/fiodefs.h"
142478Sdlw #include	"../libI77/f_errno.h"
15*12149Sdlw #include <sys/param.h>
16*12149Sdlw #ifndef	NCARGS
17*12149Sdlw #define NCARGS	256
18*12149Sdlw #endif
192440Sdlw 
202440Sdlw 
212401Sdlw long system_(s, n)
222401Sdlw char *s;
232401Sdlw long n;
242401Sdlw {
25*12149Sdlw 	char buf[NCARGS - 50];
262535Sdlw 	long i;
272440Sdlw 
282478Sdlw 	if (n >= sizeof buf)
292478Sdlw 		return(-(long)(errno=F_ERARG));
302535Sdlw 	for (i = 0; i < MXUNIT; i++)
312535Sdlw 		flush_(&i);
322478Sdlw 	g_char(s, n, buf);
332478Sdlw 	return((long)system(buf));
342401Sdlw }
35*12149Sdlw 
36*12149Sdlw /*
37*12149Sdlw  * this is a sane version of the libc/stdio routine.
38*12149Sdlw  */
39*12149Sdlw 
40*12149Sdlw #include	<signal.h>
41*12149Sdlw 
42*12149Sdlw char	*getenv();
43*12149Sdlw char	*rindex();
44*12149Sdlw 
45*12149Sdlw system(s)
46*12149Sdlw char *s;
47*12149Sdlw {
48*12149Sdlw 	register int (*istat)(), (*qstat)();
49*12149Sdlw 	int status, pid, w;
50*12149Sdlw 	char	*shname, *shell;
51*12149Sdlw 
52*12149Sdlw 	if ((shell = getenv("SHELL")) == NULL)
53*12149Sdlw 		shell = "/bin/sh";
54*12149Sdlw 
55*12149Sdlw 	if (shname = rindex(shell, '/'))
56*12149Sdlw 		shname++;
57*12149Sdlw 	else
58*12149Sdlw 		shname = shell;
59*12149Sdlw 
60*12149Sdlw 	if ((pid = fork()) == 0) {
61*12149Sdlw 		execl(shell, shname, "-c", s, 0);
62*12149Sdlw 		_exit(127);
63*12149Sdlw 	}
64*12149Sdlw 	istat = signal(SIGINT, SIG_IGN);
65*12149Sdlw 	qstat = signal(SIGQUIT, SIG_IGN);
66*12149Sdlw 	while ((w = wait(&status)) != pid && w != -1)
67*12149Sdlw 		;
68*12149Sdlw 	if (w == -1)
69*12149Sdlw 		status = -1;
70*12149Sdlw 	signal(SIGINT, istat);
71*12149Sdlw 	signal(SIGQUIT, qstat);
72*12149Sdlw 	return(status);
73*12149Sdlw }
74