xref: /titanic_44/usr/src/lib/libbc/libc/gen/4.2/system.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 1.2 */
31 
32 /*LINTLIBRARY*/
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <string.h>
37 #include <sys/file.h>
38 
39 extern int execl();
40 
41 int
42 system(s)
43 char	*s;
44 {
45 	int	status;
46 	pid_t	pid, w;
47 	register void (*istat)(), (*qstat)();
48 	char	path[256];
49 	char 	*c;
50 
51 	while (*s == ' ' || *s == '\t')
52 		s++;
53 
54 	if (strncmp(s, "/usr/ucb", strlen("/usr/ucb")) == 0) {
55 		/* check if command is under /usr/ucb, if not
56 		 * replace /usr/ucb with /usr/bin.
57  		 */
58 		strcpy(path, s);
59 		if ((c = strchr(path, ' ')) != NULL)
60 			*c ='\0';
61 		if (access(path, F_OK) == -1) {
62 			strncpy(path, "/usr/bin", strlen("/usr/bin"));
63 			if (c != NULL) *c = ' ';
64 			s = path;
65 		}
66 	}
67 	else if (strncmp(s, "/bin", strlen("/bin")) == 0 ||
68 		 strncmp(s, "/usr/bin", strlen("/usr/bin")) == 0) {
69 		/* if /usr/bin is specified, first check if a command
70 		 * with the same name exists under /usr/ucb */
71 		strcpy(path, "/usr/ucb");
72 		if (strncmp(s, "/bin", strlen("/bin")) == 0)
73 			strcat(path, strchr(s+1, '/'));
74 		else {
75 			c = strchr(s+1, '/');
76 			strcat(path, strchr(c+1, '/'));
77 		}
78 		if ((c = strchr(path, ' ')) != NULL)
79 			*c ='\0';
80 		if (access(path, F_OK) == 0) {
81 			if (c != NULL) *c = ' ';
82 			s = path;
83 		}
84 	}
85 
86 	if ((pid = vfork()) == 0) {
87 		(void) execl("/bin/sh", "sh", "-c", s, (char *)0);
88 		_exit(127);
89 	}
90 	if (pid == -1) {
91 		return (-1);
92 	}
93 	istat = signal(SIGINT, SIG_IGN);
94 	qstat = signal(SIGQUIT, SIG_IGN);
95 	w = waitpid(pid, &status, 0);
96 	(void) signal(SIGINT, istat);
97 	(void) signal(SIGQUIT, qstat);
98 	return ((w == -1) ? -1: status);
99 }
100