From 847ce91e0f087fc8c3e2c7e20dfb8f7df3407694 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 4 Oct 1993 00:12:35 +0000 Subject: [PATCH] now checks '.' or '.' or '' in PATH -- but does it LAST should maybe move the code that does this into the loop body. makes it messier tho. hmmm. --- find_path.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/find_path.c b/find_path.c index 83af6e6b8..b6298b981 100644 --- a/find_path.c +++ b/find_path.c @@ -79,6 +79,7 @@ char *find_path(file) char fn[MAXPATHLEN+1]; /* filename (path + file) */ struct stat statbuf; /* for stat(2) */ int statfailed; /* stat(2) return value */ + int checkdot = 0; /* check current dir? */ char *qualify(); if (strlen(file) > MAXPATHLEN) { @@ -99,11 +100,23 @@ char *find_path(file) exit(1); } - while ((n = index(path, ':'))) { - *n = '\0'; - (void)strcpy(fn, path); - (void)strcat(fn, "/"); - (void)strcat(fn, file); + do { + /* cheap strtok() */ + if ((n = index(path, ':'))) + *n = '\0'; + + /* + * search current dir last if it is in PATH + * This will miss sneaky things like using './' or './/' + */ + if (*path == NULL || (*path == '.' && *(path+1) == NULL)) { + checkdot = 1; + path = n + 1; + continue; + } + + /* filename to try */ + (void)sprintf(fn, "%s/%s", path, file); /* stat the file to make sure it exists and is executable */ statfailed = stat(fn, &statbuf); @@ -114,6 +127,21 @@ char *find_path(file) exit(1); } path = n + 1; + + } while (n); + + /* check current dir if dot was in the PATH */ + if (checkdot) { + (void)sprintf(fn, "./%s", file); + + /* stat the file to make sure it exists and is executable */ + statfailed = stat(fn, &statbuf); + if (!statfailed && (statbuf.st_mode & 0000111)) + return(qualify(fn)); + else if (statfailed && errno != ENOENT && errno != ENOTDIR) { + perror("find_path: stat"); + exit(1); + } } return(NULL); } -- 2.50.1