Code

test GNU_SOURCE and include features.h if present to clear warning about asprintf...
[nagiosplug.git] / plugins / negate.c
index c76f5ca08cddddddc6bd50fdeb48c1cc0dc24741..d1fcb5dd6bd7fd45ed68f1926ec5c59194cda6f2 100644 (file)
@@ -23,7 +23,7 @@
  *
  *****************************************************************************/
 
-#define PROGNAME "negate"
+const char *progname = "negate";
 #define REVISION "$Revision$"
 #define COPYRIGHT "2002"
 #define AUTHOR "Karl DeBisschop"
 #define SUMMARY "Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n"
 
 #define OPTIONS "\
-\[-t timeout] <definition of wrapped plugin>"
+[-t timeout] <definition of wrapped plugin>"
 
 #define LONGOPTIONS "\
   -t, --timeout=INTEGER\n\
     Terminate test if timeout limit is exceeded (default: %d)\n\
      [keep this less than the plugin timeout to retain CRITICAL status]\n"
 
+#define EXAMPLES "\
+  negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
+    Run check_ping and invert result. Must use full path to plugin\n\
+  negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
+    Use single quotes if you need to retain spaces\n"
+
 #define DESCRIPTION "\
 This plugin is a wrapper to take the output of another plugin and invert it.\n\
 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
@@ -52,10 +58,10 @@ Otherwise, the output state of the wrapped plugin is unchanged.\n"
 
 char *command_line;
 
-static int process_arguments (int, char **);
-static int validate_arguments (void);
-static void print_usage (void);
-static void print_help (void);
+int process_arguments (int, char **);
+int validate_arguments (void);
+void print_usage (void);
+void print_help (void);
 \f
 /******************************************************************************
 
@@ -76,7 +82,7 @@ Please note that all tags must be lowercase to use the DocBook XML DTD.
 <manvolnum>5<manvolnum>
 </refmeta>
 <refnamdiv>
-<refname>&PROGNAME;</refname>
+<refname>&progname;</refname>
 <refpurpose>&SUMMARY;</refpurpose>
 </refnamdiv>
 </refentry>
@@ -117,7 +123,7 @@ main (int argc, char **argv)
        char input_buffer[MAX_INPUT_BUFFER];
 
        if (process_arguments (argc, argv) == ERROR)
-               usage ("Could not parse arguments");
+               usage ("Could not parse arguments\n");
 
        /* Set signal handling and alarm */
        if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
@@ -174,14 +180,14 @@ main (int argc, char **argv)
 void
 print_help (void)
 {
-       print_revision (PROGNAME, REVISION);
+       print_revision (progname, REVISION);
        printf
                ("Copyright (c) %s %s <%s>\n\n%s\n",
                 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
        print_usage ();
        printf
-               ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n", 
-                DEFAULT_TIMEOUT);
+               ("\nOptions:\n" LONGOPTIONS "\n" "Examples:\n" EXAMPLES "\n"
+                DESCRIPTION "\n", DEFAULT_TIMEOUT);
        support ();
 }
 
@@ -189,14 +195,9 @@ void
 print_usage (void)
 {
        printf ("Usage:\n" " %s %s\n"
-#ifdef HAVE_GETOPT_H
                                        " %s (-h | --help) for detailed help\n"
                                        " %s (-V | --version) for version information\n",
-#else
-                                       " %s -h for detailed help\n"
-                                       " %s -V for version information\n",
-#endif
-                                       PROGNAME, OPTIONS, PROGNAME, PROGNAME);
+                                       progname, OPTIONS, progname, progname);
 }
 \f
 
@@ -222,7 +223,6 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"help", no_argument, 0, 'h'},
@@ -230,15 +230,11 @@ process_arguments (int argc, char **argv)
                {"timeout", required_argument, 0, 't'},
                {0, 0, 0, 0}
        };
-#endif
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c = getopt_long (argc, argv, "hVt:",
+               c = getopt_long (argc, argv, "+hVt:",
                                 long_options, &option_index);
-#else
-               c = getopt (argc, argv, "hVt:");
-#endif
+
                if (c == -1 || c == EOF)
                        break;
 
@@ -249,7 +245,7 @@ process_arguments (int argc, char **argv)
                        print_help ();
                        exit (EXIT_SUCCESS);
                case 'V':     /* version */
-                       print_revision (PROGNAME, REVISION);
+                       print_revision (progname, REVISION);
                        exit (EXIT_SUCCESS);
                case 't':     /* timeout period */
                        if (!is_integer (optarg))
@@ -259,8 +255,8 @@ process_arguments (int argc, char **argv)
                }
        }
 
-       command_line = strscpy (command_line, argv[optind]);
-       for (c = optind+1; c <= argc; c++) {
+       asprintf (&command_line, "%s", argv[optind]);
+       for (c = optind+1; c < argc; c++) {
                asprintf (&command_line, "%s %s", command_line, argv[c]);
        }
 
@@ -282,6 +278,8 @@ process_arguments (int argc, char **argv)
 int
 validate_arguments ()
 {
+       if (command_line == NULL)
+               return ERROR;
        return STATE_OK;
 }
 \f