]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getopt.c
Update slapd to use lutil_passwd() for both user and root passwords.
[openldap] / libraries / liblutil / getopt.c
1 /*
2         getopt.c
3
4         modified public-domain AT&T getopt(3)
5         modified by Kurt Zeilenga for inclusion into OpenLDAP
6 */
7
8 #include "portable.h"
9
10 #ifndef HAVE_GETOPT
11
12 #include <stdio.h>
13
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #ifdef HAVE_IO_H
18 #include <io.h>
19 #endif
20
21 #ifndef STDERR_FILENO
22 #define STDERR_FILENO 2
23 #endif
24
25 int opterr = 1;
26 int optind = 1;
27 int optopt;
28 char * optarg;
29
30 static void ERR (char * const argv[], const char * s, char c)
31 {
32         char errbuf[2];
33
34 #ifdef DF_TRACE_DEBUG
35 printf("DF_TRACE_DEBUG:         static void ERR () in getopt.c\n");
36 #endif
37         if (opterr)
38         {
39                 errbuf[0] = c;
40                 errbuf[1] = '\n';
41                 (void) write(STDERR_FILENO,argv[0],strlen(argv[0]));
42                 (void) write(STDERR_FILENO,s,strlen(s));
43                 (void) write(STDERR_FILENO,errbuf,sizeof errbuf);
44         }
45 }
46
47 int getopt (int argc, char * const argv [], const char * opts)
48 {
49         static int sp = 1, error = (int) '?';
50         static char sw = '-', eos = '\0', arg = ':';
51         register char c, * cp;
52
53 #ifdef DF_TRACE_DEBUG
54 printf("DF_TRACE_DEBUG:         int getopt () in getopt.c\n");
55 #endif
56         if (sp == 1)
57                 if (optind >= argc || argv[optind][0] != sw
58                 || argv[optind][1] == eos)
59                         return EOF;
60                 else if (strcmp(argv[optind],"--") == 0)
61                 {
62                         optind++;
63                         return EOF;
64                 }
65         c = argv[optind][sp];
66         optopt = (int) c;
67         if (c == arg || (cp = strchr(opts,c)) == NULL)
68         {
69                 ERR(argv,": illegal option--",c);
70                 if (argv[optind][++sp] == eos)
71                 {
72                         optind++;
73                         sp = 1;
74                 }
75                 return error;
76         }
77         else if (*++cp == arg)
78         {
79                 if (argv[optind][sp + 1] != eos)
80                         optarg = &argv[optind++][sp + 1];
81                 else if (++optind >= argc)
82                 {
83                         ERR(argv,": option requires an argument--",c);
84                         sp = 1;
85                         return error;
86                 }
87                 else
88                         optarg = argv[optind++];
89                 sp = 1;
90         }
91         else
92         {
93                 if (argv[optind][++sp] == eos)
94                 {
95                         sp = 1;
96                         optind++;
97                 }
98                 optarg = NULL;
99         }
100         return (int) c;
101 }
102 #endif /* HAVE_GETOPT */