tools/trace-malloc/getopt.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/trace-malloc/getopt.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +/*
     1.9 + * Copyright (c) 1987 Regents of the University of California.
    1.10 + * All rights reserved.
    1.11 + *
    1.12 + * Redistribution and use in source and binary forms are permitted
    1.13 + * provided that: (1) source distributions retain this entire copyright
    1.14 + * notice and comment, and (2) distributions including binaries display
    1.15 + * the following acknowledgement:  ``This product includes software
    1.16 + * developed by the University of California, Berkeley and its contributors''
    1.17 + * in the documentation or other materials provided with the distribution
    1.18 + * and in all advertising materials mentioning features or use of this
    1.19 + * software. Neither the name of the University nor the names of its
    1.20 + * contributors may be used to endorse or promote products derived
    1.21 + * from this software without specific prior written permission.
    1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.25 + */
    1.26 +
    1.27 +#if defined(LIBC_SCCS) && !defined(lint)
    1.28 +static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
    1.29 +#endif /* LIBC_SCCS and not lint */
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include <string.h>
    1.33 +#define index strchr
    1.34 +#define rindex strrchr
    1.35 +
    1.36 +/*
    1.37 + * get option letter from argument vector
    1.38 + */
    1.39 +int     opterr = 1,             /* if error message should be printed */
    1.40 +	optind = 1,             /* index into parent argv vector */
    1.41 +	optopt;                 /* character checked for validity */
    1.42 +char    *optarg;                /* argument associated with option */
    1.43 +
    1.44 +#define BADCH   (int)'?'
    1.45 +#define EMSG    ""
    1.46 +
    1.47 +int getopt(int nargc, char **nargv, char *ostr)
    1.48 +{
    1.49 +	static char *place = EMSG;              /* option letter processing */
    1.50 +	register char *oli;                     /* option letter list index */
    1.51 +	char *p;
    1.52 +
    1.53 +	if (!*place) {                          /* update scanning pointer */
    1.54 +		if (optind >= nargc || *(place = nargv[optind]) != '-') {
    1.55 +			place = EMSG;
    1.56 +			return(EOF);
    1.57 +		}
    1.58 +		if (place[1] && *++place == '-') {      /* found "--" */
    1.59 +			++optind;
    1.60 +			place = EMSG;
    1.61 +			return(EOF);
    1.62 +		}
    1.63 +	}                                       /* option letter okay? */
    1.64 +	if ((optopt = (int)*place++) == (int)':' ||
    1.65 +	    !(oli = index(ostr, optopt))) {
    1.66 +		/*
    1.67 +		 * if the user didn't specify '-' as an option,
    1.68 +		 * assume it means EOF.
    1.69 +		 */
    1.70 +		if (optopt == (int)'-')
    1.71 +			return(EOF);
    1.72 +		if (!*place)
    1.73 +			++optind;
    1.74 +		if (opterr) {
    1.75 +			if (!(p = rindex(*nargv, '/')))
    1.76 +				p = *nargv;
    1.77 +			else
    1.78 +				++p;
    1.79 +			(void)fprintf(stderr, "%s: illegal option -- %c\n",
    1.80 +			    p, optopt);
    1.81 +		}
    1.82 +		return(BADCH);
    1.83 +	}
    1.84 +	if (*++oli != ':') {                    /* don't need argument */
    1.85 +		optarg = NULL;
    1.86 +		if (!*place)
    1.87 +			++optind;
    1.88 +	}
    1.89 +	else {                                  /* need an argument */
    1.90 +		if (*place)                     /* no white space */
    1.91 +			optarg = place;
    1.92 +		else if (nargc <= ++optind) {   /* no arg */
    1.93 +			place = EMSG;
    1.94 +			if (!(p = rindex(*nargv, '/')))
    1.95 +				p = *nargv;
    1.96 +			else
    1.97 +				++p;
    1.98 +			if (opterr)
    1.99 +				(void)fprintf(stderr,
   1.100 +				    "%s: option requires an argument -- %c\n",
   1.101 +				    p, optopt);
   1.102 +			return(BADCH);
   1.103 +		}
   1.104 +		else                            /* white space */
   1.105 +			optarg = nargv[optind];
   1.106 +		place = EMSG;
   1.107 +		++optind;
   1.108 +	}
   1.109 +	return(optopt);                         /* dump back option letter */
   1.110 +}

mercurial