Home | History | Annotate | Line # | Download | only in gen
execvp.c revision 1.3
      1 /*	$NetBSD: execvp.c,v 1.3 1997/07/13 18:57:04 christos 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 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)exec.c	8.1 (Berkeley) 6/4/93";
     40 #else
     41 __RCSID("$NetBSD: execvp.c,v 1.3 1997/07/13 18:57:04 christos Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <limits.h>
     50 #include <unistd.h>
     51 #include <paths.h>
     52 
     53 extern char **environ;
     54 
     55 int
     56 execvp(name, argv)
     57 	const char *name;
     58 	char * const *argv;
     59 {
     60 	static int memsize;
     61 	static char **memp;
     62 	register int cnt, lp, ln;
     63 	register char *p;
     64 	int eacces = 0, etxtbsy = 0;
     65 	char *bp, *cur, *path, buf[PATH_MAX];
     66 
     67 	/* "" is not a valid filename; check this before traversing PATH. */
     68 	if (name[0] == '\0') {
     69 		errno = ENOENT;
     70 		path = NULL;
     71 		goto done;
     72 	}
     73 	/* If it's an absolute or relative path name, it's easy. */
     74 	if (strchr(name, '/')) {
     75 		bp = (char *)name;
     76 		cur = path = NULL;
     77 		goto retry;
     78 	}
     79 	bp = buf;
     80 
     81 	/* Get the path we're searching. */
     82 	if (!(path = getenv("PATH")))
     83 		path = _PATH_DEFPATH;
     84 	cur = path = strdup(path);
     85 
     86 	while ((p = strsep(&cur, ":")) != NULL) {
     87 		/*
     88 		 * It's a SHELL path -- double, leading and trailing colons
     89 		 * mean the current directory.
     90 		 */
     91 		if (!*p) {
     92 			p = ".";
     93 			lp = 1;
     94 		} else
     95 			lp = strlen(p);
     96 		ln = strlen(name);
     97 
     98 		/*
     99 		 * If the path is too long complain.  This is a possible
    100 		 * security issue; given a way to make the path too long
    101 		 * the user may execute the wrong program.
    102 		 */
    103 		if (lp + ln + 2 > sizeof(buf)) {
    104 			(void)write(STDERR_FILENO, "execvp: ", 8);
    105 			(void)write(STDERR_FILENO, p, lp);
    106 			(void)write(STDERR_FILENO, ": path too long\n", 16);
    107 			continue;
    108 		}
    109 		bcopy(p, buf, lp);
    110 		buf[lp] = '/';
    111 		bcopy(name, buf + lp + 1, ln);
    112 		buf[lp + ln + 1] = '\0';
    113 
    114 retry:		(void)execve(bp, argv, environ);
    115 		switch(errno) {
    116 		case EACCES:
    117 			eacces = 1;
    118 			break;
    119 		case ENOENT:
    120 			break;
    121 		case ENOEXEC:
    122 			for (cnt = 0; argv[cnt]; ++cnt);
    123 			if ((cnt + 2) * sizeof(char *) > memsize) {
    124 				memsize = (cnt + 2) * sizeof(char *);
    125 				if ((memp = realloc(memp, memsize)) == NULL) {
    126 					memsize = 0;
    127 					goto done;
    128 				}
    129 			}
    130 			memp[0] = "sh";
    131 			memp[1] = bp;
    132 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
    133 			(void)execve(_PATH_BSHELL, memp, environ);
    134 			goto done;
    135 		case ETXTBSY:
    136 			if (etxtbsy < 3)
    137 				(void)sleep(++etxtbsy);
    138 			goto retry;
    139 		default:
    140 			goto done;
    141 		}
    142 	}
    143 	if (eacces)
    144 		errno = EACCES;
    145 	else if (!errno)
    146 		errno = ENOENT;
    147 done:	if (path)
    148 		free(path);
    149 	return (-1);
    150 }
    151