]> git.sur5r.net Git - openldap/blob - libraries/vms/getopt.c
Fixed SASL interactive free bug (ITS#2423)
[openldap] / libraries / vms / getopt.c
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that: (1) source distributions retain this entire copyright
7  * notice and comment, and (2) distributions including binaries display
8  * the following acknowledgement:  ``This product includes software
9  * developed by the University of California, Berkeley and its contributors''
10  * in the documentation or other materials provided with the distribution
11  * and in all advertising materials mentioning features or use of this
12  * software. Neither the name of the University nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "lber.h"
27 #define index strchr
28 #define rindex strrchr
29
30 /*
31  * get option letter from argument vector
32  */
33 int     opterr = 1,             /* if error message should be printed */
34         optind = 1,             /* index into parent argv vector */
35         optopt;                 /* character checked for validity */
36 char    *optarg;                /* argument associated with option */
37
38 #define BADCH   (int)'?'
39 #define EMSG    ""
40
41 getopt(nargc, nargv, ostr)
42         int nargc;
43         char **nargv, *ostr;
44 {
45         static char *place = EMSG;              /* option letter processing */
46         register char *oli;                     /* option letter list index */
47         char *p;
48
49         if (!*place) {                          /* update scanning pointer */
50                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
51                         place = EMSG;
52                         return(EOF);
53                 }
54                 if (place[1] && *++place == '-') {      /* found "--" */
55                         ++optind;
56                         place = EMSG;
57                         return(EOF);
58                 }
59         }                                       /* option letter okay? */
60         if ((optopt = (int)*place++) == (int)':' ||
61             !(oli = index(ostr, optopt))) {
62                 /*
63                  * if the user didn't specify '-' as an option,
64                  * assume it means EOF.
65                  */
66                 if (optopt == (int)'-')
67                         return(EOF);
68                 if (!*place)
69                         ++optind;
70                 if (opterr) {
71                         if (!(p = rindex(*nargv, '/')))
72                                 p = *nargv;
73                         else
74                                 ++p;
75                         (void)fprintf(stderr, "%s: illegal option -- %c\n",
76                             p, optopt);
77                 }
78                 return(BADCH);
79         }
80         if (*++oli != ':') {                    /* don't need argument */
81                 optarg = NULL;
82                 if (!*place)
83                         ++optind;
84         }
85         else {                                  /* need an argument */
86                 if (*place)                     /* no white space */
87                         optarg = place;
88                 else if (nargc <= ++optind) {   /* no arg */
89                         place = EMSG;
90                         if (!(p = rindex(*nargv, '/')))
91                                 p = *nargv;
92                         else
93                                 ++p;
94                         if (opterr)
95                                 (void)fprintf(stderr,
96                                     "%s: option requires an argument -- %c\n",
97                                     p, optopt);
98                         return(BADCH);
99                 }
100                 else                            /* white space */
101                         optarg = nargv[optind];
102                 place = EMSG;
103                 ++optind;
104         }
105         return(optopt);                         /* dump back option letter */
106 }