getopt.c
Go to the documentation of this file.
00001 /*
00002 POSIX getopt for Windows
00003 
00004 AT&T Public License
00005 
00006 Code given out at the 1985 UNIFORUM conference in Dallas.
00007 
00008 Significant updates
00009     2014-12-02: Updated to use CRL_HAVE_GETOPT preprocessor definition and work with modern dialects of C and C++
00010 */
00011 
00012 #include "getopt.h"
00013 
00014 #ifndef CRL_HAVE_GETOPT
00015 
00016 #include <stdio.h>
00017 #include <string.h>
00018 
00019 #define ERR(s, c)                \
00020     if (opterr) {                \
00021         char errbuf[2];          \
00022         errbuf[0] = c;           \
00023         errbuf[1] = '\n';        \
00024         fputs (argv[0], stderr); \
00025         fputs (s, stderr);       \
00026         fputc(c, stderr);        \
00027     }                            \
00028 
00029 int opterr = 1;
00030 int optind = 1;
00031 int optopt;
00032 char* optarg;
00033 
00034 int getopt(int argc, char** argv, char* opts)
00035 {
00036     static int sp = 1;
00037     register int c;
00038     register char *cp;
00039 
00040     if (sp == 1)
00041     {
00042         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
00043         {
00044             return EOF;
00045         }
00046         else if (strcmp (argv[optind], "--") == 0)
00047         {
00048             optind++;
00049             return EOF;
00050         }
00051     }
00052     optopt = c = argv[optind][sp];
00053     if (c == ':' || (cp = strchr(opts, c)) == NULL)
00054     {
00055         ERR (": illegal option -- ", c);
00056         if(argv[optind][++sp] == '\0')
00057         {
00058             optind++;
00059             sp = 1;
00060         }
00061         return '?';
00062     }
00063     if (*++cp == ':')
00064     {
00065         if (argv[optind][sp+1] != '\0')
00066         {
00067             optarg = &argv[optind++][sp+1];
00068         }
00069         else if (++optind >= argc)
00070         {
00071             ERR (": option requires an argument -- ", c);
00072             sp = 1;
00073             return '?';
00074         }
00075         else
00076         {
00077             optarg = argv[optind++];
00078         }
00079         sp = 1;
00080     }
00081     else
00082     {
00083         if (argv[optind][++sp] == '\0')
00084         {
00085             sp = 1;
00086             optind++;
00087         }
00088         optarg = NULL;
00089     }
00090     return c;
00091 }
00092 
00093 #endif


multisense_lib
Author(s):
autogenerated on Fri Apr 5 2019 02:28:24