examples/Stdlib/ConsoleApp/GETOPT.C

00001 /*
00002 Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #if defined(LIBC_SCCS) && !defined(lint)
00032 static char sccsid[] = "@(#)getopt.c    8.2 (Berkeley) 4/2/94";
00033 #endif /* LIBC_SCCS and not lint */
00034 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 
00039 int     opterr = 1,             /* if error message should be printed */
00040         optind = 1,             /* index into parent argv vector */
00041         optopt,                 /* character checked for validity */
00042         optreset;               /* reset getopt */
00043 char    *optarg;                /* argument associated with option */
00044 
00045 #define BADCH   (int)'?'
00046 #define BADARG  (int)':'
00047 #define EMSG    ""
00048 
00049 /*
00050  * getopt --
00051  *      Parse argc/argv argument vector.
00052  */
00053 int
00054 getopt(int nargc, char * const *nargv, const char *ostr)
00055 {
00056 //      extern char *__progname;
00057         static char *place = EMSG;              /* option letter processing */
00058         char *oli=NULL;                         /* option letter list index */
00059 
00060         if (optreset || !*place) {              /* update scanning pointer */
00061                 optreset = 0;
00062                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
00063                         place = EMSG;
00064                         return (EOF);
00065                 }
00066                 if (place[1] && *++place == '-') {      /* found "--" */
00067                         ++optind;
00068                         place = EMSG;
00069                         return (EOF);
00070                 }
00071         }                                       /* option letter okay? */
00072 
00073         optopt = (int)*place++;
00074         if (optopt != (int)':') oli = strchr(ostr, optopt);
00075         if ((optopt == (int)':') || !oli)
00076                 {
00077                 /*
00078                  * if the user didn't specify '-' as an option,
00079                  * assume it means EOF.
00080                  */
00081                 if (optopt == (int)'-')
00082                         return (EOF);
00083                 if (!*place)
00084                         ++optind;
00085                 if (opterr && *ostr != ':')
00086                         (void)fprintf(stderr,
00087 //                          "%s: illegal option -- %c\n", __progname, optopt);
00088                                 "illegal option -- %c\n", optopt);
00089                 return (BADCH);
00090                 }
00091         if (*++oli != ':') {                    /* don't need argument */
00092                 optarg = NULL;
00093                 if (!*place)
00094                         ++optind;
00095         }
00096         else {                                  /* need an argument */
00097                 if (*place)                     /* no white space */
00098                         optarg = place;
00099                 else if (nargc <= ++optind) {   /* no arg */
00100                         place = EMSG;
00101                         if (*ostr == ':')
00102                                 return (BADARG);
00103                         if (opterr)
00104                                 (void)fprintf(stderr,
00105 //                                  "%s: option requires an argument -- %c\n",
00106 //                                  __progname, optopt);
00107                                     "option requires an argument -- %c\n",
00108                                         optopt);
00109                         return (BADCH);
00110                 }
00111                 else                            /* white space */
00112                         optarg = nargv[optind];
00113                 place = EMSG;
00114                 ++optind;
00115         }
00116         return (optopt);                        /* dump back option letter */
00117 }

Generated by  doxygen 1.6.2