Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

getopt.c

Go to the documentation of this file.
00001 /* Getopt for GNU.
00002    NOTE: getopt is now part of the C library, so if you don't know what
00003    "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
00004    before changing it!
00005 
00006    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95
00007         Free Software Foundation, Inc.
00008 
00009    This program is free software; you can redistribute it and/or modify it
00010    under the terms of the GNU General Public License as published by the
00011    Free Software Foundation; either version 2, or (at your option) any
00012    later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
00022 
00023 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 
00024    Ditto for AIX 3.2 and <stdlib.h>.  */
00025 #ifndef _NO_PROTO
00026 #define _NO_PROTO
00027 #endif
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032 
00033 #if !defined (__STDC__) || !__STDC__
00034 /* This is a separate conditional since some stdc systems reject `defined
00035    (const)'.
00036  */
00037 #ifndef const
00038 #define const
00039 #endif
00040 #endif
00041 
00042 #include <stdio.h>
00043 
00044 /* Comment out all this code if we are using the GNU C Library, and are not
00045    actually compiling the library itself.  This code is part of the GNU C
00046    Library, but also included in many other GNU distributions.  Compiling
00047    and linking in this code is a waste when using the GNU C library
00048    (especially if it is a shared library).  Rather than having every GNU
00049    program understand `configure --with-gnu-libc' and omit the object files,
00050    it is simpler to just do this in the source for each such file. */
00051 
00052 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
00053 
00054 
00055 /* This needs to come after some library #include to get __GNU_LIBRARY__
00056    defined. */
00057 #ifdef  __GNU_LIBRARY__
00058 /* Don't include stdlib.h for non-GNU C libraries because some of them
00059    contain conflicting prototypes for getopt. */
00060 #include <stdlib.h>
00061 #endif                          /* GNU C library.  */
00062 
00063 #ifndef _
00064 /* This is for other GNU distributions with internationalized messages.
00065    When compiling libc, the _ macro is predefined. */
00066 #ifdef HAVE_LIBINTL_H
00067 # include <libintl.h>
00068 # define _(msgid)       gettext (msgid)
00069 #else
00070 # define _(msgid)       (msgid)
00071 #endif
00072 #endif
00073 
00074 /* This version of `getopt' appears to the caller like standard Unix
00075    `getopt' but it behaves differently for the user, since it allows the
00076    user to intersperse the options with the other arguments.
00077 
00078    As `getopt' works, it permutes the elements of ARGV so that, when it is
00079    done, all the options precede everything else.  Thus all application
00080    programs are extended to handle flexible argument order.
00081 
00082    Setting the environment variable POSIXLY_CORRECT disables permutation. 
00083    Then the behavior is completely standard.
00084 
00085    GNU application programs can use a third alternative mode in which they
00086    can distinguish the relative order of options and other arguments. */
00087 
00088 #include "getopt.h"
00089 
00090 /* For communication from `getopt' to the caller.  When `getopt' finds an
00091    option that takes an argument, the argument value is returned here. 
00092    Also, when `ordering' is RETURN_IN_ORDER, each non-option ARGV-element is
00093    returned here. */
00094 
00095 char *optarg = NULL;
00096 
00097 /* Index in ARGV of the next element to be scanned.  This is used for
00098    communication to and from the caller and for communication between
00099    successive calls to `getopt'.
00100 
00101    On entry to `getopt', zero means this is the first call; initialize.
00102 
00103    When `getopt' returns EOF, this is the index of the first of the
00104    non-option elements that the caller should itself scan.
00105 
00106    Otherwise, `optind' communicates from one call to the next how much of
00107    ARGV has been scanned so far. */
00108 
00109 /* XXX 1003.2 says this must be 1 before any call.  */
00110 int optind = 0;
00111 
00112 /* The next char to be scanned in the option-element in which the last
00113    option character we returned was found.  This allows us to pick up the
00114    scan where we left off.
00115 
00116    If this is zero, or a null string, it means resume the scan by advancing
00117    to the next ARGV-element. */
00118 
00119 static char *nextchar;
00120 
00121 /* Callers store zero here to inhibit the error message for unrecognized
00122    options. */
00123 
00124 int opterr = 1;
00125 
00126 /* Set to an option character which was unrecognized.  This must be
00127    initialized on some systems to avoid linking in the system's own getopt
00128    implementation. */
00129 
00130 int optopt = '?';
00131 
00132 /* Describe how to deal with options that follow non-option ARGV-elements.
00133 
00134    If the caller did not specify anything, the default is REQUIRE_ORDER if
00135    the environment variable POSIXLY_CORRECT is defined, PERMUTE otherwise.
00136 
00137    REQUIRE_ORDER means don't recognize them as options; stop option
00138    processing when the first non-option is seen.  This is what Unix does. 
00139    This mode of operation is selected by either setting the environment
00140    variable POSIXLY_CORRECT, or using `+' as the first character of the list
00141    of option characters.
00142 
00143    PERMUTE is the default.  We permute the contents of ARGV as we scan, so
00144    that eventually all the non-options are at the end.  This allows options
00145    to be given in any order, even with programs that were not written to
00146    expect this.
00147 
00148    RETURN_IN_ORDER is an option available to programs that were written to
00149    expect options and other ARGV-elements in any order and that care about
00150    the ordering of the two.  We describe each non-option ARGV-element as if
00151    it were the argument of an option with character code 1.  Using `-' as
00152    the first character of the list of option characters selects this mode of
00153    operation.
00154 
00155    The special argument `--' forces an end of option-scanning regardless of
00156    the value of `ordering'.  In the case of RETURN_IN_ORDER, only `--' can
00157    cause `getopt' to return EOF with `optind' != ARGC. */
00158 
00159 static enum {
00160         REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
00161 } ordering;
00162 
00163 /* Value of POSIXLY_CORRECT environment variable.  */
00164 static char *posixly_correct;
00165 
00166 #ifdef  __GNU_LIBRARY__
00167 
00168 /* We want to avoid inclusion of string.h with non-GNU libraries because
00169    there are many ways it can cause trouble.  On some systems, it contains
00170    special magic macros that don't work in GCC. */
00171 #include <string.h>
00172 #define my_index        strchr
00173 #else
00174 
00175 /* Avoid depending on library functions or files
00176    whose names are inconsistent.  */
00177 
00178 char *getenv();
00179 int strncmp(const char *, const char *, size_t);
00180 
00181 static char *my_index(str, chr)
00182 const char *str;
00183 int chr;
00184 {
00185         while (*str) {
00186                 if (*str == chr)
00187                         return (char *) str;
00188                 str++;
00189         }
00190         return 0;
00191 }
00192 
00193 /* If using GCC, we can safely declare strlen this way.
00194    If not using GCC, it is ok not to declare it.  */
00195 #ifdef __GNUC__
00196 
00197 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.  That
00198    was relevant to code that was here before. */
00199 #if !defined (__STDC__) || !__STDC__
00200 
00201 /* gcc with -traditional declares the built-in strlen to return int, and has
00202    done so at least since version 2.4.5. -- rms. */
00203 extern int strlen(const char *);
00204 #endif                          /* not __STDC__ */
00205 #endif                          /* __GNUC__ */
00206 
00207 #endif                          /* not __GNU_LIBRARY__ */
00208 
00209 /* Handle permutation of arguments.  */
00210 
00211 /* Describe the part of ARGV that contains non-options that have been
00212    skipped.  `first_nonopt' is the index in ARGV of the first of them;
00213    `last_nonopt' is the index after the last of them. */
00214 
00215 static int first_nonopt;
00216 static int last_nonopt;
00217 
00218 /* Exchange two adjacent subsequences of ARGV.  One subsequence is elements
00219    [first_nonopt,last_nonopt) which contains all the non-options that have
00220    been skipped so far.  The other is elements [last_nonopt,optind), which
00221    contains all the options processed since those non-options were skipped.
00222 
00223    `first_nonopt' and `last_nonopt' are relocated so that they describe the
00224    new indices of the non-options in ARGV after they are moved. */
00225 
00226 static void exchange(argv)
00227 char **argv;
00228 {
00229         int bottom = first_nonopt;
00230         int middle = last_nonopt;
00231         int top = optind;
00232         char *tem;
00233 
00234         /* Exchange the shorter segment with the far end of the longer
00235            segment.  That puts the shorter segment into the right place.  It
00236            leaves the longer segment in the right place overall, but it
00237            consists of two parts that need to be swapped next. */
00238 
00239         while (top > middle && middle > bottom) {
00240                 if (top - middle > middle - bottom) {
00241                         /* Bottom segment is the short one.  */
00242                         int len = middle - bottom;
00243                         register int i;
00244 
00245                         /* Swap it with the top part of the top segment.  */
00246                         for (i = 0; i < len; i++) {
00247                                 tem = argv[bottom + i];
00248                                 argv[bottom + i] =
00249                                     argv[top - (middle - bottom) + i];
00250                                 argv[top - (middle - bottom) + i] = tem;
00251                         }
00252                         /* Exclude the moved bottom segment from further swapping.  */
00253                         top -= len;
00254                 } else {
00255                         /* Top segment is the short one.  */
00256                         int len = top - middle;
00257                         register int i;
00258 
00259                         /* Swap it with the bottom part of the bottom segment.  */
00260                         for (i = 0; i < len; i++) {
00261                                 tem = argv[bottom + i];
00262                                 argv[bottom + i] = argv[middle + i];
00263                                 argv[middle + i] = tem;
00264                         }
00265                         /* Exclude the moved top segment from further swapping.  */
00266                         bottom += len;
00267                 }
00268         }
00269 
00270         /* Update records for the slots the non-options now occupy.  */
00271 
00272         first_nonopt += (optind - last_nonopt);
00273         last_nonopt = optind;
00274 }
00275 
00276 /* Initialize the internal data when the first call is made.  */
00277 
00278 static const char *_getopt_initialize(optstring)
00279 const char *optstring;
00280 {
00281         /* Start processing options with ARGV-element 1 (since ARGV-element
00282            0 is the program name); the sequence of previously skipped
00283            non-option ARGV-elements is empty. */
00284 
00285         first_nonopt = last_nonopt = optind = 1;
00286 
00287         nextchar = NULL;
00288 
00289         posixly_correct = getenv("POSIXLY_CORRECT");
00290 
00291         /* Determine how to handle the ordering of options and nonoptions. */
00292 
00293         if (optstring[0] == '-') {
00294                 ordering = RETURN_IN_ORDER;
00295                 ++optstring;
00296         } else if (optstring[0] == '+') {
00297                 ordering = REQUIRE_ORDER;
00298                 ++optstring;
00299         } else if (posixly_correct != NULL)
00300                 ordering = REQUIRE_ORDER;
00301         else
00302                 ordering = PERMUTE;
00303 
00304         return optstring;
00305 }
00306 
00307 /* Scan elements of ARGV (whose length is ARGC) for option characters given
00308    in OPTSTRING.
00309 
00310    If an element of ARGV starts with '-', and is not exactly "-" or "--",
00311    then it is an option element.  The characters of this element (aside from
00312    the initial '-') are option characters.  If `getopt' is called
00313    repeatedly, it returns successively each of the option characters from
00314    each of the option elements.
00315 
00316    If `getopt' finds another option character, it returns that character,
00317    updating `optind' and `nextchar' so that the next call to `getopt' can
00318    resume the scan with the following option character or ARGV-element.
00319 
00320    If there are no more option characters, `getopt' returns `EOF'.  Then
00321    `optind' is the index in ARGV of the first ARGV-element that is not an
00322    option.  (The ARGV-elements have been permuted so that those that are not
00323    options now come last.)
00324 
00325    OPTSTRING is a string containing the legitimate option characters.  If an
00326    option character is seen that is not listed in OPTSTRING, return '?'
00327    after printing an error message.  If you set `opterr' to zero, the error
00328    message is suppressed but we still return '?'.
00329 
00330    If a char in OPTSTRING is followed by a colon, that means it wants an
00331    arg, so the following text in the same ARGV-element, or the text of the
00332    following ARGV-element, is returned in `optarg'.  Two colons mean an
00333    option that wants an optional arg; if there is text in the current
00334    ARGV-element, it is returned in `optarg', otherwise `optarg' is set to
00335    zero.
00336 
00337    If OPTSTRING starts with `-' or `+', it requests different methods of
00338    handling the non-option ARGV-elements.  See the comments about
00339    RETURN_IN_ORDER and REQUIRE_ORDER, above.
00340 
00341    Long-named options begin with `--' instead of `-'.  Their names may be
00342    abbreviated as long as the abbreviation is unique or is an exact match
00343    for some defined option.  If they have an argument, it follows the option
00344    name in the same ARGV-element, separated from the option name by a `=',
00345    or else the in next ARGV-element.  When `getopt' finds a long-named
00346    option, it returns 0 if that option's `flag' field is nonzero, the value
00347    of the option's `val' field if the `flag' field is zero.
00348 
00349    The elements of ARGV aren't really const, because we permute them.  But
00350    we pretend they're const in the prototype to be compatible with other
00351    systems.
00352 
00353    LONGOPTS is a vector of `struct option' terminated by an element
00354    containing a name which is zero.
00355 
00356    LONGIND returns the index in LONGOPT of the long-named option found.  It
00357    is only valid when a long-named option has been found by the most recent
00358    call.
00359 
00360    If LONG_ONLY is nonzero, '-' as well as '--' can introduce long-named
00361    options.  */
00362 
00363 int _getopt_internal(argc, argv, optstring, longopts, longind, long_only)
00364 int argc;
00365 char *const *argv;
00366 const char *optstring;
00367 const struct option *longopts;
00368 int *longind;
00369 int long_only;
00370 {
00371         optarg = NULL;
00372 
00373         if (optind == 0) {
00374                 optstring = _getopt_initialize(optstring);
00375                 optind = 1;     /* Don't scan ARGV[0], the program name.  */
00376         }
00377 
00378         if (nextchar == NULL || *nextchar == '\0') {
00379                 /* Advance to the next ARGV-element.  */
00380 
00381                 if (ordering == PERMUTE) {
00382                         /* If we have just processed some options following
00383                            some non-options, exchange them so that the
00384                            options come first. */
00385 
00386                         if (first_nonopt != last_nonopt
00387                             && last_nonopt != optind)
00388                                 exchange((char **) argv);
00389                         else if (last_nonopt != optind)
00390                                 first_nonopt = optind;
00391 
00392                         /* Skip any additional non-options and extend the
00393                            range of non-options previously skipped. */
00394 
00395                         while (optind < argc
00396                                && (argv[optind][0] != '-'
00397                                    || argv[optind][1] == '\0'))
00398                                 optind++;
00399                         last_nonopt = optind;
00400                 }
00401 
00402                 /* The special ARGV-element `--' means premature end of
00403                    options.  Skip it like a null option, then exchange with
00404                    previous non-options as if it were an option, then skip
00405                    everything else like a non-option. */
00406 
00407                 if (optind != argc && !strcmp(argv[optind], "--")) {
00408                         optind++;
00409 
00410                         if (first_nonopt != last_nonopt
00411                             && last_nonopt != optind)
00412                                 exchange((char **) argv);
00413                         else if (first_nonopt == last_nonopt)
00414                                 first_nonopt = optind;
00415                         last_nonopt = argc;
00416 
00417                         optind = argc;
00418                 }
00419 
00420                 /* If we have done all the ARGV-elements, stop the scan and
00421                    back over any non-options that we skipped and permuted.
00422                    */
00423 
00424                 if (optind == argc) {
00425                         /* Set the next-arg-index to point at the
00426                            non-options that we previously skipped, so the
00427                            caller will digest them. */
00428                         if (first_nonopt != last_nonopt)
00429                                 optind = first_nonopt;
00430                         return EOF;
00431                 }
00432 
00433                 /* If we have come to a non-option and did not permute it,
00434                    either stop the scan or describe it to the caller and
00435                    pass it by. */
00436                 if ((argv[optind][0] != '-' || argv[optind][1] == '\0')) {
00437                         if (ordering == REQUIRE_ORDER)
00438                                 return EOF;
00439                         optarg = argv[optind++];
00440                         return 1;
00441                 }
00442 
00443                 /* We have found another option-ARGV-element.  Skip the
00444                    initial punctuation. */
00445 
00446                 nextchar = (argv[optind] + 1
00447                             + (longopts != NULL
00448                                && argv[optind][1] == '-'));
00449         }
00450 
00451         /* Decode the current option-ARGV-element.  */
00452 
00453         /* Check whether the ARGV-element is a long option.
00454 
00455            If long_only and the ARGV-element has the form "-f", where f is a
00456            valid short option, don't consider it an abbreviated form of a
00457            long option that starts with f.  Otherwise there would be no way
00458            to give the -f short option.
00459 
00460            On the other hand, if there's a long option "fubar" and the
00461            ARGV-element is "-fu", do consider that an abbreviation of the
00462            long option, just like "--fu", and not "-f" with arg "u".
00463 
00464            This distinction seems to be the most useful approach.  */
00465 
00466         if (longopts != NULL
00467             && (argv[optind][1] == '-' || (long_only && (argv[optind][2]
00468                                                          ||
00469                                                          !my_index
00470                                                          (optstring,
00471                                                           argv[optind]
00472                                                           [1]))))) {
00473                 char *nameend;
00474                 const struct option *p;
00475                 const struct option *pfound == NULL;
00476                 int exact = 0;
00477                 int ambig = 0;
00478                 int indfound = 0;
00479                 int option_index;
00480 
00481                 for (nameend = nextchar; *nameend && *nameend != '=';
00482                      nameend++)
00483                         /* Do nothing.  */ ;
00484 
00485                 /* Test all long options for either exact match
00486                    or abbreviated matches.  */
00487                 for (p = longopts, option_index = 0; p->name;
00488                      p++, option_index++)
00489                         if (!strncmp
00490                             (p->name, nextchar, nameend - nextchar)) {
00491                                 if (nameend - nextchar == strlen(p->name)) {
00492                                         /* Exact match found.  */
00493                                         pfound = p;
00494                                         indfound = option_index;
00495                                         exact = 1;
00496                                         break;
00497                                 } else if (pfound == NULL) {
00498                                         /* First nonexact match found.  */
00499                                         pfound = p;
00500                                         indfound = option_index;
00501                                 } else
00502                                         /* Second or later nonexact match found.  */
00503                                         ambig = 1;
00504                         }
00505 
00506                 if (ambig && !exact) {
00507                         if (opterr)
00508                                 fprintf(stderr,
00509                                         _
00510                                         ("%s: option `%s' is ambiguous\n"),
00511                                         argv[0], argv[optind]);
00512                         nextchar += strlen(nextchar);
00513                         optind++;
00514                         return '?';
00515                 }
00516 
00517                 if (pfound != NULL) {
00518                         option_index = indfound;
00519                         optind++;
00520                         if (*nameend) {
00521                                 /* Don't test has_arg with >, because some C compilers don't
00522                                    allow it to be used on enums.  */
00523                                 if (pfound->has_arg)
00524                                         optarg = nameend + 1;
00525                                 else {
00526                                         if (opterr) {
00527                                                 if (argv[optind - 1][1] ==
00528                                                     '-')
00529                                                         /* --option */
00530                                                         fprintf(stderr,
00531                                                                 _
00532                                                                 ("%s: option `--%s' doesn't allow an argument\n"),
00533                                                                 argv[0],
00534                                                                 pfound->
00535                                                                 name);
00536                                                 else
00537                                                         /* +option or -option */
00538                                                         fprintf(stderr,
00539                                                                 _
00540                                                                 ("%s: option `%c%s' doesn't allow an argument\n"),
00541                                                                 argv[0],
00542                                                                 argv[optind
00543                                                                      -
00544                                                                      1][0],
00545                                                                 pfound->
00546                                                                 name);
00547                                         }
00548 
00549                                         nextchar += strlen(nextchar);
00550                                         return '?';
00551                                 }
00552                         } else if (pfound->has_arg == 1) {
00553                                 if (optind < argc)
00554                                         optarg = argv[optind++];
00555                                 else {
00556                                         if (opterr)
00557                                                 fprintf(stderr,
00558                                                         _
00559                                                         ("%s: option `%s' requires an argument\n"),
00560                                                         argv[0],
00561                                                         argv[optind - 1]);
00562                                         nextchar += strlen(nextchar);
00563                                         return optstring[0] ==
00564                                             ':' ? ':' : '?';
00565                                 }
00566                         }
00567                         nextchar += strlen(nextchar);
00568                         if (longind != NULL)
00569                                 *longind = option_index;
00570                         if (pfound->flag) {
00571                                 *(pfound->flag) = pfound->val;
00572                                 return 0;
00573                         }
00574                         return pfound->val;
00575                 }
00576 
00577                 /* Can't find it as a long option.  If this is not
00578                    getopt_long_only, or the option starts with '--' or is
00579                    not a valid short option, then it's an error.  Otherwise
00580                    interpret it as a short option. */
00581                 if (!long_only || argv[optind][1] == '-'
00582                     || my_index(optstring, *nextchar) == NULL) {
00583                         if (opterr) {
00584                                 if (argv[optind][1] == '-')
00585                                         /* --option */
00586                                         fprintf(stderr,
00587                                                 _
00588                                                 ("%s: unrecognized option `--%s'\n"),
00589                                                 argv[0], nextchar);
00590                                 else
00591                                         /* +option or -option */
00592                                         fprintf(stderr,
00593                                                 _
00594                                                 ("%s: unrecognized option `%c%s'\n"),
00595                                                 argv[0], argv[optind][0],
00596                                                 nextchar);
00597                         }
00598                         nextchar = (char *) "";
00599                         optind++;
00600                         return '?';
00601                 }
00602         }
00603 
00604         /* Look at and handle the next short option-character.  */
00605 
00606         {
00607                 char c = *nextchar++;
00608                 char *temp = my_index(optstring, c);
00609 
00610                 /* Increment `optind' when we start to process its last character.  */
00611                 if (*nextchar == '\0')
00612                         ++optind;
00613 
00614                 if (temp == NULL || c == ':') {
00615                         if (opterr) {
00616                                 if (posixly_correct)
00617                                         /* 1003.2 specifies the format of this message.  */
00618                                         fprintf(stderr,
00619                                                 _
00620                                                 ("%s: illegal option -- %c\n"),
00621                                                 argv[0], c);
00622                                 else
00623                                         fprintf(stderr,
00624                                                 _
00625                                                 ("%s: invalid option -- %c\n"),
00626                                                 argv[0], c);
00627                         }
00628                         optopt = c;
00629                         return '?';
00630                 }
00631                 if (temp[1] == ':') {
00632                         if (temp[2] == ':') {
00633                                 /* This is an option that accepts an argument optionally.  */
00634                                 if (*nextchar != '\0') {
00635                                         optarg = nextchar;
00636                                         optind++;
00637                                 } else
00638                                         optarg = NULL;
00639                                 nextchar = NULL;
00640                         } else {
00641                                 /* This is an option that requires an argument.  */
00642                                 if (*nextchar != '\0') {
00643                                         optarg = nextchar;
00644                                         /* If we end this ARGV-element by taking the rest as an arg,
00645                                            we must advance to the next element now.  */
00646                                         optind++;
00647                                 } else if (optind == argc) {
00648                                         if (opterr) {
00649                                                 /* 1003.2 specifies the format of this message.  */
00650                                                 fprintf(stderr,
00651                                                         _
00652                                                         ("%s: option requires an argument -- %c\n"),
00653                                                         argv[0], c);
00654                                         }
00655                                         optopt = c;
00656                                         if (optstring[0] == ':')
00657                                                 c = ':';
00658                                         else
00659                                                 c = '?';
00660                                 } else
00661                                         /* We already incremented `optind' once;
00662                                            increment it again when taking next ARGV-elt as argument.  */
00663                                         optarg = argv[optind++];
00664                                 nextchar = NULL;
00665                         }
00666                 }
00667                 return c;
00668         }
00669 }
00670 
00671 int getopt(argc, argv, optstring)
00672 int argc;
00673 char *const *argv;
00674 const char *optstring;
00675 {
00676         return _getopt_internal(argc, argv, optstring,
00677                                 (const struct option *) 0, (int *) 0, 0);
00678 }
00679 
00680 #endif                          /* _LIBC or not __GNU_LIBRARY__.  */
00681 
00682 #ifdef TEST
00683 
00684 /* Compile with -DTEST to make an executable for use in testing
00685    the above definition of `getopt'.  */
00686 
00687 int main(argc, argv)
00688 int argc;
00689 char **argv;
00690 {
00691         int c;
00692         int digit_optind = 0;
00693 
00694         while (1) {
00695                 int this_option_optind = optind ? optind : 1;
00696 
00697                 c = getopt(argc, argv, "abc:d:0123456789");
00698                 if (c == EOF)
00699                         break;
00700 
00701                 switch (c) {
00702                 case '0':
00703                 case '1':
00704                 case '2':
00705                 case '3':
00706                 case '4':
00707                 case '5':
00708                 case '6':
00709                 case '7':
00710                 case '8':
00711                 case '9':
00712                         if (digit_optind != 0
00713                             && digit_optind != this_option_optind)
00714                                 printf
00715                                     ("digits occur in two different argv-elements.\n");
00716                         digit_optind = this_option_optind;
00717                         printf("option %c\n", c);
00718                         break;
00719 
00720                 case 'a':
00721                         printf("option a\n");
00722                         break;
00723 
00724                 case 'b':
00725                         printf("option b\n");
00726                         break;
00727 
00728                 case 'c':
00729                         printf("option c with value `%s'\n", optarg);
00730                         break;
00731 
00732                 case '?':
00733                         break;
00734 
00735                 default:
00736                         printf
00737                             ("?? getopt returned character code 0%o ??\n",
00738                              c);
00739                 }
00740         }
00741 
00742         if (optind < argc) {
00743                 printf("non-option ARGV-elements: ");
00744                 while (optind < argc)
00745                         printf("%s ", argv[optind++]);
00746                 printf("\n");
00747         }
00748 
00749         exit(0);
00750 }
00751 
00752 #endif                          /* TEST */

Generated on Tue Apr 9 13:53:07 2002 for RWIL - Real World Interface Library by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001