aboutsummaryrefslogtreecommitdiffstats
path: root/src/unistd/searchpath2.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unistd/searchpath2.c76
1 files changed, 3 insertions, 73 deletions
diff --git a/src/unistd/searchpath2.c b/src/unistd/searchpath2.c
index 9921550..b2dd800 100644
--- a/src/unistd/searchpath2.c
+++ b/src/unistd/searchpath2.c
@@ -16,11 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-/* TODO temporary contants from other headers { */
-#define _CS_PATH 1
-/* } */
@@ -43,9 +38,8 @@
*
* @param name The name of the sought executable. Must not be `NULL`.
* @param fallback Value to use instead of the value of $PATH, if
- * path is not defined. If `NULL` the fall rules for
- * the `exec`-functions are used: The current working
- * directory, and the default value for $PATH.
+ * path is not defined. If `NULL` the default value
+ * for $PATH is used.
* @return The pathname of the sought file, `NULL` on error,
* or if not found. Files that are not executable
* are (almost) ignored.
@@ -63,70 +57,6 @@
*/
char* searchpath2(const char* name, const char* fallback)
{
- char* path;
- size_t len = 0;
- char* pathname = NULL;
- char* p;
- char* q;
- int eacces = 0;
- int saved_errno;
-
- if (strstarts(name, "./") || strstarts(name, "../") || strstarts(name, "/"))
- {
- if (access(name, X_OK) == 0)
- return strdup(name);
- return NULL;
- }
-
- path = getenv("PATH");
- if ((path == NULL) && (fallback == NULL))
- {
- if ((len = confstr(_CS_PATH, NULL, 0)))
- {
- path = malloc((2 + len) * sizeof(char));
- if (path == NULL)
- goto fail;
- if (!confstr(_CS_PATH, stpcpy(path, ".:"), len))
- free(path), path = NULL;
- }
- if (path == NULL)
- path = strdup(".:/usr/local/bin:/bin:/usr/bin");
- }
- else
- path = strdup(path == NULL ? fallback : path);
- if (path == NULL)
- goto fail;
-
- pathname = malloc((strlen(path) + strlen(name) + 2) * sizeof(char));
- if (pathname == NULL)
- goto fail;
-
- for (p = path; *p; p = q + 1)
- {
- if (p == (q = strchr(p, ':')))
- continue;
- *q = '\0';
-
- stpcpy(stpcpy(stpcpy(pathname, p), "/"), name);
-
- if (access(pathname, X_OK) == 0)
- {
- char* truncated = realloc(pathname, (strlen(pathname) + 1) * sizeof(char));
- return truncated == NULL ? pathname : truncated;
- }
- else if (errno == EACCES) eacces = 1;
- else if (errno != ENOENT) goto fail;
- }
-
- free(path);
- free(pathname);
- return errno = (eacces ? EACCES : ENOENT), -1;
-
- fail:
- saved_errno = errno;
- free(path);
- free(pathname);
- errno = saved_errno;
- return -1;
+ return searchpath3(name, fallback, NULL);
}