version 1.24, 2016/01/14 22:00:53 |
version 1.25, 2016/01/14 22:02:13 |
|
|
findprog(char *prog, char *path, int progmode, int allmatches) |
findprog(char *prog, char *path, int progmode, int allmatches) |
{ |
{ |
char *p, filename[PATH_MAX]; |
char *p, filename[PATH_MAX]; |
int proglen, plen, rval = 0; |
int len, rval = 0; |
struct stat sbuf; |
struct stat sbuf; |
char *pathcpy; |
char *pathcpy; |
|
|
|
|
err(1, "strdup"); |
err(1, "strdup"); |
pathcpy = path; |
pathcpy = path; |
|
|
proglen = strlen(prog); |
|
while ((p = strsep(&pathcpy, ":")) != NULL) { |
while ((p = strsep(&pathcpy, ":")) != NULL) { |
if (*p == '\0') |
if (*p == '\0') |
p = "."; |
p = "."; |
|
|
plen = strlen(p); |
len = strlen(p); |
while (plen > 0 && p[plen-1] == '/') |
while (len > 0 && p[len-1] == '/') |
p[--plen] = '\0'; /* strip trailing '/' */ |
p[--len] = '\0'; /* strip trailing '/' */ |
|
|
if (plen + 1 + proglen >= sizeof(filename)) { |
len = snprintf(filename, sizeof(filename), "%s/%s", p, prog); |
|
if (len < 0 || len >= sizeof(filename)) { |
warnc(ENAMETOOLONG, "%s/%s", p, prog); |
warnc(ENAMETOOLONG, "%s/%s", p, prog); |
free(path); |
free(path); |
return (0); |
return (0); |
} |
} |
|
|
snprintf(filename, sizeof(filename), "%s/%s", p, prog); |
|
if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) && |
if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) && |
access(filename, X_OK) == 0) { |
access(filename, X_OK) == 0) { |
(void)puts(filename); |
(void)puts(filename); |