]> git.sur5r.net Git - openldap/blob - libraries/macintosh/getopt.c
Import some lint removal
[openldap] / libraries / macintosh / 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(int nargc, char **nargv, char *ostr)
42 {
43         static char *place = EMSG;              /* option letter processing */
44         register char *oli;                     /* option letter list index */
45         char *p;
46
47         if (!*place) {                          /* update scanning pointer */
48                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
49                         place = EMSG;
50                         return(EOF);
51                 }
52                 if (place[1] && *++place == '-') {      /* found "--" */
53                         ++optind;
54                         place = EMSG;
55                         return(EOF);
56                 }
57         }                                       /* option letter okay? */
58         if ((optopt = (int)*place++) == (int)':' ||
59             !(oli = index(ostr, optopt))) {
60                 /*
61                  * if the user didn't specify '-' as an option,
62                  * assume it means EOF.
63                  */
64                 if (optopt == (int)'-')
65                         return(EOF);
66                 if (!*place)
67                         ++optind;
68                 if (opterr) {
69                         if (!(p = rindex(*nargv, '/')))
70                                 p = *nargv;
71                         else
72                                 ++p;
73                         (void)fprintf(stderr, "%s: illegal option -- %c\n",
74                             p, optopt);
75                 }
76                 return(BADCH);
77         }
78         if (*++oli != ':') {                    /* don't need argument */
79                 optarg = NULL;
80                 if (!*place)
81                         ++optind;
82         }
83         else {                                  /* need an argument */
84                 if (*place)                     /* no white space */
85                         optarg = place;
86                 else if (nargc <= ++optind) {   /* no arg */
87                         place = EMSG;
88                         if (!(p = rindex(*nargv, '/')))
89                                 p = *nargv;
90                         else
91                                 ++p;
92                         if (opterr)
93                                 (void)fprintf(stderr,
94                                     "%s: option requires an argument -- %c\n",
95                                     p, optopt);
96                         return(BADCH);
97                 }
98                 else                            /* white space */
99                         optarg = nargv[optind];
100                 place = EMSG;
101                 ++optind;
102         }
103         return(optopt);                         /* dump back option letter */
104 }