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