Behave normally when executed without any options.
Search only BASH_SOURCE_PATH for scripts to source
when isolated mode is enabled via the -i option.

Signed-off-by: Matheus Afonso Martins Moreira <math...@matheusmoreira.com>
---
 builtins/source.def | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/builtins/source.def b/builtins/source.def
index 26040e70..7f870bfe 100644
--- a/builtins/source.def
+++ b/builtins/source.def
@@ -83,6 +83,7 @@ extern int errno;
 static void uw_maybe_pop_dollar_vars (void *);
 static int execute_file_contents (WORD_LIST *, char *, char *);
 static char *search_for_file (WORD_LIST *);
+static char *isolated_search_for_file (WORD_LIST *);
 
 /* If non-zero, `.' uses $PATH to look up the script to be sourced. */
 int source_uses_path = 1;
@@ -177,6 +178,27 @@ search_for_file (WORD_LIST *list)
   return (filename);
 }
 
+static char *
+isolated_search_for_file (WORD_LIST *list)
+{
+  char *filename = NULL;
+
+  if (absolute_pathname (list->word->word))
+    filename = savestring (list->word->word);
+  else
+    filename = find_in_path_var (list->word->word, "BASH_SOURCE_PATH", 
FS_READABLE);
+
+  if (filename == 0)
+    {
+      if (source_searches_cwd == 0)
+        return (NULL);
+      else
+        filename = savestring (list->word->word);
+    }
+
+  return (filename);
+}
+
 /* Read and execute commands from the file passed as argument. */
 int
 source_builtin (WORD_LIST *list)
@@ -226,7 +248,10 @@ source_builtin (WORD_LIST *list)
     }
 #endif
 
-  filename = search_for_file (list);
+  if (!isolated_mode)
+    filename = search_for_file (list);
+  else
+    filename = isolated_search_for_file (list);
 
   if (filename == 0)
     {
-- 
2.44.0


Reply via email to