]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getopt.c
Update copyright statements
[openldap] / libraries / liblutil / getopt.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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         {
63                 if (optind >= argc || argv[optind][0] != sw
64                 || argv[optind][1] == eos)
65                         return EOF;
66                 else if (strcmp(argv[optind],"--") == 0)
67                 {
68                         optind++;
69                         return EOF;
70                 }
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 }
109 #endif /* HAVE_GETOPT */