Back to index

openldap  2.4.31
getopt.c
Go to the documentation of this file.
00001 /* getopt.c -- replacement getopt(3) routines */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* This work is based upon the public-domain getopt(3) routines
00018  * developed by AT&T.  Modified by Kurt D. Zeilenga for inclusion
00019  * into OpenLDAP Software.  Significant contributors include:
00020  *   Howard Chu
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #ifndef HAVE_GETOPT
00026 
00027 #include <stdio.h>
00028 
00029 #include <ac/string.h>
00030 #include <ac/unistd.h>
00031 
00032 #ifdef HAVE_IO_H
00033 #include <io.h>
00034 #endif
00035 
00036 #include "lutil.h"
00037 
00038 #ifndef STDERR_FILENO
00039 #define STDERR_FILENO 2
00040 #endif
00041 
00042 int opterr = 1;
00043 int optind = 1;
00044 int optopt;
00045 char * optarg;
00046 
00047 #ifdef HAVE_EBCDIC
00048 extern int _trans_argv;
00049 #endif
00050 
00051 static void ERR (char * const argv[], const char * s, char c)
00052 {
00053 #ifdef DF_TRACE_DEBUG
00054 printf("DF_TRACE_DEBUG:     static void ERR () in getopt.c\n");
00055 #endif
00056        if (opterr)
00057        {
00058               char *ptr, outbuf[4096];
00059 
00060               ptr = lutil_strncopy(outbuf, argv[0], sizeof(outbuf) - 2);
00061               ptr = lutil_strncopy(ptr, s, sizeof(outbuf)-2 -(ptr-outbuf));
00062               *ptr++ = c;
00063               *ptr++ = '\n';
00064 #ifdef HAVE_EBCDIC
00065               __atoe_l(outbuf, ptr - outbuf);
00066 #endif
00067               (void) write(STDERR_FILENO,outbuf,ptr - outbuf);
00068        }
00069 }
00070 
00071 int getopt (int argc, char * const argv [], const char * opts)
00072 {
00073        static int sp = 1, error = (int) '?';
00074        static char sw = '-', eos = '\0', arg = ':';
00075        register char c, * cp;
00076 
00077 #ifdef DF_TRACE_DEBUG
00078 printf("DF_TRACE_DEBUG:     int getopt () in getopt.c\n");
00079 #endif
00080 
00081 #ifdef HAVE_EBCDIC
00082        if (_trans_argv) {
00083               int i;
00084               for (i=0; i<argc; i++) __etoa(argv[i]);
00085               _trans_argv = 0;
00086        }
00087 #endif
00088        if (sp == 1)
00089        {
00090               if (optind >= argc || argv[optind][0] != sw
00091               || argv[optind][1] == eos)
00092                      return EOF;
00093               else if (strcmp(argv[optind],"--") == 0)
00094               {
00095                      optind++;
00096                      return EOF;
00097               }
00098        }
00099        c = argv[optind][sp];
00100        optopt = (int) c;
00101        if (c == arg || (cp = strchr(opts,c)) == NULL)
00102        {
00103               ERR(argv,_(": illegal option--"),c);
00104               if (argv[optind][++sp] == eos)
00105               {
00106                      optind++;
00107                      sp = 1;
00108               }
00109               return error;
00110        }
00111        else if (*++cp == arg)
00112        {
00113               if (argv[optind][sp + 1] != eos)
00114                      optarg = &argv[optind++][sp + 1];
00115               else if (++optind >= argc)
00116               {
00117                      ERR(argv,_(": option requires an argument--"),c);
00118                      sp = 1;
00119                      return error;
00120               }
00121               else
00122                      optarg = argv[optind++];
00123               sp = 1;
00124        }
00125        else
00126        {
00127               if (argv[optind][++sp] == eos)
00128               {
00129                      sp = 1;
00130                      optind++;
00131               }
00132               optarg = NULL;
00133        }
00134        return (int) c;
00135 }
00136 #endif /* HAVE_GETOPT */