examples/Stdlib/ConsoleApp/GETOPT.C

00001 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #if defined(LIBC_SCCS) && !defined(lint)
00017 static char sccsid[] = "@(#)getopt.c    8.2 (Berkeley) 4/2/94";
00018 #endif /* LIBC_SCCS and not lint */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 int     opterr = 1,             /* if error message should be printed */
00025         optind = 1,             /* index into parent argv vector */
00026         optopt,                 /* character checked for validity */
00027         optreset;               /* reset getopt */
00028 char    *optarg;                /* argument associated with option */
00029 
00030 #define BADCH   (int)'?'
00031 #define BADARG  (int)':'
00032 #define EMSG    ""
00033 
00034 /*
00035  * getopt --
00036  *      Parse argc/argv argument vector.
00037  */
00038 int
00039 getopt(int nargc, char * const *nargv, const char *ostr)
00040 {
00041 //      extern char *__progname;
00042         static char *place = EMSG;              /* option letter processing */
00043         char *oli=NULL;                         /* option letter list index */
00044 
00045         if (optreset || !*place) {              /* update scanning pointer */
00046                 optreset = 0;
00047                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
00048                         place = EMSG;
00049                         return (EOF);
00050                 }
00051                 if (place[1] && *++place == '-') {      /* found "--" */
00052                         ++optind;
00053                         place = EMSG;
00054                         return (EOF);
00055                 }
00056         }                                       /* option letter okay? */
00057 
00058         optopt = (int)*place++;
00059         if (optopt != (int)':') oli = strchr(ostr, optopt);
00060         if ((optopt == (int)':') || !oli)
00061                 {
00062                 /*
00063                  * if the user didn't specify '-' as an option,
00064                  * assume it means EOF.
00065                  */
00066                 if (optopt == (int)'-')
00067                         return (EOF);
00068                 if (!*place)
00069                         ++optind;
00070                 if (opterr && *ostr != ':')
00071                         (void)fprintf(stderr,
00072 //                          "%s: illegal option -- %c\n", __progname, optopt);
00073                                 "illegal option -- %c\n", optopt);
00074                 return (BADCH);
00075                 }
00076         if (*++oli != ':') {                    /* don't need argument */
00077                 optarg = NULL;
00078                 if (!*place)
00079                         ++optind;
00080         }
00081         else {                                  /* need an argument */
00082                 if (*place)                     /* no white space */
00083                         optarg = place;
00084                 else if (nargc <= ++optind) {   /* no arg */
00085                         place = EMSG;
00086                         if (*ostr == ':')
00087                                 return (BADARG);
00088                         if (opterr)
00089                                 (void)fprintf(stderr,
00090 //                                  "%s: option requires an argument -- %c\n",
00091 //                                  __progname, optopt);
00092                                     "option requires an argument -- %c\n",
00093                                         optopt);
00094                         return (BADCH);
00095                 }
00096                 else                            /* white space */
00097                         optarg = nargv[optind];
00098                 place = EMSG;
00099                 ++optind;
00100         }
00101         return (optopt);                        /* dump back option letter */
00102 }

Generated by  doxygen 1.6.2