diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-10-11 01:49:55 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-10-11 01:49:55 +0200 |
commit | 24dbdbb0356b41f6baa489a4ea685b957189ade2 (patch) | |
tree | 388b80f769475058f4605a50f866899b0277549d /src/unistd | |
parent | m doc: exec will use . and default PATH if PATH is not set (diff) | |
download | slibc-24dbdbb0356b41f6baa489a4ea685b957189ade2.tar.gz slibc-24dbdbb0356b41f6baa489a4ea685b957189ade2.tar.bz2 slibc-24dbdbb0356b41f6baa489a4ea685b957189ade2.tar.xz |
execvpe: simplify and fix errors
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/unistd')
-rw-r--r-- | src/unistd/exec.c | 53 |
1 files changed, 16 insertions, 37 deletions
diff --git a/src/unistd/exec.c b/src/unistd/exec.c index 5ee3345..ab1a09a 100644 --- a/src/unistd/exec.c +++ b/src/unistd/exec.c @@ -300,9 +300,6 @@ int execve(const char* path, char* const argv[], char* const envp[]) int execvpe(const char* file, char* const argv[], char* const envp[]) { char* path = NULL; - char* path_cwd = NULL; - char* path_def = NULL; - char* path_env; char* pathname = NULL; char* p; char* q; @@ -316,55 +313,40 @@ int execvpe(const char* file, char* const argv[], char* const envp[]) if (!*file) return errno = ENOENT, -1; - path_env = getenv(PATH); - if (path_env == NULL) + path = getenv(PATH); + if (path == NULL) { - path_cwd = get_current_dir_name(); - if (path_cwd == NULL) - goto fail; - - len = confstr(_CS_PATH, NULL, 0); - if (len) + if ((len = confstr(_CS_PATH, NULL, 0))) { - path_def = malloc((1 + len) * sizeof(char)); - if (path_def == NULL) + path = malloc((2 + len) * sizeof(char)); + if (path == NULL) goto fail; - *path_def = ':'; - if (confstr(_CS_PATH, path_def + 1, len)) - path_def = NULL, len = 0; + if (!confstr(_CS_PATH, stpcpy(path, ".:"), len)) + free(path), path = NULL; } - if (path_def == NULL) - path_def = ":/usr/local/bin:/bin:/usr/bin"; - - path = malloc((strlen(path_cwd) + strlen(path_def) + 1) * sizeof(char)); if (path == NULL) - goto fail; - - stpcpy(stpcpy(path, path_cwd), path_def); - free(path_cwd), path_cwd = NULL; - if (len) - free(path_def), path_def = NULL; + path = strdup(".:/usr/local/bin:/bin:/usr/bin"); } else - if (path = strdup(path_env), path == NULL) - goto fail; + path = strdup(path); + if (path == NULL) + goto fail; pathname = malloc((strlen(path) + strlen(file) + 2) * sizeof(char)); + if (pathname == NULL) + goto fail; for (p = path; *p; p = q + 1) { - q = strchr(p, ':'); - if (q == p) + if (p == (q = strchr(p, ':'))) continue; *q = '\0'; stpcpy(stpcpy(stpcpy(pathname, p), "/"), file); execve(pathname, argv, envp); - if (errno == EACCES) - eacces = 1; - else if (errno != ENOENT) - goto fail; + if (errno == EACCES) eacces = 1; + else if (errno != ENOENT) goto fail; } free(path); @@ -373,9 +355,6 @@ int execvpe(const char* file, char* const argv[], char* const envp[]) fail: saved_errno = errno; - free(path_cwd); - if (len) - free(path_def); free(path); free(pathname); errno = saved_errno; |