Code

Code cleanup
[nagiosplug.git] / plugins / check_udp.c
index d00ce9c345fc94449e0f3173f795034f34edb24f..ac2210257d4f32d422fe2794b6a16063f89237f6 100644 (file)
@@ -41,7 +41,7 @@
 #include "netutils.h"
 #include "utils.h"
 
-#define PROGNAME "check_udp"
+const char *progname = "check_udp";
 
 int warning_time = 0;
 int check_warning_time = FALSE;
@@ -49,7 +49,6 @@ int critical_time = 0;
 int check_critical_time = FALSE;
 
 int process_arguments (int, char **);
-int call_getopt (int, char **);
 void print_usage (void);
 void print_help (void);
 
@@ -57,7 +56,7 @@ int verbose = FALSE;
 int server_port = 0;
 char *server_address = NULL;
 char *server_expect = NULL;
-char *server_send = NULL;
+char *server_send = "";
 
 int
 main (int argc, char **argv)
@@ -126,52 +125,6 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-       if (argc < 2)
-               usage ("\n");
-
-       for (c = 1; c < argc; c++) {
-               if (strcmp ("-to", argv[c]) == 0)
-                       strcpy (argv[c], "-t");
-               else if (strcmp ("-wt", argv[c]) == 0)
-                       strcpy (argv[c], "-w");
-               else if (strcmp ("-ct", argv[c]) == 0)
-                       strcpy (argv[c], "-c");
-       }
-
-       c = 0;
-       while ((c += call_getopt (argc - c, &argv[c])) < argc) {
-
-               if (is_option (argv[c]))
-                       continue;
-
-               if (server_address == NULL) {
-                       if (argc > c) {
-                               if (is_host (argv[c]) == FALSE)
-                                       usage ("Invalid host name/address\n");
-                               server_address = argv[c];
-                       }
-                       else {
-                               usage ("Host name was not supplied\n");
-                       }
-               }
-       }
-
-       if (server_send == NULL)
-               server_send = strscpy (server_send, "");
-
-       return OK;
-}
-
-
-
-
-
-int
-call_getopt (int argc, char **argv)
-{
-       int c, i = 0;
-
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"hostname", required_argument, 0, 'H'},
@@ -186,43 +139,35 @@ call_getopt (int argc, char **argv)
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
-       while (1) {
-#ifdef HAVE_GETOPT_H
-               c =
-                       getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options,
-                                                                        &option_index);
-#else
-               c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
-#endif
+       if (argc < 2)
+               usage ("\n");
+
+       for (c = 1; c < argc; c++) {
+               if (strcmp ("-to", argv[c]) == 0)
+                       strcpy (argv[c], "-t");
+               else if (strcmp ("-wt", argv[c]) == 0)
+                       strcpy (argv[c], "-w");
+               else if (strcmp ("-ct", argv[c]) == 0)
+                       strcpy (argv[c], "-c");
+       }
 
-               i++;
+       while (1) {
+               c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
 
                if (c == -1 || c == EOF || c == 1)
                        break;
 
-               switch (c) {
-               case 'H':
-               case 'c':
-               case 'w':
-               case 't':
-               case 'p':
-               case 'e':
-               case 's':
-                       i++;
-               }
-
                switch (c) {
                case '?':                                                                       /* print short usage statement if args not parsable */
-                       printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
+                       printf ("%s: Unknown argument: %s\n\n", progname, optarg);
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'h':                                                                       /* help */
                        print_help ();
                        exit (STATE_OK);
                case 'V':                                                                       /* version */
-                       print_revision (my_basename (argv[0]), "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
                case 'v':                                                                       /* verbose mode */
                        verbose = TRUE;
@@ -262,7 +207,18 @@ call_getopt (int argc, char **argv)
                        break;
                }
        }
-       return i;
+
+       c = optind;
+       if (server_address == NULL && c < argc && argv[c]) {
+               if (is_host (argv[c]) == FALSE)
+                       usage ("Invalid host name/address\n");
+               server_address = argv[c++];
+       }
+
+       if (server_address == NULL)
+               usage ("Host name was not supplied\n");
+
+       return c;
 }
 
 
@@ -274,7 +230,7 @@ print_usage (void)
 {
        printf
                ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
-                "         [-e expect] [-s send] [-t to_sec] [-v]\n", PROGNAME);
+                "         [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
 }
 
 
@@ -284,7 +240,7 @@ print_usage (void)
 void
 print_help (void)
 {
-       print_revision (PROGNAME, "$Revision$");
+       print_revision (progname, "$Revision$");
        printf
                ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
                 "This plugin tests an UDP connection with the specified host.\n\n");