[common] fix getopt license to be compatible

This commit is contained in:
Geoffrey McRae 2018-05-22 03:19:13 +10:00
parent b3aadccfc4
commit 6e0eac0abc
2 changed files with 71 additions and 48 deletions

View file

@ -1 +1 @@
This source comes from: https://gist.github.com/superwills/5815344 This source comes from: https://github.com/Stichting-MINIX-Research-Foundation/minix

View file

@ -1,4 +1,4 @@
#include "getopt.h" /* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */
/* /*
* Copyright (c) 1987, 1993, 1994 * Copyright (c) 1987, 1993, 1994
@ -12,11 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@ -33,8 +29,9 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int opterr = 1, /* if error message should be printed */ int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */ optind = 1, /* index into parent argv vector */
@ -50,57 +47,83 @@ char *optarg; /* argument associated with option */
* getopt -- * getopt --
* Parse argc/argv argument vector. * Parse argc/argv argument vector.
*/ */
int getopt(int nargc, char * const nargv[], const char *ostr) int
getopt(int nargc, char * const nargv[], const char *ostr)
{ {
static char *place = EMSG; /* option letter processing */ static char *place = EMSG; /* option letter processing */
const char *oli; /* option letter list index */ char *oli; /* option letter list index */
if (optreset || !*place) { /* update scanning pointer */ if (optreset || *place == 0) { /* update scanning pointer */
optreset = 0; optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') { place = nargv[optind];
if (optind >= nargc || *place++ != '-') {
/* Argument is absent or is not an option */
place = EMSG; place = EMSG;
return (-1); return (-1);
} }
if (place[1] && *++place == '-') { /* found "--" */ optopt = *place++;
if (optopt == '-' && *place == 0) {
/* "--" => end of options */
++optind; ++optind;
place = EMSG; place = EMSG;
return (-1); return (-1);
} }
} /* option letter okay? */ if (optopt == 0) {
if ((optopt = (int)*place++) == (int)':' || /* Solitary '-', treat as a '-' option
!(oli = strchr(ostr, optopt))) { if the program (eg su) is looking for it. */
/* place = EMSG;
* if the user didn't specify '-' as an option, if (strchr(ostr, '-') == NULL)
* assume it means -1. return -1;
*/ optopt = '-';
if (optopt == (int)'-') }
return (-1); }
if (!*place) else
optopt = *place++;
/* See if option letter is one the caller wanted... */
if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
if (*place == 0)
++optind; ++optind;
if (opterr && *ostr != ':') if (opterr && *ostr != ':')
(void)printf("illegal option -- %c\n", optopt); (void)fprintf(stderr,
"unknown option -- %c\n",
optopt);
return (BADCH); return (BADCH);
} }
if (*++oli != ':') { /* don't need argument */
/* Does this option need an argument? */
if (oli[1] != ':') {
/* don't need argument */
optarg = NULL; optarg = NULL;
if (!*place) if (*place == 0)
++optind; ++optind;
} }
else { /* need an argument */ else {
if (*place) /* no white space */ /* Option-argument is either the rest of this argument or the
entire next argument. */
if (*place)
optarg = place; optarg = place;
else if (nargc <= ++optind) { /* no arg */ else if (oli[2] == ':')
/*
* GNU Extension, for optional arguments if the rest of
* the argument is empty, we return NULL
*/
optarg = NULL;
else if (nargc > ++optind)
optarg = nargv[optind];
else {
/* option-argument absent */
place = EMSG; place = EMSG;
if (*ostr == ':') if (*ostr == ':')
return (BADARG); return (BADARG);
if (opterr) if (opterr)
(void)printf("option requires an argument -- %c\n", optopt); (void)fprintf(stderr,
"option requires an argument -- %c\n",
optopt);
return (BADCH); return (BADCH);
} }
else /* white space */
optarg = nargv[optind];
place = EMSG; place = EMSG;
++optind; ++optind;
} }
return (optopt); /* dump back option letter */ return (optopt); /* return option letter */
} }