xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/execvp.c (revision 8461248208fabd3a8230615f8615e5bf1b4dcdcb)
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 /*
34  *	execlp(name, arg,...,0)	(like execl, but does path search)
35  *	execvp(name, argv)	(like execv, but does path search)
36  */
37 #include <sys/errno.h>
38 #include <sys/param.h>
39 #include <varargs.h>
40 #define	NULL	0
41 
42 static char *execat();
43 static char *shell = "/bin/sh";
44 extern char *getenv(), *strchr();
45 extern unsigned sleep();
46 extern int errno, execv();
47 
48 /*VARARGS1*/
49 int
50 execlp(name, va_alist)
51 	char	*name;
52 	va_dcl
53 {
54 	va_list args;
55 
56 	va_start(args);
57 	return(execvp(name, (char **)args));
58 }
59 
60 int
61 execvp(name, argv)
62 char	*name, **argv;
63 {
64 	char	*pathstr;
65 	char	fname[MAXPATHLEN];
66 	char	*newargs[256];
67 	int	i;
68 	register char	*cp;
69 	register unsigned etxtbsy=1;
70 	register int eacces=0;
71 
72 	if((pathstr = getenv("PATH")) == NULL)
73 		pathstr = ":/usr/ucb:/bin:/usr/bin";
74 	cp = strchr(name, '/')? "": pathstr;
75 
76 	do {
77 		cp = execat(cp, name, fname);
78 	retry:
79 		(void) execv(fname, argv);
80 		switch(errno) {
81 		case ENOEXEC:
82 			newargs[0] = "sh";
83 			newargs[1] = fname;
84 			for(i=1; newargs[i+1]=argv[i]; ++i) {
85 				if(i >= 254) {
86 					errno = E2BIG;
87 					return(-1);
88 				}
89 			}
90 			(void) execv(shell, newargs);
91 			return(-1);
92 		case ETXTBSY:
93 			if(++etxtbsy > 5)
94 				return(-1);
95 			(void) sleep(etxtbsy);
96 			goto retry;
97 		case EACCES:
98 			++eacces;
99 			break;
100 		case ENOMEM:
101 		case E2BIG:
102 		case EFAULT:
103 			return(-1);
104 		}
105 	} while(cp);
106 	if(eacces)
107 		errno = EACCES;
108 	return(-1);
109 }
110 
111 static char *
112 execat(s1, s2, si)
113 register char *s1, *s2;
114 char	*si;
115 {
116 	register char	*s;
117 	char	*end;
118 
119 	s = si;
120 	end = s + MAXPATHLEN;
121 	while(*s1 && *s1 != ':' && s < end)
122 		*s++ = *s1++;
123 	if(si != s && s < end)
124 		*s++ = '/';
125 	while(*s2 && s < end)
126 		*s++ = *s2++;
127 	*s = '\0';
128 	return(*s1? ++s1: 0);
129 }
130