Subject: Mismatched lws_zalloc / free
[platform/upstream/libwebsockets.git] / win32port / win32helpers / getopt.c
1 /*      $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $       */\r
2 \r
3 /*\r
4  * Copyright (c) 1987, 1993, 1994\r
5  *      The Regents of the University of California.  All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or without\r
8  * modification, are permitted provided that the following conditions\r
9  * are met:\r
10  * 1. Redistributions of source code must retain the above copyright\r
11  *    notice, this list of conditions and the following disclaimer.\r
12  * 2. Redistributions in binary form must reproduce the above copyright\r
13  *    notice, this list of conditions and the following disclaimer in the\r
14  *    documentation and/or other materials provided with the distribution.\r
15  * 3. All advertising materials mentioning features or use of this software\r
16  *    must display the following acknowledgement:\r
17  *      This product includes software developed by the University of\r
18  *      California, Berkeley and its contributors.\r
19  * 4. Neither the name of the University nor the names of its contributors\r
20  *    may be used to endorse or promote products derived from this software\r
21  *    without specific prior written permission.\r
22  *\r
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
33  * SUCH DAMAGE.\r
34  */\r
35 \r
36 #if 0\r
37 static char sccsid[] = "@(#)getopt.c    8.3 (Berkeley) 4/27/95";\r
38 #endif\r
39 \r
40 #include <assert.h>\r
41 #include <errno.h>\r
42 #include <stdio.h>\r
43 #include <string.h>\r
44 \r
45 #define __P(x) x\r
46 #define _DIAGASSERT(x) assert(x)\r
47 \r
48 #ifdef __weak_alias\r
49 __weak_alias(getopt,_getopt);\r
50 #endif\r
51 \r
52 \r
53 int     opterr = 1,             /* if error message should be printed */\r
54         optind = 1,             /* index into parent argv vector */\r
55         optopt,                 /* character checked for validity */\r
56         optreset;               /* reset getopt */\r
57 char    *optarg;                /* argument associated with option */\r
58 \r
59 static char * _progname __P((char *));\r
60 int getopt_internal __P((int, char * const *, const char *));\r
61 \r
62 static char *\r
63 _progname(nargv0)\r
64         char * nargv0;\r
65 {\r
66         char * tmp;\r
67 \r
68         _DIAGASSERT(nargv0 != NULL);\r
69 \r
70         tmp = strrchr(nargv0, '/');\r
71         if (tmp)\r
72                 tmp++;\r
73         else\r
74                 tmp = nargv0;\r
75         return(tmp);\r
76 }\r
77 \r
78 #define BADCH   (int)'?'\r
79 #define BADARG  (int)':'\r
80 #define EMSG    ""\r
81 \r
82 /*\r
83  * getopt --\r
84  *      Parse argc/argv argument vector.\r
85  */\r
86 int\r
87 getopt(nargc, nargv, ostr)\r
88         int nargc;\r
89         char * const nargv[];\r
90         const char *ostr;\r
91 {\r
92         static char *__progname = 0;\r
93         static char *place = EMSG;              /* option letter processing */\r
94         char *oli;                              /* option letter list index */\r
95         __progname = __progname?__progname:_progname(*nargv);\r
96 \r
97         _DIAGASSERT(nargv != NULL);\r
98         _DIAGASSERT(ostr != NULL);\r
99 \r
100         if (optreset || !*place) {              /* update scanning pointer */\r
101                 optreset = 0;\r
102                 if (optind >= nargc || *(place = nargv[optind]) != '-') {\r
103                         place = EMSG;\r
104                         return (-1);\r
105                 }\r
106                 if (place[1] && *++place == '-' /* found "--" */\r
107                     && place[1] == '\0') {\r
108                         ++optind;\r
109                         place = EMSG;\r
110                         return (-1);\r
111                 }\r
112         }                                       /* option letter okay? */\r
113         if ((optopt = (int)*place++) == (int)':' ||\r
114             !(oli = strchr(ostr, optopt))) {\r
115                 /*\r
116                  * if the user didn't specify '-' as an option,\r
117                  * assume it means -1.\r
118                  */\r
119                 if (optopt == (int)'-')\r
120                         return (-1);\r
121                 if (!*place)\r
122                         ++optind;\r
123                 if (opterr && *ostr != ':')\r
124                         (void)fprintf(stderr,\r
125                             "%s: illegal option -- %c\n", __progname, optopt);\r
126                 return (BADCH);\r
127         }\r
128         if (*++oli != ':') {                    /* don't need argument */\r
129                 optarg = NULL;\r
130                 if (!*place)\r
131                         ++optind;\r
132         }\r
133         else {                                  /* need an argument */\r
134                 if (*place)                     /* no white space */\r
135                         optarg = place;\r
136                 else if (nargc <= ++optind) {   /* no arg */\r
137                         place = EMSG;\r
138                         if (*ostr == ':')\r
139                                 return (BADARG);\r
140                         if (opterr)\r
141                                 (void)fprintf(stderr,\r
142                                     "%s: option requires an argument -- %c\n",\r
143                                     __progname, optopt);\r
144                         return (BADCH);\r
145                 }\r
146                 else                            /* white space */\r
147                         optarg = nargv[optind];\r
148                 place = EMSG;\r
149                 ++optind;\r
150         }\r
151         return (optopt);                        /* dump back option letter */\r
152 }\r
153 \r