tools/trace-malloc/getopt.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial