aboutsummaryrefslogtreecommitdiff
path: root/getopt.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-12-14 03:55:02 -0500
committerB. Watson <urchlay@slackware.uk>2024-12-14 03:55:02 -0500
commite11de8814424c2abb61d91a050e1096d17765787 (patch)
tree5977a364b71896d0a26a7ec6cfe06c50526d6c7e /getopt.c
parent8ff2836f9625d4ca4b14257034f4816e4303d7b8 (diff)
downloaduxd-e11de8814424c2abb61d91a050e1096d17765787.tar.gz
Add getopt.c (ancient but public domain).
Diffstat (limited to 'getopt.c')
-rw-r--r--getopt.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/getopt.c b/getopt.c
new file mode 100644
index 0000000..c6bf394
--- /dev/null
+++ b/getopt.c
@@ -0,0 +1,96 @@
+/* 20070425 bkw:
+ Public Domain implementation of getopt(),
+ downloaded from http://www.zdomain.com/a56/src/getopt.c
+ Following anonymous comment was in the original version:
+*/
+
+/*
+ I got this off net.sources from Henry Spencer.
+ It is a public domain getopt(3) like in System V.
+ I have made the following modifications:
+
+ index(s,c) was added because too many people could
+ not compile getopt without it.
+
+ A test main program was added, ifdeffed by GETOPT.
+ This main program is a public domain implementation
+ of the getopt(1) program like in System V. The getopt
+ program can be used to standardize shell option handling.
+ e.g. cc -DGETOPT getopt.c -o getopt
+*/
+
+/* 20070425 bkw:
+ Altered slightly for cassio (use fatal() for error messages,
+ get rid of main(), add return type to getopt()...)
+*/
+
+/* 20241214 bkw: modified slightly for use with uxd. */
+
+extern char *self;
+
+#include <stdio.h>
+
+#define ARGCH (int)':'
+#define BADCH (int)'?'
+#define EMSG ""
+#define ENDARGS "--"
+
+/* this is included because index is not on some UNIX systems */
+static
+char *
+index (s, c)
+register char *s;
+register int c;
+ {
+ while (*s)
+ if (c == *s) return (s);
+ else s++;
+ return (NULL);
+ }
+
+/*
+ * get option letter from argument vector
+ */
+int opterr = 1, /* useless, never set or used */
+ optind = 1, /* index into parent argv vector */
+ optopt; /* character checked for validity */
+char *optarg; /* argument associated with option */
+
+#define tell(s) fprintf(stderr, "%s: %s", self, s); \
+ fputc(optopt,stderr);fputs(" (use -h for help)\n",stderr);return(BADCH);
+
+
+int getopt(int nargc, char **nargv, char *ostr)
+{
+ static char *place = EMSG; /* option letter processing */
+ register char *oli; /* option letter list index */
+ char *index();
+
+ if(!*place) { /* update scanning pointer */
+ if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
+ if (*place == '-') { /* found "--" */
+ ++optind;
+ return(EOF);
+ }
+ } /* option letter okay? */
+ if ((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
+ if(!*place) ++optind;
+ tell("illegal option: -");
+ }
+ if (*++oli != ARGCH) { /* don't need argument */
+ optarg = NULL;
+ if (!*place) ++optind;
+ }
+ else { /* need an argument */
+ if (*place) optarg = place; /* no white space */
+ else if (nargc <= ++optind) { /* no arg */
+ place = EMSG;
+ tell("option requires an argument: -");
+ }
+ else optarg = nargv[optind]; /* white space */
+ place = EMSG;
+ ++optind;
+ }
+ return(optopt); /* dump back option letter */
+}
+