Code

Fixed reading too many argv parameters and changed to asprintf
[nagiosplug.git] / plugins / check_udp.c
index f6b528dcaaa3ced63e5ac30d0d59f835ceb366a4..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;
@@ -125,7 +125,6 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"hostname", required_argument, 0, 'H'},
@@ -140,7 +139,6 @@ process_arguments (int argc, char **argv)
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                usage ("\n");
@@ -155,22 +153,21 @@ process_arguments (int argc, char **argv)
        }
 
        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
+               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 '?':                                                                       /* 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;
@@ -212,14 +209,14 @@ process_arguments (int argc, char **argv)
        }
 
        c = optind;
-       if (server_address == NULL && argv[c]) {
+       if (server_address == NULL && c < argc && argv[c]) {
                if (is_host (argv[c]) == FALSE)
                        usage ("Invalid host name/address\n");
                server_address = argv[c++];
        }
-       else {
+
+       if (server_address == NULL)
                usage ("Host name was not supplied\n");
-       }
 
        return c;
 }
@@ -233,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);
 }
 
 
@@ -243,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");