Extract the file searching algorithm of the source builtin into a static helper function. Makes the code easier to understand and separates the searching from the error handling logic.
Signed-off-by: Matheus Afonso Martins Moreira <math...@matheusmoreira.com> --- builtins/source.def | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/builtins/source.def b/builtins/source.def index b1567aab..f7c37a14 100644 --- a/builtins/source.def +++ b/builtins/source.def @@ -87,6 +87,30 @@ int source_uses_path = 1; is not found in the $PATH. */ int source_searches_cwd = 1; +static char * +search_for_file (list) + WORD_LIST *list; +{ + char *filename = NULL, *x; + + /* XXX -- should this be absolute_pathname? */ + if (posixly_correct && strchr (list->word->word, '/')) + filename = savestring (list->word->word); + else if (absolute_pathname (list->word->word)) + filename = savestring (list->word->word); + else if (source_uses_path) + filename = find_path_file (list->word->word); + 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 (list) @@ -113,31 +137,20 @@ source_builtin (list) } #endif - filename = (char *)NULL; - /* XXX -- should this be absolute_pathname? */ - if (posixly_correct && strchr (list->word->word, '/')) - filename = savestring (list->word->word); - else if (absolute_pathname (list->word->word)) - filename = savestring (list->word->word); - else if (source_uses_path) - filename = find_path_file (list->word->word); + filename = search_for_file (list); + if (filename == 0) { - if (source_searches_cwd == 0) - { - x = printable_filename (list->word->word, 0); - builtin_error (_("%s: file not found"), x); - if (x != list->word->word) - free (x); - if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) - { - last_command_exit_value = EXECUTION_FAILURE; - jump_to_top_level (EXITPROG); - } - return (EXECUTION_FAILURE); - } - else - filename = savestring (list->word->word); + x = printable_filename (list->word->word, 0); + builtin_error (_("%s: file not found"), x); + if (x != list->word->word) + free (x); + if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) + { + last_command_exit_value = EXECUTION_FAILURE; + jump_to_top_level (EXITPROG); + } + return (EXECUTION_FAILURE); } return execute_file_contents (list, filename, "source"); -- 2.44.0