]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/getopt.c
Cleanup extraneous debug
[openldap] / libraries / liblutil / getopt.c
index a19d9ba2c92aeabb7b92b120f52efe2856984b63..5e4e02273c3c9ac327666da239a6289246bfeac9 100644 (file)
@@ -1,21 +1,42 @@
-/*
-       getopt.c
+/* getopt.c -- replacement getopt(3) routines */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2007 The OpenLDAP Foundation.
+ * Portions Copyright 1998-2003 Kurt D. Zeilenga.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* This work is based upon the public-domain getopt(3) routines
+ * developed by AT&T.  Modified by Kurt D. Zeilenga for inclusion
+ * into OpenLDAP Software.  Significant contributors include:
+ *   Howard Chu
+ */
 
-       modified public-domain AT&T getopt(3)
-*/
+#include "portable.h"
+
+#ifndef HAVE_GETOPT
 
 #include <stdio.h>
-#include <string.h>
 
-#ifdef _POSIX_SOURCE
-#      include <unistd.h>
-#else
-#      define STDERR_FILENO 2
-#      ifdef __STDC__
-               extern int write (int fildes, char * buf, unsigned nbyte);
-#      else
-               extern int write ();
-#      endif
+#include <ac/string.h>
+#include <ac/unistd.h>
+
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+
+#include "lutil.h"
+
+#ifndef STDERR_FILENO
+#define STDERR_FILENO 2
 #endif
 
 int opterr = 1;
@@ -23,35 +44,31 @@ int optind = 1;
 int optopt;
 char * optarg;
 
-#ifdef __STDC__
-       static void ERR (char ** argv, char * s, char c)
-#else
-       static void ERR (argv, s, c)
-       char ** argv, * s, c;
+#ifdef HAVE_EBCDIC
+extern int _trans_argv;
 #endif
-{
-       char errbuf[2];
 
+static void ERR (char * const argv[], const char * s, char c)
+{
 #ifdef DF_TRACE_DEBUG
 printf("DF_TRACE_DEBUG:        static void ERR () in getopt.c\n");
 #endif
        if (opterr)
        {
-               errbuf[0] = c;
-               errbuf[1] = '\n';
-               (void) write(STDERR_FILENO,argv[0],strlen(argv[0]));
-               (void) write(STDERR_FILENO,s,strlen(s));
-               (void) write(STDERR_FILENO,errbuf,sizeof errbuf);
+               char *ptr, outbuf[4096];
+
+               ptr = lutil_strncopy(outbuf, argv[0], sizeof(outbuf) - 2);
+               ptr = lutil_strncopy(ptr, s, sizeof(outbuf)-2 -(ptr-outbuf));
+               *ptr++ = c;
+               *ptr++ = '\n';
+#ifdef HAVE_EBCDIC
+               __atoe_l(outbuf, ptr - outbuf);
+#endif
+               (void) write(STDERR_FILENO,outbuf,ptr - outbuf);
        }
 }
 
-#ifdef __STDC__
-       int getopt (int argc, char ** argv, char * opts)
-#else
-       int getopt (argc, argv, opts)
-       int argc;
-       char ** argv, * opts;
-#endif
+int getopt (int argc, char * const argv [], const char * opts)
 {
        static int sp = 1, error = (int) '?';
        static char sw = '-', eos = '\0', arg = ':';
@@ -59,8 +76,17 @@ printf("DF_TRACE_DEBUG:      static void ERR () in getopt.c\n");
 
 #ifdef DF_TRACE_DEBUG
 printf("DF_TRACE_DEBUG:        int getopt () in getopt.c\n");
+#endif
+
+#ifdef HAVE_EBCDIC
+       if (_trans_argv) {
+               int i;
+               for (i=0; i<argc; i++) __etoa(argv[i]);
+               _trans_argv = 0;
+       }
 #endif
        if (sp == 1)
+       {
                if (optind >= argc || argv[optind][0] != sw
                || argv[optind][1] == eos)
                        return EOF;
@@ -69,11 +95,12 @@ printf("DF_TRACE_DEBUG:     int getopt () in getopt.c\n");
                        optind++;
                        return EOF;
                }
+       }
        c = argv[optind][sp];
        optopt = (int) c;
        if (c == arg || (cp = strchr(opts,c)) == NULL)
        {
-               ERR(argv,": illegal option--",c);
+               ERR(argv,_(": illegal option--"),c);
                if (argv[optind][++sp] == eos)
                {
                        optind++;
@@ -87,7 +114,7 @@ printf("DF_TRACE_DEBUG:      int getopt () in getopt.c\n");
                        optarg = &argv[optind++][sp + 1];
                else if (++optind >= argc)
                {
-                       ERR(argv,": option requires an argument--",c);
+                       ERR(argv,_(": option requires an argument--"),c);
                        sp = 1;
                        return error;
                }
@@ -106,3 +133,4 @@ printf("DF_TRACE_DEBUG:     int getopt () in getopt.c\n");
        }
        return (int) c;
 }
+#endif /* HAVE_GETOPT */