1 /* getopt.c -- replacement getopt(3) routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2018 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* This work is based upon the public-domain getopt(3) routines
18 * developed by AT&T. Modified by Kurt D. Zeilenga for inclusion
19 * into OpenLDAP Software. Significant contributors include:
29 #include <ac/string.h>
30 #include <ac/unistd.h>
39 #define STDERR_FILENO 2
48 extern int _trans_argv;
51 static void ERR (char * const argv[], const char * s, char c)
54 printf("DF_TRACE_DEBUG: static void ERR () in getopt.c\n");
58 char *ptr, outbuf[4096];
60 ptr = lutil_strncopy(outbuf, argv[0], sizeof(outbuf) - 2);
61 ptr = lutil_strncopy(ptr, s, sizeof(outbuf)-2 -(ptr-outbuf));
65 __atoe_l(outbuf, ptr - outbuf);
67 (void) write(STDERR_FILENO,outbuf,ptr - outbuf);
71 int getopt (int argc, char * const argv [], const char * opts)
73 static int sp = 1, error = (int) '?';
74 static char sw = '-', eos = '\0', arg = ':';
75 register char c, * cp;
78 printf("DF_TRACE_DEBUG: int getopt () in getopt.c\n");
84 for (i=0; i<argc; i++) __etoa(argv[i]);
90 if (optind >= argc || argv[optind][0] != sw
91 || argv[optind][1] == eos)
93 else if (strcmp(argv[optind],"--") == 0)
101 if (c == arg || (cp = strchr(opts,c)) == NULL)
103 ERR(argv,_(": illegal option--"),c);
104 if (argv[optind][++sp] == eos)
111 else if (*++cp == arg)
113 if (argv[optind][sp + 1] != eos)
114 optarg = &argv[optind++][sp + 1];
115 else if (++optind >= argc)
117 ERR(argv,_(": option requires an argument--"),c);
122 optarg = argv[optind++];
127 if (argv[optind][++sp] == eos)
136 #endif /* HAVE_GETOPT */