xref: /csrg-svn/usr.bin/f77/libU77/system_.c (revision 39144)
12401Sdlw /*
223049Skre  * Copyright (c) 1980 Regents of the University of California.
323049Skre  * All rights reserved.  The Berkeley software License Agreement
423049Skre  * specifies the terms and conditions for redistribution.
52401Sdlw  *
6*39144Sbostic  *	@(#)system_.c	5.2	09/13/89
723049Skre  */
823049Skre 
923049Skre /*
102401Sdlw  * execute a unix command
112401Sdlw  *
122401Sdlw  * calling sequence:
132401Sdlw  *	iexit = system(command)
142401Sdlw  * where:
152401Sdlw  *	iexit will return the exit status of the command
162401Sdlw  *	command is a character string containing the command to be executed
172401Sdlw  */
182401Sdlw 
192440Sdlw #include	"../libI77/fiodefs.h"
202478Sdlw #include	"../libI77/f_errno.h"
2112149Sdlw #include <sys/param.h>
2212149Sdlw #ifndef	NCARGS
2312149Sdlw #define NCARGS	256
2412149Sdlw #endif
252440Sdlw 
262440Sdlw 
272401Sdlw long system_(s, n)
282401Sdlw char *s;
292401Sdlw long n;
302401Sdlw {
3112149Sdlw 	char buf[NCARGS - 50];
322535Sdlw 	long i;
332440Sdlw 
342478Sdlw 	if (n >= sizeof buf)
352478Sdlw 		return(-(long)(errno=F_ERARG));
362535Sdlw 	for (i = 0; i < MXUNIT; i++)
372535Sdlw 		flush_(&i);
382478Sdlw 	g_char(s, n, buf);
392478Sdlw 	return((long)system(buf));
402401Sdlw }
4112149Sdlw 
4212149Sdlw /*
4312149Sdlw  * this is a sane version of the libc/stdio routine.
4412149Sdlw  */
4512149Sdlw 
4612149Sdlw #include	<signal.h>
4712149Sdlw 
4812149Sdlw char	*getenv();
4912149Sdlw char	*rindex();
5012149Sdlw 
5112149Sdlw system(s)
5212149Sdlw char *s;
5312149Sdlw {
54*39144Sbostic 	register sig_t istat, qstat;
5512149Sdlw 	int status, pid, w;
5612149Sdlw 	char	*shname, *shell;
5712149Sdlw 
5812149Sdlw 	if ((shell = getenv("SHELL")) == NULL)
5912149Sdlw 		shell = "/bin/sh";
6012149Sdlw 
6112149Sdlw 	if (shname = rindex(shell, '/'))
6212149Sdlw 		shname++;
6312149Sdlw 	else
6412149Sdlw 		shname = shell;
6512149Sdlw 
6612149Sdlw 	if ((pid = fork()) == 0) {
6712149Sdlw 		execl(shell, shname, "-c", s, 0);
6812149Sdlw 		_exit(127);
6912149Sdlw 	}
7012149Sdlw 	istat = signal(SIGINT, SIG_IGN);
7112149Sdlw 	qstat = signal(SIGQUIT, SIG_IGN);
7212149Sdlw 	while ((w = wait(&status)) != pid && w != -1)
7312149Sdlw 		;
7412149Sdlw 	if (w == -1)
7512149Sdlw 		status = -1;
76*39144Sbostic 	(void)signal(SIGINT, istat);
77*39144Sbostic 	(void)signal(SIGQUIT, qstat);
7812149Sdlw 	return(status);
7912149Sdlw }
80