[sane-devel] [PATCH 1/2] use getopt()

Olaf Meeuwissen paddy-hack at member.fsf.org
Sat Sep 12 06:50:26 UTC 2015


Hi Matteo,

I reviewed this patch and it looks *mostly* okay.  The only place where
I think you dropped the ball a bit is in the command-line option error
handling.  The original code outputs a usage message when it finds the
help option as well as when it encounters anything unexpected.  It also
returns a suitable exit value whereas your code does not.

Could you fix that?

Matteo Croce writes:

> [snip]
> @@ -3274,35 +3275,26 @@ main (int argc, char *argv[])
>    numchildren = 0;
>    run_mode = SANED_RUN_INETD;
>  
> -  if (argc >= 2)
> -    {
> -      if (strncmp (argv[1], "-a", 2) == 0)
> -	run_mode = SANED_RUN_ALONE;
> -      else if (strncmp (argv[1], "-d", 2) == 0)
> -	{
> -	  run_mode = SANED_RUN_DEBUG;
> -	  log_to_syslog = SANE_FALSE;
> -	}
> -      else if (strncmp (argv[1], "-s", 2) == 0)
> -	run_mode = SANED_RUN_DEBUG;
> -      else
> -        {
> -          printf ("Usage: saned [ -a [ username ] | -d [ n ] | -s [ n ] ] | -h\n");
> -          if ((strncmp (argv[1], "-h", 2) == 0) ||
> -               (strncmp (argv[1], "--help", 6) == 0))
> -            exit (EXIT_SUCCESS);
> -          else
> -            exit (EXIT_FAILURE);
> -        }
> +  while((c = getopt(argc, argv, "a:d:s:h")) != -1)
> +  {
> +    switch(c) {
> +    case 'a':
> +      run_mode = SANED_RUN_ALONE;
> +      user = optarg;
> +      break;
> +    case 'd':
> +      log_to_syslog = SANE_FALSE;
> +    case 's':
> +      run_mode = SANED_RUN_DEBUG;
> +      debug = atoi(optarg);
> +      break;
> +    case 'h':
> +      printf ("Usage: %s [ -a [ username ] | -d [ n ] | -s [ n ] ] | -h\n", argv[0]);
> +      return;
>      }
> -
> +  }
>    if (run_mode == SANED_RUN_DEBUG)
> -    {
> -      if (argv[1][2])
> -	debug = atoi (argv[1] + 2);
> -
>        DBG (DBG_WARN, "main: starting debug mode (level %d)\n", debug);
> -    }
>  
>    if (log_to_syslog)
>      openlog ("saned", LOG_PID | LOG_CONS, LOG_DAEMON);
> @@ -3342,11 +3334,15 @@ main (int argc, char *argv[])
>  
>    if ((run_mode == SANED_RUN_ALONE) || (run_mode == SANED_RUN_DEBUG))
>      {
> -      run_standalone(argc, argv);
> +      run_standalone(user);
>      }
>    else
>      {
> -      run_inetd(argc, argv);
> +#ifdef HAVE_OS2_H
> +      run_inetd(argv[1]);
> +#else
> +      run_inetd();
> +#endif
>      }
>  
>    DBG (DBG_WARN, "saned exiting\n");
> -- 
> 2.1.4

-- 
Sent with my mu4e



More information about the sane-devel mailing list