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