aboutsummaryrefslogtreecommitdiffstats
path: root/host/examples/getopt
diff options
context:
space:
mode:
Diffstat (limited to 'host/examples/getopt')
-rw-r--r--host/examples/getopt/CMakeLists.txt24
-rw-r--r--host/examples/getopt/getopt.c70
-rw-r--r--host/examples/getopt/getopt.h19
3 files changed, 113 insertions, 0 deletions
diff --git a/host/examples/getopt/CMakeLists.txt b/host/examples/getopt/CMakeLists.txt
new file mode 100644
index 000000000..46ad1460c
--- /dev/null
+++ b/host/examples/getopt/CMakeLists.txt
@@ -0,0 +1,24 @@
+#
+# Copyright 2015 Ettus Research LLC
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+########################################################################
+# getopt library for C examples since MSVC does not include it
+########################################################################
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIRECTORY})
+ADD_LIBRARY(getopt STATIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/getopt.c
+)
diff --git a/host/examples/getopt/getopt.c b/host/examples/getopt/getopt.c
new file mode 100644
index 000000000..e571ff9c8
--- /dev/null
+++ b/host/examples/getopt/getopt.c
@@ -0,0 +1,70 @@
+/*
+ * This file was copied from the following newsgroup posting:
+ *
+ * Newsgroups: mod.std.unix
+ * Subject: public domain AT&T getopt source
+ * Date: 3 Nov 85 19:34:15 GMT
+ *
+ * Here's something you've all been waiting for: the AT&T public domain
+ * source for getopt(3). It is the code which was given out at the 1985
+ * UNIFORUM conference in Dallas. I obtained it by electronic mail
+ * directly from AT&T. The people there assure me that it is indeed
+ * in the public domain.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
+
+int opterr = 1;
+int optind = 1;
+int optopt;
+char *optarg;
+
+int
+getopt(int argc, char **argv, char *opts)
+{
+ static int sp;
+ int c;
+ char *cp;
+
+ sp = 1;
+
+ if (sp == 1) {
+ if (optind >= argc ||
+ argv[optind][0] != '-' || argv[optind][1] == '\0')
+ return (EOF);
+ else if (strcmp(argv[optind], "--") == 0) {
+ optind++;
+ return (EOF);
+ }
+ }
+ optopt = c = argv[optind][sp];
+ if (c == ':' || (cp = strchr(opts, c)) == NULL) {
+ ERR(": illegal option -- ", c);
+ if (argv[optind][++sp] == '\0') {
+ optind++;
+ sp = 1;
+ }
+ return ('?');
+ }
+ if (*++cp == ':') {
+ if (argv[optind][sp + 1] != '\0')
+ optarg = &argv[optind++][sp + 1];
+ else if (++optind >= argc) {
+ ERR(": option requires an argument -- ", c);
+ sp = 1;
+ return ('?');
+ } else
+ optarg = argv[optind++];
+ sp = 1;
+ } else {
+ if (argv[optind][++sp] == '\0') {
+ sp = 1;
+ optind++;
+ }
+ optarg = NULL;
+ }
+ return (c);
+}
diff --git a/host/examples/getopt/getopt.h b/host/examples/getopt/getopt.h
new file mode 100644
index 000000000..ada96ecf0
--- /dev/null
+++ b/host/examples/getopt/getopt.h
@@ -0,0 +1,19 @@
+/*
+ * This header is for a function released into the public domain
+ * by AT&T in 1985. See the newsgroup posting:
+ *
+ * Newsgroups: mod.std.unix
+ * Subject: public domain AT&T getopt source
+ * Date: 3 Nov 85 19:34:15 GMT
+ */
+#ifndef _GETOPT_H_
+#define _GETOPT_H_
+
+extern int optarr;
+extern int optind;
+extern int optopt;
+extern char* optarg;
+
+int getopt(int argc, char **argv, char *opts);
+
+#endif /* _GETOPT_H_ */