xref: /netbsd-src/lib/libc/gen/execvp.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: execvp.c,v 1.1 1996/07/03 21:41:54 jtc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: execvp.c,v 1.1 1996/07/03 21:41:54 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <unistd.h>
50 #include <paths.h>
51 
52 extern char **environ;
53 
54 int
55 execvp(name, argv)
56 	const char *name;
57 	char * const *argv;
58 {
59 	static int memsize;
60 	static char **memp;
61 	register int cnt, lp, ln;
62 	register char *p;
63 	int eacces = 0, etxtbsy = 0;
64 	char *bp, *cur, *path, buf[PATH_MAX];
65 
66 	/* If it's an absolute or relative path name, it's easy. */
67 	if (strchr(name, '/')) {
68 		bp = (char *)name;
69 		cur = path = NULL;
70 		goto retry;
71 	}
72 	bp = buf;
73 
74 	/* Get the path we're searching. */
75 	if (!(path = getenv("PATH")))
76 		path = _PATH_DEFPATH;
77 	cur = path = strdup(path);
78 
79 	while (p = strsep(&cur, ":")) {
80 		/*
81 		 * It's a SHELL path -- double, leading and trailing colons
82 		 * mean the current directory.
83 		 */
84 		if (!*p) {
85 			p = ".";
86 			lp = 1;
87 		} else
88 			lp = strlen(p);
89 		ln = strlen(name);
90 
91 		/*
92 		 * If the path is too long complain.  This is a possible
93 		 * security issue; given a way to make the path too long
94 		 * the user may execute the wrong program.
95 		 */
96 		if (lp + ln + 2 > sizeof(buf)) {
97 			(void)write(STDERR_FILENO, "execvp: ", 8);
98 			(void)write(STDERR_FILENO, p, lp);
99 			(void)write(STDERR_FILENO, ": path too long\n", 16);
100 			continue;
101 		}
102 		bcopy(p, buf, lp);
103 		buf[lp] = '/';
104 		bcopy(name, buf + lp + 1, ln);
105 		buf[lp + ln + 1] = '\0';
106 
107 retry:		(void)execve(bp, argv, environ);
108 		switch(errno) {
109 		case EACCES:
110 			eacces = 1;
111 			break;
112 		case ENOENT:
113 			break;
114 		case ENOEXEC:
115 			for (cnt = 0; argv[cnt]; ++cnt);
116 			if ((cnt + 2) * sizeof(char *) > memsize) {
117 				memsize = (cnt + 2) * sizeof(char *);
118 				if ((memp = realloc(memp, memsize)) == NULL) {
119 					memsize = 0;
120 					goto done;
121 				}
122 			}
123 			memp[0] = "sh";
124 			memp[1] = bp;
125 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
126 			(void)execve(_PATH_BSHELL, memp, environ);
127 			goto done;
128 		case ETXTBSY:
129 			if (etxtbsy < 3)
130 				(void)sleep(++etxtbsy);
131 			goto retry;
132 		default:
133 			goto done;
134 		}
135 	}
136 	if (eacces)
137 		errno = EACCES;
138 	else if (!errno)
139 		errno = ENOENT;
140 done:	if (path)
141 		free(path);
142 	return (-1);
143 }
144