bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] findprog-in: Set errno to indicate why NULL was returned.


From: Paul Smith
Subject: [PATCH] findprog-in: Set errno to indicate why NULL was returned.
Date: Sat, 14 Sep 2019 15:35:16 -0400

Set errno to either ENOENT if the program was not found, or another
error if a program was found but was no suitable (i.e., EACCES).

* modules/findprog: Depend on errno.
* lib/findprog-in.c (find_in_given_path): Save errno if it is not ENOENT
and reset errno before returning NULL.
* lib/findprog.h (find_in_given_path): Update the documentation.
---
 lib/findprog-in.c   |  9 +++++++++
 lib/findprog.h      | 20 ++++++++++++--------
 modules/findprog-in |  1 +
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/lib/findprog-in.c b/lib/findprog-in.c
index d601e060d..8ea03f8f7 100644
--- a/lib/findprog-in.c
+++ b/lib/findprog-in.c
@@ -24,6 +24,7 @@
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <unistd.h>
 
 #include "filename.h"
@@ -74,6 +75,8 @@ const char *
 find_in_given_path (const char *progname, const char *path,
                     bool optimize_for_exec)
 {
+  int saved_errno = ENOENT;
+
   {
     bool has_slash = false;
     {
@@ -142,11 +145,14 @@ find_in_given_path (const char *progname, const char 
*path,
                         else
                           return progpathname;
                       }
+                    else if (errno != ENOENT)
+                      saved_errno = errno;
 
                     free (progpathname);
                   }
               }
 
+            errno = saved_errno;
             return NULL;
           }
       }
@@ -221,6 +227,8 @@ find_in_given_path (const char *progname, const char *path,
                     free (path_copy);
                     return progpathname;
                   }
+                else if (errno != ENOENT)
+                  saved_errno = errno;
 
                 free (progpathname);
               }
@@ -234,5 +242,6 @@ find_in_given_path (const char *progname, const char *path,
     free (path_copy);
   }
 
+  errno = saved_errno;
   return NULL;
 }
diff --git a/lib/findprog.h b/lib/findprog.h
index f7b44071f..b1b6caa5f 100644
--- a/lib/findprog.h
+++ b/lib/findprog.h
@@ -41,14 +41,18 @@ extern const char *find_in_path (const char *progname);
    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.
-   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.  */
+   On systems which support executable suffixes these are checked, if PROGNAME
+   does not already contain a suffix.
+   Returns either PROGNAME, or a freshly malloc()ed string, or NULL.  If NULL
+   is returned then errno is set (ENOENT, EACCES, etc.)
+   If PROGNAME contains a slash ('/' on POSIX; '/' or '\' on Windows) then:
+   - If OPTIMIZE_FOR_EXEC is true returns PROGNAME without checking suffixes
+     or existence under the assumption that the resulting pathname will be
+     used with execl/execv/execlp/execvp.
+   - Else if PROGNAME refers an executable program it is returned.
+   - Else NULL is returned and errno is set.
+   Otherwise PATH is searched and the pathname is returned if found, or
+   returns NULL and errno is set.  */
 extern const char *find_in_given_path (const char *progname, const char *path,
                                        bool optimize_for_exec);
 
diff --git a/modules/findprog-in b/modules/findprog-in
index ce7faa50e..d38e6f2be 100644
--- a/modules/findprog-in
+++ b/modules/findprog-in
@@ -9,6 +9,7 @@ m4/eaccess.m4
 
 Depends-on:
 stdbool
+errno
 filename
 xalloc
 xconcat-filename
-- 
2.18.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]