]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getopt.c
Rework counter code in fallback.
[openldap] / libraries / liblutil / getopt.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 #ifndef STDERR_FILENO
27 #define STDERR_FILENO 2
28 #endif
29
30 int opterr = 1;
31 int optind = 1;
32 int optopt;
33 char * optarg;
34
35 static void ERR (char * const argv[], const char * s, char c)
36 {
37         char errbuf[2];
38
39 #ifdef DF_TRACE_DEBUG
40 printf("DF_TRACE_DEBUG:         static void ERR () in getopt.c\n");
41 #endif
42         if (opterr)
43         {
44                 errbuf[0] = c;
45                 errbuf[1] = '\n';
46                 (void) write(STDERR_FILENO,argv[0],strlen(argv[0]));
47                 (void) write(STDERR_FILENO,s,strlen(s));
48                 (void) write(STDERR_FILENO,errbuf,sizeof errbuf);
49         }
50 }
51
52 int getopt (int argc, char * const argv [], const char * opts)
53 {
54         static int sp = 1, error = (int) '?';
55         static char sw = '-', eos = '\0', arg = ':';
56         register char c, * cp;
57
58 #ifdef DF_TRACE_DEBUG
59 printf("DF_TRACE_DEBUG:         int getopt () in getopt.c\n");
60 #endif
61         if (sp == 1)
62                 if (optind >= argc || argv[optind][0] != sw
63                 || argv[optind][1] == eos)
64                         return EOF;
65                 else if (strcmp(argv[optind],"--") == 0)
66                 {
67                         optind++;
68                         return EOF;
69                 }
70         c = argv[optind][sp];
71         optopt = (int) c;
72         if (c == arg || (cp = strchr(opts,c)) == NULL)
73         {
74                 ERR(argv,": illegal option--",c);
75                 if (argv[optind][++sp] == eos)
76                 {
77                         optind++;
78                         sp = 1;
79                 }
80                 return error;
81         }
82         else if (*++cp == arg)
83         {
84                 if (argv[optind][sp + 1] != eos)
85                         optarg = &argv[optind++][sp + 1];
86                 else if (++optind >= argc)
87                 {
88                         ERR(argv,": option requires an argument--",c);
89                         sp = 1;
90                         return error;
91                 }
92                 else
93                         optarg = argv[optind++];
94                 sp = 1;
95         }
96         else
97         {
98                 if (argv[optind][++sp] == eos)
99                 {
100                         sp = 1;
101                         optind++;
102                 }
103                 optarg = NULL;
104         }
105         return (int) c;
106 }
107 #endif /* HAVE_GETOPT */