The PATH variable is hard coded in the user command finder function.
Transforming it into an argument allows reusing the file finding logic
with variables other than PATH, making the code more flexible.

Signed-off-by: Matheus Afonso Martins Moreira <math...@matheusmoreira.com>
---
 findcmd.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/findcmd.c b/findcmd.c
index 73c07bd8..fbcc0451 100644
--- a/findcmd.c
+++ b/findcmd.c
@@ -52,8 +52,8 @@ extern int errno;
 #endif
 
 /* Static functions defined and used in this file. */
-static char *_find_user_command_internal (const char *, int);
-static char *find_user_command_internal (const char *, int);
+static char *_find_user_command_internal (const char *, const char *, int);
+static char *find_user_command_internal (const char *, const char *, int);
 static char *find_user_command_in_path (const char *, char *, int, int *);
 static char *find_in_path_element (const char *, char *, int, size_t, struct 
stat *, int *);
 static char *find_absolute_program (const char *, int);
@@ -246,7 +246,7 @@ executable_or_directory (const char *file)
 char *
 find_user_command (const char *name)
 {
-  return (find_user_command_internal (name, FS_EXEC_PREFERRED|FS_NODIRS));
+  return (find_user_command_internal (name, "PATH", 
FS_EXEC_PREFERRED|FS_NODIRS));
 }
 
 /* Locate the file referenced by NAME, searching along the contents
@@ -257,7 +257,7 @@ find_user_command (const char *name)
 char *
 find_path_file (const char *name)
 {
-  return (find_user_command_internal (name, FS_READABLE));
+  return (find_user_command_internal (name, "PATH", FS_READABLE));
 }
 
 /* Get $PATH and normalize it. USE_TEMPENV, if non-zero, says to look in the
@@ -278,14 +278,15 @@ path_value (const char *pathvar, int use_tempenv)
 }
 
 static char *
-_find_user_command_internal (const char *name, int flags)
+_find_user_command_internal (const char *name, const char *path_var, int flags)
 {
   char *path_list, *cmd;
   SHELL_VAR *var;
 
-  /* Search for the value of PATH in both the temporary environments and
-     in the regular list of variables. */
-  path_list = path_value ("PATH", 1);
+  /* Search for the value of specified path variable
+     in both the temporary environments
+     and in the regular list of variables. */
+  path_list = path_value (path_var, 1);
 
   if (path_list == 0)
     return (savestring (name));
@@ -296,7 +297,7 @@ _find_user_command_internal (const char *name, int flags)
 }
 
 static char *
-find_user_command_internal (const char *name, int flags)
+find_user_command_internal (const char *name, const char *path_var, int flags)
 {
 #ifdef __WIN32__
   char *res, *dotexe;
@@ -304,13 +305,13 @@ find_user_command_internal (const char *name, int flags)
   dotexe = (char *)xmalloc (strlen (name) + 5);
   strcpy (dotexe, name);
   strcat (dotexe, ".exe");
-  res = _find_user_command_internal (dotexe, flags);
+  res = _find_user_command_internal (dotexe, path_var, flags);
   free (dotexe);
   if (res == 0)
-    res = _find_user_command_internal (name, flags);
+    res = _find_user_command_internal (name, path_var, flags);
   return res;
 #else
-  return (_find_user_command_internal (name, flags));
+  return (_find_user_command_internal (name, path_var, flags));
 #endif
 }
 
-- 
2.44.0


Reply via email to