CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / tools / trace-malloc / getopt.c
blob08d9d1d9b085b292814862baec3a1e41df130f4c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * Copyright (c) 1987 Regents of the University of California.
39 * All rights reserved.
41 * Redistribution and use in source and binary forms are permitted
42 * provided that: (1) source distributions retain this entire copyright
43 * notice and comment, and (2) distributions including binaries display
44 * the following acknowledgement: ``This product includes software
45 * developed by the University of California, Berkeley and its contributors''
46 * in the documentation or other materials provided with the distribution
47 * and in all advertising materials mentioning features or use of this
48 * software. Neither the name of the University nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90";
58 #endif /* LIBC_SCCS and not lint */
60 #include <stdio.h>
61 #include <string.h>
62 #define index strchr
63 #define rindex strrchr
66 * get option letter from argument vector
68 int opterr = 1, /* if error message should be printed */
69 optind = 1, /* index into parent argv vector */
70 optopt; /* character checked for validity */
71 char *optarg; /* argument associated with option */
73 #define BADCH (int)'?'
74 #define EMSG ""
76 getopt(int nargc, char **nargv, char *ostr)
78 static char *place = EMSG; /* option letter processing */
79 register char *oli; /* option letter list index */
80 char *p;
82 if (!*place) { /* update scanning pointer */
83 if (optind >= nargc || *(place = nargv[optind]) != '-') {
84 place = EMSG;
85 return(EOF);
87 if (place[1] && *++place == '-') { /* found "--" */
88 ++optind;
89 place = EMSG;
90 return(EOF);
92 } /* option letter okay? */
93 if ((optopt = (int)*place++) == (int)':' ||
94 !(oli = index(ostr, optopt))) {
96 * if the user didn't specify '-' as an option,
97 * assume it means EOF.
99 if (optopt == (int)'-')
100 return(EOF);
101 if (!*place)
102 ++optind;
103 if (opterr) {
104 if (!(p = rindex(*nargv, '/')))
105 p = *nargv;
106 else
107 ++p;
108 (void)fprintf(stderr, "%s: illegal option -- %c\n",
109 p, optopt);
111 return(BADCH);
113 if (*++oli != ':') { /* don't need argument */
114 optarg = NULL;
115 if (!*place)
116 ++optind;
118 else { /* need an argument */
119 if (*place) /* no white space */
120 optarg = place;
121 else if (nargc <= ++optind) { /* no arg */
122 place = EMSG;
123 if (!(p = rindex(*nargv, '/')))
124 p = *nargv;
125 else
126 ++p;
127 if (opterr)
128 (void)fprintf(stderr,
129 "%s: option requires an argument -- %c\n",
130 p, optopt);
131 return(BADCH);
133 else /* white space */
134 optarg = nargv[optind];
135 place = EMSG;
136 ++optind;
138 return(optopt); /* dump back option letter */