use snprintf, strdup, ... where possible to make for safer operation -- Martin Pelikan
[rrdtool.git] / src / rrd_getopt.c
index 706a67a..7d157a0 100644 (file)
 #define _NO_PROTO
 #endif
 
-#ifdef HAVE_CONFIG_H
-#include "../rrd_config.h"
-#endif
 
-#if !defined (__STDC__) || !__STDC__
+#if !defined WIN32 && (!defined (__STDC__) || !__STDC__)
 /* This is a separate conditional since some stdc systems
    reject `defined (const)'.  */
 #ifndef const
 #endif
 #endif
 
+#ifndef WIN32
+#ifdef HAVE_CONFIG_H
+#include "../rrd_config.h"
+#endif
+#endif
+
+#include "rrd_i18n.h"
+
+
 #include <stdio.h>
 
 /* Comment out all this code if we are using the GNU C Library, and are not
 #define getpid() GetCurrentProcessId()
 #endif
 
-#ifndef _
-/* This is for other GNU distributions with internationalized messages.
-   When compiling libc, the _ macro is predefined.  */
-#ifdef HAVE_LIBINTL_H
-# include <libintl.h>
-# define _(msgid)      gettext (msgid)
-#else
-# define _(msgid)      (msgid)
-#endif
-#endif
-
 /* This version of `getopt' appears to the caller like standard Unix `getopt'
    but it behaves differently for the user, since it allows the user
    to intersperse the options with the other arguments.
    Also, when `ordering' is RETURN_IN_ORDER,
    each non-option ARGV-element is returned here.  */
 
+/*
+ * On some versions of Solaris, opterr and friends are defined in core libc
+ * rather than in a separate getopt module.  Define these variables only
+ * if configure found they aren't there by default.  (We assume that testing
+ * opterr is sufficient for all of these except optreset.)
+ */
+#ifndef HAVE_INT_OPTERR
+
 char     *optarg = NULL;
 
 /* Index in ARGV of the next element to be scanned.
@@ -132,9 +135,28 @@ char     *optarg = NULL;
    Otherwise, `optind' communicates from one call to the next
    how much of ARGV has been scanned so far.  */
 
+/* Callers store zero here to inhibit the error message
+   for unrecognized options.  */
+
+int       opterr = 1;
+
+/* Set to an option character which was unrecognized.
+   This must be initialized on some systems to avoid linking in the
+   system's own getopt implementation.  */
+
+int       optopt = '?';
+
 /* 1003.2 says this must be 1 before any call.  */
 int       optind = 1;
 
+#else
+ extern int      opterr;
+ extern int      optind;
+ extern int      optopt;
+ extern char     *optarg;
+#endif
+
+
 /* Formerly, initialization of getopt depended on optind==0, which
    causes problems with re-calling getopt as programs generally don't
    know that. */
@@ -150,16 +172,6 @@ int       __getopt_initialized = 0;
 
 static char *nextchar;
 
-/* Callers store zero here to inhibit the error message
-   for unrecognized options.  */
-
-int       opterr = 1;
-
-/* Set to an option character which was unrecognized.
-   This must be initialized on some systems to avoid linking in the
-   system's own getopt implementation.  */
-
-int       optopt = '?';
 
 /* Describe how to deal with options that follow non-option ARGV-elements.
 
@@ -214,11 +226,7 @@ static char *posixly_correct;
 char     *getenv(
     );
 
-static char *my_index(
-    str,
-    chr)
-    const char *str;
-    int chr;
+static char* my_index(const char* str, int chr)
 {
     while (*str) {
         if (*str == chr)
@@ -267,7 +275,8 @@ static char *const *original_argv;
    to getopt is that one passed to the process.  */
 static void store_args(
     int argc,
-    char *const *argv) __attribute__ ((unused));
+    char *const *argv);
+
 static void store_args(
     int argc,
     char *const *argv)
@@ -295,9 +304,7 @@ static void exchange(
     char **);
 #endif
 
-static void exchange(
-    argv)
-    char    **argv;
+static void exchange(char** argv)
 {
     int       bottom = first_nonopt;
     int       middle = last_nonopt;
@@ -353,13 +360,9 @@ static const char *_getopt_initialize(
     char *const *,
     const char *);
 #endif
-static const char *_getopt_initialize(
-    argc,
-    argv,
-    optstring)
-    int argc;
-    char     *const *argv;
-    const char *optstring;
+static const char* _getopt_initialize(int argc,
+                                      char* const* argv,
+                                      const char* optstring)
 {
     /* Start processing options with ARGV-element 1 (since ARGV-element 0
        is the program name); the sequence of previously skipped
@@ -393,7 +396,7 @@ static const char *_getopt_initialize(
            considered as options.  */
         char      var[100];
 
-        sprintf(var, "_%d_GNU_nonoption_argv_flags_", getpid());
+        snprintf(var, sizeof var, "_%d_GNU_nonoption_argv_flags_", getpid());
         nonoption_flags = getenv(var);
         if (nonoption_flags == NULL)
             nonoption_flags_len = 0;
@@ -462,19 +465,16 @@ static const char *_getopt_initialize(
    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
    long-named options.  */
 
-int _getopt_internal(
-    argc,
-    argv,
-    optstring,
-    longopts,
-    longind,
-    long_only)
-    int argc;
-    char     *const *argv;
-    const char *optstring;
-    const struct option *longopts;
-    int      *longind;
-    int long_only;
+int _getopt_internal(int argc,
+#ifdef WIN32
+                     char** argv,
+#else // WIN32
+                     char* const* argv,
+#endif //WIN32
+                     const char *optstring,
+                     const struct option *longopts,
+                     int* longind,
+                     int long_only)
 {
     optarg = NULL;
 
@@ -874,18 +874,6 @@ int _getopt_internal(
     }
 }
 
-int getopt(
-    argc,
-    argv,
-    optstring)
-    int argc;
-    char     *const *argv;
-    const char *optstring;
-{
-    return _getopt_internal(argc, argv, optstring,
-                            (const struct option *) 0, (int *) 0, 0);
-}
-
 #endif                          /* Not ELIDE_CODE.  */
 \f
 #ifdef TEST