util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Sami Kerola <kerolasa@iki.fi>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH 14/16] chfn: clean up parse_argv()
Date: Mon, 15 Dec 2014 10:45:13 +0100	[thread overview]
Message-ID: <20141215094513.GY19904@x2.net.home> (raw)
In-Reply-To: <1418579052-29386-15-git-send-email-kerolasa@iki.fi>

On Sun, Dec 14, 2014 at 05:44:10PM +0000, Sami Kerola wrote:
> Use switch() case ?: for all option parsing, as in most of the other
> source files.
> 
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
>  login-utils/chfn.c | 48 ++++++++++++++++++------------------------------
>  1 file changed, 18 insertions(+), 30 deletions(-)
> 
> diff --git a/login-utils/chfn.c b/login-utils/chfn.c
> index 81abd11..0d37a32 100644
> --- a/login-utils/chfn.c
> +++ b/login-utils/chfn.c
> @@ -121,11 +121,11 @@ static int check_gecos_string(const char *msg, char *gecos)
>   *	parse the command line arguments.
>   *	returns true if no information beyond the username was given.
>   */
> -static void parse_argv(struct chfn_control *ctl, int argc, char *argv[], struct finfo *pinfo)
> +static void parse_argv(struct chfn_control *ctl, int argc, char *argv[],
> +		       struct finfo *pinfo)
>  {
> -	int index, c, status;
> -
> -	static struct option long_options[] = {
^^^^^^^^^^^^
> +	int index, c, status = 0;
> +	const struct option long_options[] = {

static

>  		{"full-name", required_argument, 0, 'f'},
>  		{"office", required_argument, 0, 'o'},
>  		{"office-phone", required_argument, 0, 'p'},
> @@ -135,49 +135,37 @@ static void parse_argv(struct chfn_control *ctl, int argc, char *argv[], struct
>  		{NULL, no_argument, 0, '0'},
>  	};
>  
> -	optind = 0;
> -	while (true) {
> -		c = getopt_long(argc, argv, "f:r:p:h:o:uv", long_options,
> -				&index);
> -		if (c == -1)
> -			break;
> -		/* version?  output version and exit. */
> -		if (c == 'v') {
> -			printf(UTIL_LINUX_VERSION);
> -			exit(EXIT_SUCCESS);
> -		}
> -		if (c == 'u')
> -			usage(stdout);
> -		/* all other options must have an argument. */
> -		if (!optarg)
> -			usage(stderr);
> -		/* ok, we were given an argument */
> -		ctl->interactive = 0;
> -
> -		/* now store the argument */
> +	while ((c = getopt_long(argc, argv, "f:r:p:h:o:uv", long_options,
> +				&index)) != -1) {
>  		switch (c) {
>  		case 'f':
>  			pinfo->full_name = optarg;
> -			status = check_gecos_string(_("Name"), optarg);
> +			status += check_gecos_string(_("Name"), optarg);
>  			break;
>  		case 'o':
>  			pinfo->office = optarg;
> -			status = check_gecos_string(_("Office"), optarg);
> +			status += check_gecos_string(_("Office"), optarg);
>  			break;
>  		case 'p':
>  			pinfo->office_phone = optarg;
> -			status = check_gecos_string(_("Office Phone"), optarg);
> +			status += check_gecos_string(_("Office Phone"), optarg);
>  			break;
>  		case 'h':
>  			pinfo->home_phone = optarg;
> -			status = check_gecos_string(_("Home Phone"), optarg);
> +			status += check_gecos_string(_("Home Phone"), optarg);
>  			break;
> +		case 'v':
> +			printf(UTIL_LINUX_VERSION);
> +			exit(EXIT_SUCCESS);
> +		case 'u':
> +			usage(stdout);
>  		default:
>  			usage(stderr);
>  		}
> -		if (status != 0)
> -			exit(EXIT_FAILURE);
> +		ctl->interactive = 0;
>  	}
> +	if (status != 0)
> +		exit(EXIT_FAILURE);
>  	/* done parsing arguments.  check for a username. */
>  	if (optind < argc) {
>  		if (optind + 1 < argc)
> -- 
> 2.1.3
> 
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

  reply	other threads:[~2014-12-15  9:45 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-14 17:43 [PATCH 00/16] pull: updates to lslogins and chfn Sami Kerola
2014-12-14 17:43 ` [PATCH 01/16] lslogins: allow changing password changed and expiration time formats Sami Kerola
2014-12-14 17:43 ` [PATCH 02/16] lslogins: make journald last logs time stamps to honor --time-format Sami Kerola
2014-12-14 17:43 ` [PATCH 03/16] lslogins: tell why command failed Sami Kerola
2014-12-14 17:44 ` [PATCH 04/16] lslogins: fix short options Sami Kerola
2014-12-14 17:44 ` [PATCH 05/16] lslogins: reject unknown time format arguments Sami Kerola
2014-12-15  9:28   ` Karel Zak
2014-12-19  9:30     ` Sami Kerola
2014-12-14 17:44 ` [PATCH 06/16] lslogins: add space to systemd journal header and message Sami Kerola
2014-12-14 17:44 ` [PATCH 07/16] lslogins: use hardcoded paths from pathnames.h Sami Kerola
2014-12-14 17:44 ` [PATCH 08/16] chfn: remove function prototypes Sami Kerola
2014-12-14 17:44 ` [PATCH 09/16] chfn: rewrite prompt() to use strutils Sami Kerola
2014-12-14 17:44 ` [PATCH 10/16] chfn: use xasprintf() rather than bunch of strlen() and malloc() calls Sami Kerola
2014-12-14 17:44 ` [PATCH 11/16] chfn: fix usage() regression Sami Kerola
2014-12-14 17:44 ` [PATCH 12/16] chfn: simplify parse_passwd() by using strsep() Sami Kerola
2014-12-14 17:44 ` [PATCH 13/16] chfn: add minimalistic struct chfn_control Sami Kerola
2014-12-14 17:44 ` [PATCH 14/16] chfn: clean up parse_argv() Sami Kerola
2014-12-15  9:45   ` Karel Zak [this message]
2014-12-14 17:44 ` [PATCH 15/16] chfn: move new and old finger structs to chfn control struct Sami Kerola
2014-12-15 10:07   ` Karel Zak
2014-12-19  9:41     ` Sami Kerola
2014-12-14 17:44 ` [PATCH 16/16] chfn: make command to obey login.defs CHFN_RESTRICT instructions Sami Kerola
2014-12-15 10:11   ` Karel Zak
2014-12-19 13:30 ` [PATCH 00/16] pull: updates to lslogins and chfn Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141215094513.GY19904@x2.net.home \
    --to=kzak@redhat.com \
    --cc=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).