bug-bash
[Top][All Lists]
Advanced

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

[PATCH 2/9] findcmd: parameterize path variable in functions


From: Matheus Afonso Martins Moreira
Subject: [PATCH 2/9] findcmd: parameterize path variable in functions
Date: Sun, 5 May 2024 06:54:55 -0300

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 <matheus@matheusmoreira.com>
---
 findcmd.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/findcmd.c b/findcmd.c
index 95f231e5..cef342e0 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 PARAMS((const char *, int));
-static char *find_user_command_internal PARAMS((const char *, int));
+static char *_find_user_command_internal PARAMS((const char *, const char *, 
int));
+static char *find_user_command_internal PARAMS((const char *, const char *, 
int));
 static char *find_user_command_in_path PARAMS((const char *, char *, int, int 
*));
 static char *find_in_path_element PARAMS((const char *, char *, int, int, 
struct stat *, int *));
 static char *find_absolute_program PARAMS((const char *, int));
@@ -241,7 +241,7 @@ char *
 find_user_command (name)
      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
@@ -253,12 +253,12 @@ char *
 find_path_file (name)
      const char *name;
 {
-  return (find_user_command_internal (name, FS_READABLE));
+  return (find_user_command_internal (name, "PATH", FS_READABLE));
 }
 
 static char *
-_find_user_command_internal (name, flags)
-     const char *name;
+_find_user_command_internal (name, path_var, flags)
+     const char *name, *path_var;
      int flags;
 {
   char *path_list, *cmd;
@@ -266,7 +266,7 @@ _find_user_command_internal (name, flags)
 
   /* Search for the value of PATH in both the temporary environments and
      in the regular list of variables. */
-  if (var = find_variable_tempenv ("PATH"))    /* XXX could be array? */
+  if (var = find_variable_tempenv (path_var))  /* XXX could be array? */
     path_list = value_cell (var);
   else
     path_list = (char *)NULL;
@@ -280,8 +280,8 @@ _find_user_command_internal (name, flags)
 }
 
 static char *
-find_user_command_internal (name, flags)
-     const char *name;
+find_user_command_internal (name, path_var, flags)
+     const char *name, *path_var;
      int flags;
 {
 #ifdef __WIN32__
@@ -290,13 +290,14 @@ find_user_command_internal (name, 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);
+     const char *name;
   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

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