Implement command line parsing using getopt, replace the help string with a nicer one. --- set proper return value with invalid arguments make arguments to -a -d and -s optional
frontend/saned.c | 90 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/frontend/saned.c b/frontend/saned.c index 108512d..5ef7c23 100644 --- a/frontend/saned.c +++ b/frontend/saned.c @@ -2923,7 +2923,7 @@ do_bindings (int *nfds, struct pollfd **fds) static void -run_standalone (int argc, char **argv) +run_standalone (char *user) { struct pollfd *fds = NULL; struct pollfd *fdp = NULL; @@ -2944,13 +2944,13 @@ run_standalone (int argc, char **argv) if (run_mode != SANED_RUN_DEBUG) { - if (argc > 2) + if (user) { - pwent = getpwnam(argv[2]); + pwent = getpwnam(user); if (pwent == NULL) { - DBG (DBG_ERR, "FATAL ERROR: user %s not found on system\n", argv[2]); + DBG (DBG_ERR, "FATAL ERROR: user %s not found on system\n", user); bail_out (1); } @@ -2981,7 +2981,7 @@ run_standalone (int argc, char **argv) while (grp->gr_mem[i]) { - if (strcmp(grp->gr_mem[i], argv[2]) == 0) + if (strcmp(grp->gr_mem[i], user) == 0) { int need_to_add = 1, j; @@ -3172,7 +3172,11 @@ run_standalone (int argc, char **argv) static void -run_inetd (int argc, char **argv) +#ifdef HAVE_OS2_H +run_inetd (char *sock) +#else +run_inetd () +#endif { int fd = -1; @@ -3238,18 +3242,13 @@ run_inetd (int argc, char **argv) close (dave_null); } -#ifndef HAVE_OS2_H - /* Unused in this function */ - argc = argc; - argv = argv; - -#else +#ifdef HAVE_OS2_H /* under OS/2, the socket handle is passed as argument on the command line; the socket handle is relative to IBM TCP/IP, so a call to impsockethandle() is required to add it to the EMX runtime */ - if (argc == 2) + if (sock) { - fd = _impsockhandle (atoi (argv[1]), 0); + fd = _impsockhandle (atoi (sock), 0); if (fd == -1) perror ("impsockhandle"); } @@ -3258,12 +3257,19 @@ run_inetd (int argc, char **argv) handle_connection(fd); } +void usage(char *me, int err) +{ + fprintf (stderr, "Usage: %s [-a username] [-d n] [-s n] [-h]\n", me); + exit(err); +} int main (int argc, char *argv[]) { char options[64] = ""; debug = DBG_WARN; + int c; + char *user = NULL; prog_name = strrchr (argv[0], '/'); if (prog_name) @@ -3274,35 +3280,31 @@ 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': + usage(argv[0], EXIT_SUCCESS); + default: + usage(argv[0], EXIT_FAILURE); } - + } +#ifndef HAVE_OS2_H + if(optind != argc) + usage(argv[0], EXIT_FAILURE); +#endif 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 +3344,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 -- sane-devel mailing list: sane-devel@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel Unsubscribe: Send mail with subject "unsubscribe your_password" to sane-devel-requ...@lists.alioth.debian.org