bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Paul Smith <psmith@gnu.org>
To: bug-gnulib@gnu.org
Subject: [PATCH] findprog-in: Set errno to indicate why NULL was returned.
Date: Sat, 14 Sep 2019 15:35:16 -0400	[thread overview]
Message-ID: <20190914193516.21375-1-psmith@gnu.org> (raw)

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	other threads:[~2019-09-14 19:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-14 19:35 Paul Smith [this message]
2019-09-14 19:38 ` [PATCH] findprog-in: Set errno to indicate why NULL was returned Paul Smith
2019-09-15 17:11 ` new module 'access' Bruno Haible
2019-09-16 15:22   ` Eli Zaretskii
2019-09-16 22:45     ` Bruno Haible
2019-09-17  6:15       ` Eli Zaretskii
2019-09-28 12:06         ` Bruno Haible
2019-09-28 12:17           ` Eli Zaretskii
2019-09-28 13:29             ` Bruno Haible
2019-09-28 13:54               ` Eli Zaretskii
2019-09-15 17:58 ` [PATCH] findprog-in: Set errno to indicate why NULL was returned Bruno Haible
2019-09-16 15:18   ` Eli Zaretskii
2019-09-28 11:48     ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190914193516.21375-1-psmith@gnu.org \
    --to=psmith@gnu.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).