]> Savannah Git Hosting - gnulib.git/commitdiff
findprog-in: Set errno when the search fails.
authorPaul Smith <psmith@gnu.org>
Sun, 15 Sep 2019 17:47:43 +0000 (19:47 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 15 Sep 2019 17:47:43 +0000 (19:47 +0200)
* lib/findprog-in.c: Include <errno.h>.
(find_in_given_path): Set errno before returning NULL.
* lib/findprog.h (find_in_given_path): Update comment accordingly.
Define the term "slash".

ChangeLog
lib/findprog-in.c
lib/findprog.h

index 7d2eb3aa93ad0ba69806c041d4cb5bfca9cb6769..903757318768c09c208bd178fa5435869a4adf60 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2019-09-15  Paul Smith  <psmith@gnu.org>
+            Bruno Haible  <bruno@clisp.org>
+
+       findprog-in: Set errno when the search fails.
+       * lib/findprog-in.c: Include <errno.h>.
+       (find_in_given_path): Set errno before returning NULL.
+       * lib/findprog.h (find_in_given_path): Update comment accordingly.
+       Define the term "slash".
+
 2019-09-15  Bruno Haible  <bruno@clisp.org>
 
        findprog, findprog-lgpl, findprog-in: Fix crash on MSVC.
index d601e060d911f4f400e8f42cc31e8459f3ed94e3..5e906805d8ba77adc964c0625e3e5999ee796c71 100644 (file)
@@ -21,6 +21,7 @@
 /* Specification.  */
 #include "findprog.h"
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -98,6 +99,7 @@ find_in_given_path (const char *progname, const char *path,
           {
             /* Try the various suffixes and see whether one of the files
                with such a suffix is actually executable.  */
+            int failure_errno;
             size_t i;
             #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
             const char *progbasename;
@@ -113,6 +115,7 @@ find_in_given_path (const char *progname, const char *path,
             #endif
 
             /* Try all platform-dependent suffixes.  */
+            failure_errno = ENOENT;
             for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++)
               {
                 const char *suffix = suffixes[i];
@@ -143,10 +146,14 @@ find_in_given_path (const char *progname, const char *path,
                           return progpathname;
                       }
 
+                    if (errno != ENOENT)
+                      failure_errno = errno;
+
                     free (progpathname);
                   }
               }
 
+            errno = failure_errno;
             return NULL;
           }
       }
@@ -158,11 +165,13 @@ find_in_given_path (const char *progname, const char *path,
     path = "";
 
   {
+    int failure_errno;
     /* Make a copy, to prepare for destructive modifications.  */
     char *path_copy = xstrdup (path);
     char *path_rest;
     char *cp;
 
+    failure_errno = ENOENT;
     for (path_rest = path_copy; ; path_rest = cp + 1)
       {
         const char *dir;
@@ -222,6 +231,9 @@ find_in_given_path (const char *progname, const char *path,
                     return progpathname;
                   }
 
+                if (errno != ENOENT)
+                  failure_errno = errno;
+
                 free (progpathname);
               }
           }
@@ -232,7 +244,8 @@ find_in_given_path (const char *progname, const char *path,
 
     /* Not found in PATH.  */
     free (path_copy);
-  }
 
-  return NULL;
+    errno = failure_errno;
+    return NULL;
+  }
 }
index f7b44071fbe0b01b27364807e46e21b171fe443a..804f02a03c9116fd4d5b51f7498936d7d29b70d4 100644 (file)
@@ -36,19 +36,29 @@ extern "C" {
 extern const char *find_in_path (const char *progname);
 
 /* Looks up a program in the given PATH-like string.
+
    The PATH argument consists of a list of directories, separated by ':' or
    (on native Windows) by ';'.  An empty PATH element designates the current
    directory.  A null PATH is equivalent to an empty PATH, that is, to the
    singleton list that contains only the current directory.
+
    Determines the pathname that would be called by execlp/execvp of PROGNAME.
    - If successful, it returns a pathname containing a slash (either absolute
      or relative to the current directory).  The returned string can be used
      with either execl/execv or execlp/execvp.  It is freshly malloc()ed if it
      is != PROGNAME.
-   - Otherwise, it returns NULL.
+   - Otherwise, it sets errno and returns NULL.
+     Specific errno values include:
+       - ENOENT: means that the program's file was not found.
+       - EACCESS: means that the program's file was found but lacks the
+         execute permissions.
    If OPTIMIZE_FOR_EXEC is true, the function saves some work, under the
    assumption that the resulting pathname will not be accessed directly,
-   only through execl/execv or execlp/execvp.  */
+   only through execl/execv or execlp/execvp.
+
+   Here, a "slash" means:
+     - On POSIX systems excluding Cygwin: a '/',
+     - On Windows, OS/2, DOS platforms: a '/' or '\'. */
 extern const char *find_in_given_path (const char *progname, const char *path,
                                        bool optimize_for_exec);