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
39extern int execl();
40
41int
42system(s)
43char	*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 the command
70		 * exists under /usr/bin, otherwise try /usr/ucb */
71		if (access(path, F_OK) == -1) {
72			strcpy(path, "/usr/ucb");
73			if (strncmp(s, "/bin", strlen("/bin")) == 0)
74				strcat(path, strchr(s+1, '/'));
75			else {
76				c = strchr(s+1, '/');
77				strcat(path, strchr(c+1, '/'));
78			}
79			if (c != NULL) *c = ' ';
80			s = path;
81		}
82	}
83
84	if ((pid = vfork()) == 0) {
85		(void) execl("/bin/sh", "sh", "-c", s, (char *)0);
86		_exit(127);
87	}
88	if (pid == -1) {
89		return (-1);
90	}
91	istat = signal(SIGINT, SIG_IGN);
92	qstat = signal(SIGQUIT, SIG_IGN);
93	w = waitpid(pid, &status, 0);
94	(void) signal(SIGINT, istat);
95	(void) signal(SIGQUIT, qstat);
96	return ((w == -1) ? -1: status);
97}
98