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

Matteo Croce matteo at openwrt.org
Wed Sep 9 10:14:12 UTC 2015


---
 frontend/saned.c | 80 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 38 insertions(+), 42 deletions(-)

diff --git a/frontend/saned.c b/frontend/saned.c
index 108512d..f1aaaaf 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");
     }
@@ -3264,6 +3263,8 @@ 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 +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




More information about the sane-devel mailing list