]> andersk Git - openssh.git/blobdiff - openbsd-compat/getopt.c
- dtucker@cvs.openbsd.org 2010/01/13 01:20:20
[openssh.git] / openbsd-compat / getopt.c
index 071e278855ecb2648e43d3f50c755f52561340eb..5450e43d957b031095191d9bcd1bb850114ab923 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * SUCH DAMAGE.
  */
 
-#include "config.h"
+/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
+
+#include "includes.h"
 #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: getopt.c,v 1.2 1996/08/19 08:33:32 tholo Exp $";
+static char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-int    opterr = 1,             /* if error message should be printed */
-       optind = 1,             /* index into parent argv vector */
-       optopt,                 /* character checked for validity */
-       optreset;               /* reset getopt */
-char   *optarg;                /* argument associated with option */
+int    BSDopterr = 1,          /* if error message should be printed */
+       BSDoptind = 1,          /* index into parent argv vector */
+       BSDoptopt,              /* character checked for validity */
+       BSDoptreset;            /* reset getopt */
+char   *BSDoptarg;             /* argument associated with option */
 
 #define        BADCH   (int)'?'
 #define        BADARG  (int)':'
@@ -57,7 +55,7 @@ char  *optarg;                /* argument associated with option */
  *     Parse argc/argv argument vector.
  */
 int
-getopt(nargc, nargv, ostr)
+BSDgetopt(nargc, nargv, ostr)
        int nargc;
        char * const *nargv;
        const char *ostr;
@@ -66,57 +64,60 @@ getopt(nargc, nargv, ostr)
        static char *place = EMSG;              /* option letter processing */
        char *oli;                              /* option letter list index */
 
-       if (optreset || !*place) {              /* update scanning pointer */
-               optreset = 0;
-               if (optind >= nargc || *(place = nargv[optind]) != '-') {
+       if (ostr == NULL)
+               return (-1);
+
+       if (BSDoptreset || !*place) {           /* update scanning pointer */
+               BSDoptreset = 0;
+               if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
                        place = EMSG;
                        return (-1);
                }
                if (place[1] && *++place == '-') {      /* found "--" */
-                       ++optind;
+                       ++BSDoptind;
                        place = EMSG;
                        return (-1);
                }
        }                                       /* option letter okay? */
-       if ((optopt = (int)*place++) == (int)':' ||
-           !(oli = strchr(ostr, optopt))) {
+       if ((BSDoptopt = (int)*place++) == (int)':' ||
+           !(oli = strchr(ostr, BSDoptopt))) {
                /*
                 * if the user didn't specify '-' as an option,
                 * assume it means -1.
                 */
-               if (optopt == (int)'-')
+               if (BSDoptopt == (int)'-')
                        return (-1);
                if (!*place)
-                       ++optind;
-               if (opterr && *ostr != ':')
+                       ++BSDoptind;
+               if (BSDopterr && *ostr != ':')
                        (void)fprintf(stderr,
-                           "%s: illegal option -- %c\n", __progname, optopt);
+                           "%s: illegal option -- %c\n", __progname, BSDoptopt);
                return (BADCH);
        }
        if (*++oli != ':') {                    /* don't need argument */
-               optarg = NULL;
+               BSDoptarg = NULL;
                if (!*place)
-                       ++optind;
+                       ++BSDoptind;
        }
        else {                                  /* need an argument */
                if (*place)                     /* no white space */
-                       optarg = place;
-               else if (nargc <= ++optind) {   /* no arg */
+                       BSDoptarg = place;
+               else if (nargc <= ++BSDoptind) {        /* no arg */
                        place = EMSG;
                        if (*ostr == ':')
                                return (BADARG);
-                       if (opterr)
+                       if (BSDopterr)
                                (void)fprintf(stderr,
                                    "%s: option requires an argument -- %c\n",
-                                   __progname, optopt);
+                                   __progname, BSDoptopt);
                        return (BADCH);
                }
                else                            /* white space */
-                       optarg = nargv[optind];
+                       BSDoptarg = nargv[BSDoptind];
                place = EMSG;
-               ++optind;
+               ++BSDoptind;
        }
-       return (optopt);                        /* dump back option letter */
+       return (BSDoptopt);                     /* dump back option letter */
 }
 
 #endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */
This page took 0.087822 seconds and 4 git commands to generate.