diff --git a/doc/funcref.sgml b/doc/funcref.sgml
index 409eefc87..fa764129f 100644
--- a/doc/funcref.sgml
+++ b/doc/funcref.sgml
@@ -568,6 +568,7 @@ It does not declare any functions.
+
@@ -704,7 +705,7 @@ id="malloc" name="malloc"> may still return _poserror
+
+
+
+
getenv
@@ -2211,23 +2234,32 @@ be used in presence of a prototype.
-getcpu
+getopt
-/
-
-
+/
+
-Other, more exotic CPU types are not disinguished.
+The implementation will not reorder options. A non option on the command
+line will terminate option processing. All remaining arguments are not
+recognized as options, even if the start with a '-' character.
+The function is only available as fastcall function, so it may only
+be used in presence of a prototype.
-
diff --git a/libsrc/common/Makefile b/libsrc/common/Makefile
index b779cf97f..37888b9e8 100644
--- a/libsrc/common/Makefile
+++ b/libsrc/common/Makefile
@@ -59,6 +59,7 @@ C_OBJS = _afailed.o \
fsetpos.o \
ftell.o \
getchar.o \
+ getopt.o \
gets.o \
gmtime.o \
locale.o \
diff --git a/libsrc/common/getopt.c b/libsrc/common/getopt.c
new file mode 100644
index 000000000..9faca33a2
--- /dev/null
+++ b/libsrc/common/getopt.c
@@ -0,0 +1,84 @@
+/*
+ * This is part of a changed public domain getopt implementation that
+ * had the following text on top:
+ *
+ * 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:
+ *
+ * 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
+ */
+
+#include
+#include
+#include
+#include
+
+#define ARGCH ':'
+#define BADCH '?'
+#define EMSG ""
+
+int opterr = 1; /* useless, never set or used */
+int optind = 1; /* index into parent argv vector */
+int optopt; /* character checked for validity */
+
+char *optarg; /* argument associated with option */
+
+#define tell(s) fputs(*argv,stderr);fputs(s,stderr); \
+ fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
+
+int __fastcall__ getopt (int argc, char* const* argv, const char* optstring)
+/* Get option letter from argument vector */
+{
+ static char *place = EMSG; /* option letter processing */
+
+ register char *oli; /* option letter list index */
+
+ if (!*place) { /* update scanning pointer */
+ if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
+ return (EOF);
+ }
+ if (*place == '-') {
+ /* found "--" */
+ ++optind;
+ return (EOF);
+ }
+ }
+
+ /* option letter okay? */
+ if ((optopt = (int) *place++) == ARGCH ||
+ !(oli = strchr (optstring, 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) {
+ /* no white space */
+ optarg = place;
+ }
+ else if (argc <= ++optind) { /* no arg */
+ place = EMSG;
+ tell (": option requires an argument -- ");
+ } else {
+ /* white space */
+ optarg = argv[optind];
+ }
+ place = EMSG;
+ ++optind;
+ }
+ return (optopt); /* dump back option letter */
+}
+
diff --git a/testcode/lib/getopt-test.c b/testcode/lib/getopt-test.c
new file mode 100644
index 000000000..28dbd025d
--- /dev/null
+++ b/testcode/lib/getopt-test.c
@@ -0,0 +1,61 @@
+/*
+ * This is part of a changed public domain getopt implementation that
+ * had the following text on top:
+ *
+ * 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:
+ *
+ * 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
+ */
+
+#include
+#include
+#include
+#include
+
+#define ARGCH ':'
+#define BADCH '?'
+#define ENDARGS "--"
+
+int main (int argc, char **argv)
+{
+ char *optstring = argv[1];
+
+ char *argv0 = argv[0];
+
+ int opterr = 0;
+
+ int C;
+
+ char *opi;
+
+ if (argc == 1) {
+ fprintf (stderr, "Usage: %s optstring args\n", argv0);
+ exit (1);
+ }
+ argv++;
+ argc--;
+ argv[0] = argv0;
+ while ((C = getopt (argc, argv, optstring)) != EOF) {
+ if (C == BADCH)
+ opterr++;
+ printf ("-%c ", C);
+ opi = strchr (optstring, C);
+ if (opi && opi[1] == ARGCH)
+ if (optarg)
+ printf ("\"%s\" ", optarg);
+ else
+ opterr++;
+ }
+ printf ("%s", ENDARGS);
+ while (optind < argc)
+ printf (" \"%s\"", argv[optind++]);
+ putchar ('\n');
+ return opterr;
+}
+