From 7ed7e14dee481aa982f021eaf6ff04dbd46c262c Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Mon, 31 Dec 2007 14:52:52 +0000 Subject: [PATCH] Split process_flags() out of main(). --- ChangeLog | 4 +++ src/chsh.c | 79 +++++++++++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 05879f0e..f18bac43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2007-12-31 Nicolas François + + * src/chsh.c: Split process_flags() out of main(). + 2007-12-31 Nicolas François * src/chfn.c: New function: process_flags() split out of main(). diff --git a/src/chsh.c b/src/chsh.c index b34a43d2..008da868 100644 --- a/src/chsh.c +++ b/src/chsh.c @@ -71,6 +71,7 @@ static void usage (void); static void new_fields (void); static int check_shell (const char *); static int restricted_shell (const char *); +static void process_flags (int argc, char **argv); /* * usage - print command line syntax and exit @@ -167,6 +168,46 @@ static int check_shell (const char *sh) return found; } +/* + * * process_flags - parse the command line options + * + * It will not return if an error is encountered. + */ +static void process_flags (int argc, char **argv) +{ + int option_index = 0; + int c; + static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"shell", required_argument, NULL, 's'}, + {NULL, 0, NULL, '\0'} + }; + + while ((c = + getopt_long (argc, argv, "hs:", long_options, + &option_index)) != -1) { + switch (c) { + case 'h': + usage (); + break; + case 's': + sflg++; + STRFCPY (loginsh, optarg); + break; + default: + usage (); + } + } + + /* + * There should be only one remaining argument at most and it should + * be the user's name. + */ + if (argc > (optind + 1)) { + usage (); + } +} + /* * chsh - this command controls changes to the user's shell * @@ -204,42 +245,8 @@ int main (int argc, char **argv) OPENLOG ("chsh"); - /* - * There is only one option, but use getopt() anyway to - * keep things consistent. - */ - { - int option_index = 0; - int c; - static struct option long_options[] = { - {"help", no_argument, NULL, 'h'}, - {"shell", required_argument, NULL, 's'}, - {NULL, 0, NULL, '\0'} - }; - - while ((c = - getopt_long (argc, argv, "hs:", long_options, - &option_index)) != -1) { - switch (c) { - case 'h': - usage (); - break; - case 's': - sflg++; - STRFCPY (loginsh, optarg); - break; - default: - usage (); - } - } - } - - /* - * There should be only one remaining argument at most and it should - * be the user's name. - */ - if (argc > optind + 1) - usage (); + /* parse the command line options */ + process_flags (argc, argv); /* * Get the name of the user to check. It is either the command line