4 * execvpe() function (from which we build execlp, execlpe, execvp).
6 * This version of execvpe() will *not* spawn /bin/sh if the command
7 * return ENOEXEC. That's what #! is for, folks!
9 * Since execlpe() and execvpe() aren't in POSIX, nor in glibc,
10 * I have followed QNX precedent in the implementation of the PATH:
11 * the PATH that is used is the one in the current environment, not
12 * in the new environment.
21 #define DEFAULT_PATH "/bin:/usr/bin:."
23 int execvpe(const char *file, char * const *argv, char * const *envp)
26 const char *searchpath, *esp;
27 size_t prefixlen, filelen, totallen;
29 if ( strchr(file, '/') ) /* Specific path */
30 return execve(file, argv, envp);
32 filelen = strlen(file);
34 searchpath = getenv("PATH");
36 searchpath = DEFAULT_PATH;
38 errno = ENOENT; /* Default errno, if execve() doesn't change it */
41 esp = strchr(searchpath, ':');
43 prefixlen = esp-searchpath;
45 prefixlen = strlen(searchpath);
47 if ( prefixlen == 0 || searchpath[prefixlen-1] == '/' ) {
48 totallen = prefixlen+filelen;
49 if ( totallen >= PATH_MAX )
51 memcpy(path, searchpath, prefixlen);
52 memcpy(path+prefixlen, file, filelen);
54 totallen = prefixlen+filelen+1;
55 if ( totallen >= PATH_MAX )
57 memcpy(path, searchpath, prefixlen);
58 path[prefixlen] = '/';
59 memcpy(path+prefixlen+1, file, filelen);
61 path[totallen] = '\0';
63 execve(path, argv, envp);
64 if ( errno == E2BIG || errno == ENOEXEC ||
65 errno == ENOMEM || errno == ETXTBSY )
66 break; /* Report this as an error, no more search */