The find_user_library function looks for bash library script files in the directories given by the BASH_LIBRARIES_PATH variable which are separate locations meant specifically for reusable modules.
An additional variable is introduced in order to avoid having to manipulate the PATH. Normally, the source builtin will search the directory entries in PATH which is meant for executables and commands. This means it could source an executable that is not meant to be loaded as a library. While this could be fixed by overriding PATH to contain only the library directories, doing so would interfere with the ability to execute commands in the library scripts which is a serious and unnecessary limitation. Introducing a new variable addresses both of these concerns and also improves performance by reducing the search space. Signed-off-by: Matheus Afonso Martins Moreira <math...@matheusmoreira.com> --- findcmd.c | 12 ++++++++++++ findcmd.h | 1 + 2 files changed, 13 insertions(+) diff --git a/findcmd.c b/findcmd.c index cef342e0..b4db1450 100644 --- a/findcmd.c +++ b/findcmd.c @@ -244,6 +244,18 @@ find_user_command (name) return (find_user_command_internal (name, "PATH", FS_EXEC_PREFERRED|FS_NODIRS)); } +/* Locate the script file referenced by NAME, searching along + the contents of the shell BASH_LIBRARIES_PATH variable. + Return a new string which is the full pathname to the file, + or NULL if the file couldn't be found. + Only files can be found, not directories. */ +char * +find_user_library (name) + const char *name; +{ + return (find_user_command_internal (name, "BASH_LIBRARIES_PATH", FS_NODIRS)); +} + /* Locate the file referenced by NAME, searching along the contents of the shell PATH variable. Return a new string which is the full pathname to the file, or NULL if the file couldn't be found. This diff --git a/findcmd.h b/findcmd.h index bf457814..3858df87 100644 --- a/findcmd.h +++ b/findcmd.h @@ -33,6 +33,7 @@ extern int executable_file PARAMS((const char *)); extern int is_directory PARAMS((const char *)); extern int executable_or_directory PARAMS((const char *)); extern char *find_user_command PARAMS((const char *)); +extern char *find_user_library PARAMS((const char *)); extern char *find_in_path PARAMS((const char *, char *, int)); extern char *find_path_file PARAMS((const char *)); extern char *search_for_command PARAMS((const char *, int)); -- 2.44.0