]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getopt.c
NT/NS MTA MD5 fix
[openldap] / libraries / liblutil / getopt.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7         getopt.c
8
9         modified public-domain AT&T getopt(3)
10         modified by Kurt Zeilenga for inclusion into OpenLDAP
11 */
12
13 #include "portable.h"
14
15 #ifndef HAVE_GETOPT
16
17 #include <stdio.h>
18
19 #include <ac/string.h>
20 #include <ac/unistd.h>
21
22 #ifdef HAVE_IO_H
23 #include <io.h>
24 #endif
25
26 #include "lutil.h"
27
28 #ifndef STDERR_FILENO
29 #define STDERR_FILENO 2
30 #endif
31
32 int opterr = 1;
33 int optind = 1;
34 int optopt;
35 char * optarg;
36
37 #ifdef HAVE_EBCDIC
38 extern int _trans_argv;
39 #endif
40
41 static void ERR (char * const argv[], const char * s, char c)
42 {
43 #ifdef DF_TRACE_DEBUG
44 printf("DF_TRACE_DEBUG:         static void ERR () in getopt.c\n");
45 #endif
46         if (opterr)
47         {
48                 char *ptr, outbuf[4096];
49
50                 ptr = lutil_strncopy(outbuf, argv[0], sizeof(outbuf) - 2);
51                 ptr = lutil_strncopy(ptr, s, sizeof(outbuf)-2 -(ptr-outbuf));
52                 *ptr++ = c;
53                 *ptr++ = '\n';
54 #ifdef HAVE_EBCDIC
55                 __atoe_l(outbuf, ptr - outbuf);
56 #endif
57                 (void) write(STDERR_FILENO,outbuf,ptr - outbuf);
58         }
59 }
60
61 int getopt (int argc, char * const argv [], const char * opts)
62 {
63         static int sp = 1, error = (int) '?';
64         static char sw = '-', eos = '\0', arg = ':';
65         register char c, * cp;
66
67 #ifdef DF_TRACE_DEBUG
68 printf("DF_TRACE_DEBUG:         int getopt () in getopt.c\n");
69 #endif
70
71 #ifdef HAVE_EBCDIC
72         if (_trans_argv) {
73                 int i;
74                 for (i=0; i<argc; i++) __etoa(argv[i]);
75                 _trans_argv = 0;
76         }
77 #endif
78         if (sp == 1)
79         {
80                 if (optind >= argc || argv[optind][0] != sw
81                 || argv[optind][1] == eos)
82                         return EOF;
83                 else if (strcmp(argv[optind],"--") == 0)
84                 {
85                         optind++;
86                         return EOF;
87                 }
88         }
89         c = argv[optind][sp];
90         optopt = (int) c;
91         if (c == arg || (cp = strchr(opts,c)) == NULL)
92         {
93                 ERR(argv,": illegal option--",c);
94                 if (argv[optind][++sp] == eos)
95                 {
96                         optind++;
97                         sp = 1;
98                 }
99                 return error;
100         }
101         else if (*++cp == arg)
102         {
103                 if (argv[optind][sp + 1] != eos)
104                         optarg = &argv[optind++][sp + 1];
105                 else if (++optind >= argc)
106                 {
107                         ERR(argv,": option requires an argument--",c);
108                         sp = 1;
109                         return error;
110                 }
111                 else
112                         optarg = argv[optind++];
113                 sp = 1;
114         }
115         else
116         {
117                 if (argv[optind][++sp] == eos)
118                 {
119                         sp = 1;
120                         optind++;
121                 }
122                 optarg = NULL;
123         }
124         return (int) c;
125 }
126 #endif /* HAVE_GETOPT */