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