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