From: Karel Zak Date: Mon, 4 May 2026 11:57:41 +0000 (+0200) Subject: lib: split ul_default_shell() from shells.c into default_shell.c X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=97cd9a70e7bf20a967cc25b1b577e23ebe472628;p=thirdparty%2Futil-linux.git lib: split ul_default_shell() from shells.c into default_shell.c Move ul_default_shell() and UL_SHELL_* flags into a new lib/default_shell.c and include/default_shell.h pair, and add the new file to libcommon (autotools and meson). ul_default_shell() has no dependency on libeconf -- it simply checks $SHELL, passwd, and falls back to _PATH_BSHELL. Separating it from shells.c (which depends on libeconf for /etc/shells enumeration) allows binaries that only need the default shell lookup (flock, script, scriptlive, more, exec_shell.c users) to avoid unnecessary libeconf linkage. Update callers that only use ul_default_shell() to include default_shell.h instead of shells.h. Signed-off-by: Karel Zak --- diff --git a/include/Makemodule.am b/include/Makemodule.am index 8a9d6be00..adf103d14 100644 --- a/include/Makemodule.am +++ b/include/Makemodule.am @@ -21,6 +21,7 @@ dist_noinst_HEADERS += \ include/crc64.h \ include/c_strtod.h \ include/debug.h \ + include/default_shell.h \ include/encode.h \ include/env.h \ include/exec_shell.h \ diff --git a/include/default_shell.h b/include/default_shell.h new file mode 100644 index 000000000..8b493bc3d --- /dev/null +++ b/include/default_shell.h @@ -0,0 +1,14 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef UTIL_LINUX_DEFAULT_SHELL_H +#define UTIL_LINUX_DEFAULT_SHELL_H + +#include + +#define UL_SHELL_NOENV (1 << 0) +#define UL_SHELL_NOPWD (1 << 1) + +const char *ul_default_shell(int flags, const struct passwd *pw); + +#endif /* UTIL_LINUX_DEFAULT_SHELL_H */ diff --git a/include/shells.h b/include/shells.h index 9e9114f65..9a3306349 100644 --- a/include/shells.h +++ b/include/shells.h @@ -4,22 +4,14 @@ #ifndef UTIL_LINUX_SHELLS_H #define UTIL_LINUX_SHELLS_H -#include +#include #if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR) # include -#endif - -#define UL_SHELL_NOENV (1 << 0) -#define UL_SHELL_NOPWD (1 << 1) - -#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR) econf_file *open_etc_shells(void); #endif extern void print_shells(FILE *out, const char *format); extern int is_known_shell(const char *shell_name); -const char *ul_default_shell(int flags, const struct passwd *pw); - #endif /* UTIL_LINUX_SHELLS_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index cfaddf8e8..10947e3e8 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -22,6 +22,7 @@ libcommon_la_SOURCES = \ lib/crc64.c \ lib/c_strtod.c \ lib/debug.c \ + lib/default_shell.c \ lib/encode.c \ lib/env.c \ lib/fileutils.c \ diff --git a/lib/default_shell.c b/lib/default_shell.c new file mode 100644 index 000000000..acada7e40 --- /dev/null +++ b/lib/default_shell.c @@ -0,0 +1,28 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include + +#include "c.h" +#include "default_shell.h" + +const char *ul_default_shell(int flags, const struct passwd *pw) +{ + const char *shell = NULL; + + if (!(flags & UL_SHELL_NOENV)) { + shell = getenv("SHELL"); + if (shell && *shell) + return shell; + } + if (!(flags & UL_SHELL_NOPWD)) { + if (!pw) + pw = getpwuid(getuid()); + if (pw) + shell = pw->pw_shell; + if (shell && *shell) + return shell; + } + + return _PATH_BSHELL; +} diff --git a/lib/exec_shell.c b/lib/exec_shell.c index 8f10ea4df..4e842c4c7 100644 --- a/lib/exec_shell.c +++ b/lib/exec_shell.c @@ -26,7 +26,7 @@ #include "xalloc.h" #include "exec_shell.h" -#include "shells.h" +#include "default_shell.h" void __attribute__((__noreturn__)) exec_shell(void) { diff --git a/lib/meson.build b/lib/meson.build index 6b017f659..2164d57dd 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -8,6 +8,7 @@ lib_common_sources = ''' crc64.c c_strtod.c debug.c + default_shell.c encode.c env.c fileutils.c diff --git a/lib/shells.c b/lib/shells.c index ca7a8461d..0e4576058 100644 --- a/lib/shells.c +++ b/lib/shells.c @@ -121,24 +121,3 @@ extern int is_known_shell(const char *shell_name) #endif return ret; } - -const char *ul_default_shell(int flags, const struct passwd *pw) -{ - const char *shell = NULL; - - if (!(flags & UL_SHELL_NOENV)) { - shell = getenv("SHELL"); - if (shell && *shell) - return shell; - } - if (!(flags & UL_SHELL_NOPWD)) { - if (!pw) - pw = getpwuid(getuid()); - if (pw) - shell = pw->pw_shell; - if (shell && *shell) - return shell; - } - - return _PATH_BSHELL; -} diff --git a/sys-utils/flock.c b/sys-utils/flock.c index bfd735659..0708cf63b 100644 --- a/sys-utils/flock.c +++ b/sys-utils/flock.c @@ -47,7 +47,7 @@ #include "closestream.h" #include "monotonic.h" #include "timer.h" -#include "shells.h" +#include "default_shell.h" #ifndef F_OFD_GETLK #define F_OFD_GETLK 36 diff --git a/term-utils/script.c b/term-utils/script.c index 1da9402dd..ae5caaa87 100644 --- a/term-utils/script.c +++ b/term-utils/script.c @@ -70,7 +70,7 @@ #include "signames.h" #include "pty-session.h" #include "debug.h" -#include "shells.h" +#include "default_shell.h" #include "fileutils.h" static UL_DEBUG_DEFINE_MASK(script); diff --git a/term-utils/scriptlive.c b/term-utils/scriptlive.c index f36998a99..bf7c31244 100644 --- a/term-utils/scriptlive.c +++ b/term-utils/scriptlive.c @@ -38,7 +38,7 @@ #include "pty-session.h" #include "script-playutils.h" #include "monotonic.h" -#include "shells.h" +#include "default_shell.h" #define SCRIPT_MIN_DELAY 0.0001 /* from original scriptreplay.pl */ diff --git a/text-utils/more.c b/text-utils/more.c index aa53e289c..9cea37356 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -89,7 +89,7 @@ #include "widechar.h" #include "closestream.h" #include "env.h" -#include "shells.h" +#include "default_shell.h" #ifdef TEST_PROGRAM # define NON_INTERACTIVE_MORE 1