bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH] findprog: Support searching in a specified path string


From: Paul Smith
Subject: [PATCH] findprog: Support searching in a specified path string
Date: Fri, 6 Sep 2019 19:10:11 -0400

find_prog_in_path() always uses the PATH value in the current
environment.  It can be very useful to search for programs on
a path without having to modify the environment first.

Provide find_in_path_str() which takes a path string to search.
If the path passed in is NULL, fall back to searching in the
environment's PATH value.

* lib/findprog.h (find_prog_in_path_str): Added.
(find_prog_in_path): Turn into a macro invoking the new function.
* lib/findprog.c (find_prog_in_path): Rename, add the extra
argument "in_path", and only look up PATH if it's NULL.
---
 lib/findprog.c | 12 +++++++-----
 lib/findprog.h | 10 +++++++++-
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/findprog.c b/lib/findprog.c
index 89fbe5fdf..94c938306 100644
--- a/lib/findprog.c
+++ b/lib/findprog.c
@@ -36,7 +36,7 @@
 
 
 const char *
-find_in_path (const char *progname)
+find_in_path_str (const char *progname, const char *in_path)
 {
 #if defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined 
__DJGPP__
   /* Native Windows, Cygwin, OS/2, DOS */
@@ -54,17 +54,19 @@ find_in_path (const char *progname)
        the current directory.  PATH is not used.  */
     return progname;
 
-  path = getenv ("PATH");
-  if (path == NULL || *path == '\0')
+  if (in_path == NULL)
+    in_path = getenv ("PATH");
+
+  if (in_path == NULL || *in_path == '\0')
     /* If PATH is not set, the default search path is implementation
        dependent.  */
     return progname;
 
   /* Make a copy, to prepare for destructive modifications.  */
 # if !IN_FINDPROG_LGPL
-  path = xstrdup (path);
+  path = xstrdup (in_path);
 # else
-  path = strdup (path);
+  path = strdup (in_path);
   if (path == NULL)
     /* Out of memory.  */
     return progname;
diff --git a/lib/findprog.h b/lib/findprog.h
index a354f67f7..5af3f1a90 100644
--- a/lib/findprog.h
+++ b/lib/findprog.h
@@ -29,7 +29,15 @@ extern "C" {
    Because of the latter case, callers should use execlp/execvp, not
    execl/execv on the returned pathname.
    The returned string is freshly malloc()ed if it is != PROGNAME.  */
-extern const char *find_in_path (const char *progname);
+#define find_in_path(_p) find_in_path_str(_p, 0)
+
+
+/* Look up a program in the provided path.
+   Behaves the same as find_in_path() but looks in the provided path string
+   rather than in the PATH environment variable.  If path is null then use
+   the PATH environment variable.
+   The returned string is freshly malloc()ed if it is != PROGNAME.  */
+extern const char *find_in_path_str (const char *progname, const char *path);
 
 
 #ifdef __cplusplus
-- 
2.18.0




reply via email to

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