diff options
author | Bruno Haible <bruno@clisp.org> | 2020-04-10 15:57:10 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2020-04-10 15:57:10 +0200 |
commit | 7b1de4a62f8766787160f785217671b074e0f0f2 (patch) | |
tree | 59c80ebdaa034f40c9bbf8d8a3f71b3f1d054d34 /lib/findprog.c | |
parent | 242644e7d7ab831b2d0c138bcad21d6b2758fbcc (diff) | |
download | gnulib-7b1de4a62f8766787160f785217671b074e0f0f2.tar.gz |
findprog, relocatable-prog: Ignore directories during PATH search.
Reported by Frederick Eaton via Dmitry Goncharov in
<https://lists.gnu.org/archive/html/bug-gnulib/2020-03/msg00003.html>.
* lib/findprog.c (find_in_path): When the file found in a PATH element
is a directory, continue searching.
* modules/findprog (Depends-on): Add sys_stat, stat.
* modules/findprog-lgpl (Depends-on): Likewise.
* lib/progreloc.c (maybe_executable): When the file found in a PATH
element is a directory, continue searching.
* lib/relocwrapper.c: Update comments.
* modules/relocatable-prog-wrapper (Files): Add m4/largefile.m4.
(configure.ac-early): New section.
Diffstat (limited to 'lib/findprog.c')
-rw-r--r-- | lib/findprog.c | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/lib/findprog.c b/lib/findprog.c index d0d41791e2..b562e9da6a 100644 --- a/lib/findprog.c +++ b/lib/findprog.c @@ -25,6 +25,9 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#if !(defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__) +# include <sys/stat.h> +#endif /* Avoid collision between findprog.c and findprog-lgpl.c. */ #if IN_FINDPROG_LGPL || ! GNULIB_FINDPROG_LGPL @@ -105,22 +108,29 @@ find_in_path (const char *progname) design flaw. */ if (eaccess (progpathname, X_OK) == 0) { - /* Found! */ - if (strcmp (progpathname, progname) == 0) + /* Check that the progpathname does not point to a directory. */ + struct stat statbuf; + + if (stat (progpathname, &statbuf) >= 0 + && ! S_ISDIR (statbuf.st_mode)) { - free (progpathname); - - /* Add the "./" prefix for real, that xconcatenated_filename() - optimized away. This avoids a second PATH search when the - caller uses execlp/execvp. */ - progpathname = XNMALLOC (2 + strlen (progname) + 1, char); - progpathname[0] = '.'; - progpathname[1] = '/'; - memcpy (progpathname + 2, progname, strlen (progname) + 1); + /* Found! */ + if (strcmp (progpathname, progname) == 0) + { + free (progpathname); + + /* Add the "./" prefix for real, that xconcatenated_filename() + optimized away. This avoids a second PATH search when the + caller uses execlp/execvp. */ + progpathname = XNMALLOC (2 + strlen (progname) + 1, char); + progpathname[0] = '.'; + progpathname[1] = '/'; + memcpy (progpathname + 2, progname, strlen (progname) + 1); + } + + free (path); + return progpathname; } - - free (path); - return progpathname; } free (progpathname); |