Home | History | Annotate | Line # | Download | only in gen
execvp.c revision 1.2
      1 /*	$NetBSD: execvp.c,v 1.2 1997/04/24 18:55:51 kleink 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.2 1997/04/24 18:55:51 kleink 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 	/* "" is not a valid filename; check this before traversing PATH. */
     67 	if (name[0] == '\0') {
     68 		errno = ENOENT;
     69 		goto done;
     70 	}
     71 	/* If it's an absolute or relative path name, it's easy. */
     72 	if (strchr(name, '/')) {
     73 		bp = (char *)name;
     74 		cur = path = NULL;
     75 		goto retry;
     76 	}
     77 	bp = buf;
     78 
     79 	/* Get the path we're searching. */
     80 	if (!(path = getenv("PATH")))
     81 		path = _PATH_DEFPATH;
     82 	cur = path = strdup(path);
     83 
     84 	while (p = strsep(&cur, ":")) {
     85 		/*
     86 		 * It's a SHELL path -- double, leading and trailing colons
     87 		 * mean the current directory.
     88 		 */
     89 		if (!*p) {
     90 			p = ".";
     91 			lp = 1;
     92 		} else
     93 			lp = strlen(p);
     94 		ln = strlen(name);
     95 
     96 		/*
     97 		 * If the path is too long complain.  This is a possible
     98 		 * security issue; given a way to make the path too long
     99 		 * the user may execute the wrong program.
    100 		 */
    101 		if (lp + ln + 2 > sizeof(buf)) {
    102 			(void)write(STDERR_FILENO, "execvp: ", 8);
    103 			(void)write(STDERR_FILENO, p, lp);
    104 			(void)write(STDERR_FILENO, ": path too long\n", 16);
    105 			continue;
    106 		}
    107 		bcopy(p, buf, lp);
    108 		buf[lp] = '/';
    109 		bcopy(name, buf + lp + 1, ln);
    110 		buf[lp + ln + 1] = '\0';
    111 
    112 retry:		(void)execve(bp, argv, environ);
    113 		switch(errno) {
    114 		case EACCES:
    115 			eacces = 1;
    116 			break;
    117 		case ENOENT:
    118 			break;
    119 		case ENOEXEC:
    120 			for (cnt = 0; argv[cnt]; ++cnt);
    121 			if ((cnt + 2) * sizeof(char *) > memsize) {
    122 				memsize = (cnt + 2) * sizeof(char *);
    123 				if ((memp = realloc(memp, memsize)) == NULL) {
    124 					memsize = 0;
    125 					goto done;
    126 				}
    127 			}
    128 			memp[0] = "sh";
    129 			memp[1] = bp;
    130 			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
    131 			(void)execve(_PATH_BSHELL, memp, environ);
    132 			goto done;
    133 		case ETXTBSY:
    134 			if (etxtbsy < 3)
    135 				(void)sleep(++etxtbsy);
    136 			goto retry;
    137 		default:
    138 			goto done;
    139 		}
    140 	}
    141 	if (eacces)
    142 		errno = EACCES;
    143 	else if (!errno)
    144 		errno = ENOENT;
    145 done:	if (path)
    146 		free(path);
    147 	return (-1);
    148 }
    149