xref: /csrg-svn/usr.bin/f77/libU77/system_.c (revision 47944)
1*47944Sbostic /*-
2*47944Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47944Sbostic  * All rights reserved.
42401Sdlw  *
5*47944Sbostic  * %sccs.include.proprietary.c%
623049Skre  */
723049Skre 
8*47944Sbostic #ifndef lint
9*47944Sbostic static char sccsid[] = "@(#)system_.c	5.4 (Berkeley) 04/12/91";
10*47944Sbostic #endif /* not lint */
11*47944Sbostic 
1223049Skre /*
132401Sdlw  * execute a unix command
142401Sdlw  *
152401Sdlw  * calling sequence:
162401Sdlw  *	iexit = system(command)
172401Sdlw  * where:
182401Sdlw  *	iexit will return the exit status of the command
192401Sdlw  *	command is a character string containing the command to be executed
202401Sdlw  */
212401Sdlw 
222440Sdlw #include	"../libI77/fiodefs.h"
232478Sdlw #include	"../libI77/f_errno.h"
2412149Sdlw #include <sys/param.h>
2512149Sdlw #ifndef	NCARGS
2612149Sdlw #define NCARGS	256
2712149Sdlw #endif
282440Sdlw 
292440Sdlw 
system_(s,n)302401Sdlw long system_(s, n)
312401Sdlw char *s;
322401Sdlw long n;
332401Sdlw {
3412149Sdlw 	char buf[NCARGS - 50];
352535Sdlw 	long i;
362440Sdlw 
372478Sdlw 	if (n >= sizeof buf)
382478Sdlw 		return(-(long)(errno=F_ERARG));
392535Sdlw 	for (i = 0; i < MXUNIT; i++)
402535Sdlw 		flush_(&i);
412478Sdlw 	g_char(s, n, buf);
422478Sdlw 	return((long)system(buf));
432401Sdlw }
4412149Sdlw 
4512149Sdlw /*
4642453Sbostic  * this is a sane version of the libc routine.
4712149Sdlw  */
4812149Sdlw 
4942453Sbostic #include <sys/signal.h>
5042453Sbostic #include <stdlib.h>
5142453Sbostic #include <string.h>
5212149Sdlw 
system(s)5312149Sdlw system(s)
5412149Sdlw char *s;
5512149Sdlw {
5639144Sbostic 	register sig_t istat, qstat;
5712149Sdlw 	int status, pid, w;
5842453Sbostic 	char *shname, *shell;
5912149Sdlw 
6012149Sdlw 	if ((shell = getenv("SHELL")) == NULL)
6112149Sdlw 		shell = "/bin/sh";
6212149Sdlw 
6312149Sdlw 	if (shname = rindex(shell, '/'))
6412149Sdlw 		shname++;
6512149Sdlw 	else
6612149Sdlw 		shname = shell;
6712149Sdlw 
6842453Sbostic 	if ((pid = vfork()) == 0) {
6942453Sbostic 		execl(shell, shname, "-c", s, (char *)0);
7012149Sdlw 		_exit(127);
7112149Sdlw 	}
7242453Sbostic 	if (pid == -1)
7342453Sbostic 		return(-1);
7442453Sbostic 
7512149Sdlw 	istat = signal(SIGINT, SIG_IGN);
7612149Sdlw 	qstat = signal(SIGQUIT, SIG_IGN);
7712149Sdlw 	while ((w = wait(&status)) != pid && w != -1)
7812149Sdlw 		;
7912149Sdlw 	if (w == -1)
8012149Sdlw 		status = -1;
8139144Sbostic 	(void)signal(SIGINT, istat);
8239144Sbostic 	(void)signal(SIGQUIT, qstat);
8312149Sdlw 	return(status);
8412149Sdlw }
85