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