2011-02-12 00:22:29 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-09-29 00:48:04 -04:00
|
|
|
#include <limits.h>
|
2014-04-18 22:40:28 -05:00
|
|
|
#include "libc.h"
|
2011-02-12 00:22:29 -05:00
|
|
|
|
2013-02-17 14:24:39 -05:00
|
|
|
extern char **__environ;
|
2012-10-18 16:41:27 -04:00
|
|
|
|
|
|
|
int __execvpe(const char *file, char *const argv[], char *const envp[])
|
2011-02-12 00:22:29 -05:00
|
|
|
{
|
|
|
|
const char *p, *z, *path = getenv("PATH");
|
2011-09-29 00:48:04 -04:00
|
|
|
size_t l, k;
|
2015-02-03 00:31:35 -05:00
|
|
|
int seen_eacces = 0;
|
2011-09-29 00:48:04 -04:00
|
|
|
|
|
|
|
errno = ENOENT;
|
|
|
|
if (!*file) return -1;
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
if (strchr(file, '/'))
|
2012-10-18 16:41:27 -04:00
|
|
|
return execve(file, argv, envp);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
if (!path) path = "/usr/local/bin:/bin:/usr/bin";
|
2011-09-29 00:48:04 -04:00
|
|
|
k = strnlen(file, NAME_MAX+1);
|
|
|
|
if (k > NAME_MAX) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
l = strnlen(path, PATH_MAX-1)+1;
|
2011-02-12 00:22:29 -05:00
|
|
|
|
2011-09-29 00:48:04 -04:00
|
|
|
for(p=path; ; p=z) {
|
|
|
|
char b[l+k+1];
|
2011-02-12 00:22:29 -05:00
|
|
|
z = strchr(p, ':');
|
2011-09-29 00:48:04 -04:00
|
|
|
if (!z) z = p+strlen(p);
|
|
|
|
if (z-p >= l) {
|
|
|
|
if (!*z++) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(b, p, z-p);
|
|
|
|
b[z-p] = '/';
|
|
|
|
memcpy(b+(z-p)+(z>p), file, k+1);
|
2012-10-18 16:41:27 -04:00
|
|
|
execve(b, argv, envp);
|
2015-02-03 00:31:35 -05:00
|
|
|
if (errno == EACCES) seen_eacces = 1;
|
|
|
|
else if (errno != ENOENT) return -1;
|
2011-09-29 00:48:04 -04:00
|
|
|
if (!*z++) break;
|
2011-02-12 00:22:29 -05:00
|
|
|
}
|
2015-02-03 00:31:35 -05:00
|
|
|
if (seen_eacces) errno = EACCES;
|
2011-02-12 00:22:29 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-18 16:41:27 -04:00
|
|
|
|
|
|
|
int execvp(const char *file, char *const argv[])
|
|
|
|
{
|
2013-02-17 14:24:39 -05:00
|
|
|
return __execvpe(file, argv, __environ);
|
2012-10-18 16:41:27 -04:00
|
|
|
}
|
2014-04-18 22:40:28 -05:00
|
|
|
|
|
|
|
weak_alias(__execvpe, execvpe);
|