From: Karel Zak Date: Mon, 5 Jun 2017 11:15:59 +0000 (+0200) Subject: chsh: split get_shell_list() X-Git-Tag: v2.31-rc1~340 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=54373fb9b910f5fe64f1d14a6db36437063b4983;p=thirdparty%2Futil-linux.git chsh: split get_shell_list() Let's use two functions is_known_shell() and print_shells() to make the code more readable and to avoid complex semantic of the original get_shell_list(). Signed-off-by: Karel Zak --- diff --git a/login-utils/chsh.c b/login-utils/chsh.c index c6eab15541..7d67493a46 100644 --- a/login-utils/chsh.c +++ b/login-utils/chsh.c @@ -136,28 +136,35 @@ static int init_shells(char ***shells) } /* - * get_shell_list () -- if the given shell appears in /etc/shells, + * is_known_shell() -- if the given shell appears in /etc/shells, * return true. if not, return false. - * if the given shell is NULL, /etc/shells is outputted to stdout. */ -static int get_shell_list(const char *shell_name, char ***shells) +static int is_known_shell(const char *shell_name, char ***shells) { char **s; - int found = 0; - if (!shells) - return found; - s = *shells; + if (!shells || !shell_name) + return 0; + for (s = *shells; *s; s++) { - if (shell_name) { - if (!strcmp(shell_name, *s)) { - found = 1; - break; - } - } else - printf("%s\n", *s); + if (strcmp(shell_name, *s) == 0) + return 1; } - return found; + return 0; +} + +/* + * print_shells () -- /etc/shells is outputted to stdout. + */ +static void print_shells(char ***shells) +{ + char **s; + + if (!shells) + return; + + for (s = *shells; *s; s++) + printf("%s\n", *s); } #ifdef HAVE_LIBREADLINE @@ -212,7 +219,7 @@ static void parse_argv(int argc, char **argv, struct sinfo *pinfo, char ***shell usage(stdout); case 'l': init_shells(shells); - get_shell_list(NULL, shells); + print_shells(shells); exit(EXIT_SUCCESS); case 's': if (!optarg) @@ -275,7 +282,7 @@ static void check_shell(const char *shell, char ***shells) errx(EXIT_FAILURE, _("\"%s\" is not executable"), shell); if (illegal_passwd_chars(shell)) errx(EXIT_FAILURE, _("%s: has illegal characters"), shell); - if (!get_shell_list(shell, shells)) { + if (!is_known_shell(shell, shells)) { #ifdef ONLY_LISTED_SHELLS if (!getuid()) warnx(_("Warning: \"%s\" is not listed in %s."), shell, @@ -367,7 +374,7 @@ int main(int argc, char **argv) "altering, shell change denied")); } init_shells(&global_shells); - if (uid != 0 && !get_shell_list(oldshell, &global_shells)) { + if (uid != 0 && !is_known_shell(oldshell, &global_shells)) { errno = EACCES; err(EXIT_FAILURE, _("your shell is not in %s, " "shell change denied"), _PATH_SHELLS);