/* Getopt for GNU. NOTE: getopt is now part of the C library, so if you don't know what "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu before changing it! Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc. 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 2, 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, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ /* This tells Alpha OSF/1 not to define a getopt prototype in . Ditto for AIX 3.2 and . */ #ifndef _NO_PROTO #define _NO_PROTO #endif #ifdef HAVE_CONFIG_H #include "config.h" #endif #if !defined (__STDC__) || !__STDC__ /* This is a separate conditional since some stdc systems reject `defined (const)'. */ #ifndef const #define const #endif #endif #include #include /* Comment out all this code if we are using the GNU C Library, and are not actually compiling the library itself. This code is part of the GNU C Library, but also included in many other GNU distributions. Compiling and linking in this code is a waste when using the GNU C library (especially if it is a shared library). Rather than having every GNU program understand `configure --with-gnu-libc' and omit the object files, it is simpler to just do this in the source for each such file. */ #if defined (_LIBC) || !defined (__GNU_LIBRARY__) /* This needs to come after some library #include to get __GNU_LIBRARY__ defined. */ #ifdef __GNU_LIBRARY__ /* Don't include stdlib.h for non-GNU C libraries because some of them contain conflicting prototypes for getopt. */ #include #endif /* GNU C library. */ /* This is for other GNU distributions with internationalized messages. The GNU C Library itself does not yet support such messages. */ #if HAVE_LIBINTL_H # include #else # define gettext(msgid) (msgid) #endif /* This version of `getopt' appears to the caller like standard Unix `getopt' but it behaves differently for the user, since it allows the user to intersperse the options with the other arguments. As `getopt' works, it permutes the elements of ARGV so that, when it is done, all the options precede everything else. Thus all application programs are extended to handle flexible argument order. Setting the environment variable POSIXLY_CORRECT disables permutation. Then the behavior is completely standard. GNU application programs can use a third alternative mode in which they can distinguish the relative order of options and other arguments. */ #include "getopt.h" /* For communication from `getopt' to the caller. When `getopt' finds an option that takes an argument, the argument value is returned here. Also, when `ordering' is RETURN_IN_ORDER, each non-option ARGV-element is returned here. */ char *optarg = NULL; /* Index in ARGV of the next element to be scanned. This is used for communication to and from the caller and for communication between successive calls to `getopt'. On entry to `getopt', zero means this is the first call; initialize. When `getopt' returns EOF, this is the index of the first of the non-option elements that the caller should itself scan. Otherwise, `optind' communicates from one call to the next how much of ARGV has been scanned so far. */ /* XXX 1003.2 says this must be 1 before any call. */ int optind = 0; /* The next char to be scanned in the option-element in which the last option character we returned was found. This allows us to pick up the scan where we left off. If this is zero, or a null string, it means resume the scan by advancing to the next ARGV-element. */ static char *nextchar; /* Callers store zero here to inhibit the error message for unrecognized options. */ int opterr = 1; /* Set to an option character which was unrecognized. This must be initialized on some systems to avoid linking in the system's own getopt implementation. */ int optopt = '?'; /* Describe how to deal with options that follow non-option ARGV-elements. If the caller did not specify anything, the default is REQUIRE_ORDER if the environment variable POSIXLY_CORRECT is defined, PERMUTE otherwise. REQUIRE_ORDER means don't recognize them as options; stop option processing when the first non-option is seen. This is what Unix does. This mode of operation is selected by either setting the environment variable POSIXLY_CORRECT, or using `+' as the first character of the list of option characters. PERMUTE is the default. We permute the contents of ARGV as we scan, so that eventually all the non-options are at the end. This allows options to be given in any order, even with programs that were not written to expect this. RETURN_IN_ORDER is an option available to programs that were written to expect options and other ARGV-elements in any order and that care about the ordering of the two. We describe each non-option ARGV-element as if it were the argument of an option with character code 1. Using `-' as the first character of the list of option characters selects this mode of operation. The special argument `--' forces an end of option-scanning regardless of the value of `ordering'. In the case of RETURN_IN_ORDER, only `--' can cause `getopt' to return EOF with `optind' != ARGC. */ static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering; /* Value of POSIXLY_CORRECT environment variable. */ static char *posixly_correct; #ifdef __GNU_LIBRARY__ /* We want to avoid inclusion of string.h with non-GNU libraries because there are many ways it can cause trouble. On some systems, it contains special magic macros that don't work in GCC. */ #include #define my_index strchr #else /* Avoid depending on library functions or files whose names are inconsistent. */ char *getenv (); static char * my_index (str, chr) const char *str; int chr; { while (*str) { if (*str == chr) return (char *) str; str++; } return 0; } /* If using GCC, we can safely declare strlen this way. If not using GCC, it is ok not to declare it. */ #ifdef __GNUC__ /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. That was relevant to code that was here before. */ #if !defined (__STDC__) || !__STDC__ /* gcc with -traditional declares the built-in strlen to return int, and has done so at least since version 2.4.5. -- rms. */ extern int strlen (const char *); #endif /* not __STDC__ */ #endif /* __GNUC__ */ #endif /* not __GNU_LIBRARY__ */ /* Handle permutation of arguments. */ /* Describe the part of ARGV that contains non-options that have been skipped. `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is the index after the last of them. */ static int first_nonopt; static int last_nonopt; /* Exchange two adjacent subsequences of ARGV. One subsequence is elements [first_nonopt,last_nonopt) which contains all the non-options that have been skipped so far. The other is elements [last_nonopt,optind), which contains all the options processed since those non-options were skipped. `first_nonopt' and `last_nonopt' are relocated so that they describe the new indices of the non-options in ARGV after they are moved. */ static void exchange (argv) char **argv; { int bottom = first_nonopt; int middle = last_nonopt; int top = optind; char *tem; /* Exchange the shorter segment with the far end of the longer segment. That puts the shorter segment into the right place. It leaves the longer segment in the right place overall, but it consists of two parts that need to be swapped next. */ while (top > middle && middle > bottom) { if (top - middle > middle - bottom) { /* Bottom segment is the short one. */ int len = middle - bottom; register int i; /* Swap it with the top part of the top segment. */ for (i = 0; i < len; i++) { tem = argv[bottom + i]; argv[bottom + i] = argv[top - (middle - bottom) + i]; argv[top - (middle - bottom) + i] = tem; } /* Exclude the moved bottom segment from further swapping. */ top -= len; } else { /* Top segment is the short one. */ int len = top - middle; register int i; /* Swap it with the bottom part of the bottom segment. */ for (i = 0; i < len; i++) { tem = argv[bottom + i]; argv[bottom + i] = argv[middle + i]; argv[middle + i] = tem; } /* Exclude the moved top segment from further swapping. */ bottom += len; } } /* Update records for the slots the non-options now occupy. */ first_nonopt += (optind - last_nonopt); last_nonopt = optind; } /* Initialize the internal data when the first call is made. */ static const char * _getopt_initialize (optstring) const char *optstring; { /* Start processing options with ARGV-element 1 (since ARGV-element 0 is the program name); the sequence of previously skipped non-option ARGV-elements is empty. */ first_nonopt = last_nonopt = optind = 1; nextchar = NULL; posixly_correct = getenv ("POSIXLY_CORRECT"); /* Determine how to handle the ordering of options and nonoptions. */ if (optstring[0] == '-') { ordering = RETURN_IN_ORDER; ++optstring; } else if (optstring[0] == '+') { ordering = REQUIRE_ORDER; ++optstring; } else if (posixly_correct != NULL) ordering = REQUIRE_ORDER; else ordering = PERMUTE; return optstring; } /* Scan elements of ARGV (whose length is ARGC) for option characters given in OPTSTRING. If an element of ARGV starts with '-', and is not exactly "-" or "--", then it is an option element. The characters of this element (aside from the initial '-') are option characters. If `getopt' is called repeatedly, it returns successively each of the option characters from each of the option elements. If `getopt' finds another option character, it returns that character, updating `optind' and `nextchar' so that the next call to `getopt' can resume the scan with the following option character or ARGV-element. If there are no more option characters, `getopt' returns `EOF'. Then `optind' is the index in ARGV of the first ARGV-element that is not an option. (The ARGV-elements have been permuted so that those that are not options now come last.) OPTSTRING is a string containing the legitimate option characters. If an option character is seen that is not listed in OPTSTRING, return '?' after printing an error message. If you set `opterr' to zero, the error message is suppressed but we still return '?'. If a char in OPTSTRING is followed by a colon, that means it wants an arg, so the following text in the same ARGV-element, or the text of the following ARGV-element, is returned in `optarg'. Two colons mean an option that wants an optional arg; if there is text in the current ARGV-element, it is returned in `optarg', otherwise `optarg' is set to zero. If OPTSTRING starts with `-' or `+', it requests different methods of handling the non-option ARGV-elements. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. Long-named options begin with `--' instead of `-'. The
Note that this file is kept for historic reference.
It will no longer be updated or maintained.
The recent release history can be obtained by looking
at the Git tags.

-- Matthias Andree, 2010-02-06

SVN release - fetchmail release    - release manager
====================================================
              SORT_BY (release)
r5480       - 6.3.14   (2010-02-05) - MA
r5450       - 6.3.13   (2009-10-30) - MA
r5439       - 6.3.12   (2009-10-05) - MA
r5398       - 6.3.11   (2009-08-06) - MA
r5373       - 6.3.10   (2009-07-02) - MA
r5248       - 6.3.9    (2008-11-16) - MA
r5093       - 6.3.8    (2007-04-06) - MA
r5037       - 6.3.7    (2007-02-18) - MA
r5010       - 6.3.6    (2007-01-05) - MA
r4921       - 6.3.5    (2006-10-09) - MA
r4802       - 6.3.4    (2006-04-14) - MA
r4760       - 6.3.3    (2006-03-31) - MA
r4678       - 6.3.2    (2006-01-22) - MA
r4579       - 6.3.1    (2005-12-19) - MA
r4573       - 6.2.5.5  (2005-12-19) - MA
r4499       - 6.3.0    (2005-11-30) - MA
r4439       - 6.2.5.4  (2005-11-13) - MA
r4430       - 6.2.5.3  (2005-11-13) - MA
(tarball)   - 6.2.5.2  (2005-07-22) - MA
(tarball)   - 6.2.5.1  (2005-07-20) - MA
-- maintainer change --
r3860       - 6.2.5    (2003-10-15) - ESR
r3835       - 6.2.4    (2003-08-13) - ESR
r3826       - 6.2.3    (2003-07-17) - ESR
r3806       - 6.2.2    (2003-03-01) - ESR
r3783       - 6.2.1    (2003-01-14) - ESR
r3776       - 6.2.0    (2002-12-13) - ESR
r3773       - 6.1.3    (2002-11-28) - ESR
r3765       - 6.1.2    (2002-10-31) - ESR
r3755       - 6.1.1    (2002-10-18) - ESR
r3731       - 6.1.0    (2002-09-23) - ESR
r3726       - 6.0.0    (2002-09-18) - ESR
r3703       - 5.9.14   (2002-09-06) - ESR
r3655       - 5.9.13   (2002-06-23) - ESR
r3632       - 5.9.12   (2002-06-04) - ESR
r3608       - 5.9.11   (2002-04-02) - ESR
r3598       - 5.9.10   (2002-03-10) - ESR
r3593       - 5.9.9    (2002-03-09) - ESR
r3581       - 5.9.8    (2002-02-15) - ESR
r3576       - 5.9.7    (2002-02-02) - ESR
r3564       - 5.9.6    (2001-12-14) - ESR
r3548       - 5.9.5    (2001-11-08) - ESR
r3534       - 5.9.4    (2001-10-03) - ESR
r3514       - 5.9.3    (2001-09-30) - ESR
r3493       - 5.9.2    (2001-09-26) - ESR
r3481       - 5.9.1    (2001-09-25) - ESR
r3453       - 5.9.0    (2001-08-13) - ESR
r3447       - 5.8.17   (2001-08-08) - ESR
r3438       - 5.8.16   (2001-08-04) - ESR
r3429       - 5.8.15   (2001-07-31) - ESR
r3413       - 5.8.13   (2001-07-11) - ESR
r3403       - 5.8.12   (2001-07-06) - ESR
r3389       - 5.8.11   (2001-07-02) - ESR
r3385       - 5.8.10   (2001-06-25) - ESR
r3380       - 5.8.9    (2001-06-25) - ESR
r3364       - 5.8.8    (2001-06-20) - ESR
r3355       - 5.8.7    (2001-06-17) - ESR
r3338       - 5.8.6    (2001-06-12) - ESR
r3332       - 5.8.5    (2001-05-30) - ESR
r3322       - 5.8.4    (2001-05-21) - ESR
r3312       - 5.8.3    (2001-05-12) - ESR
r3298       - 5.8.2    (2001-04-26) - ESR
r3291       - 5.8.1    (2001-04-10) - ESR
r3284       - 5.8.0    (2001-04-02) - ESR
r3280       - 5.7.7    (2001-03-29) - ESR
r3272       - 5.7.6    (2001-03-22) - ESR
r3266       - 5.7.5    (2001-03-18) - ESR
r3243       - 5.7.4    (2001-03-12) - ESR
r3236       - 5.7.3    (2001-03-11) - ESR
r3201       - 5.7.2    (2001-03-05) - ESR
r3187       - 5.7.1    (2001-03-04) - ESR
r3174       - 5.7.0    (2001-03-02) - ESR
r3134       - 5.6.8    (2001-02-22) - ESR
r3119       - 5.6.7    (2001-02-19) - ESR
r3112       - 5.6.6    (2001-02-16) - ESR
r3086       - 5.6.5    (2001-02-12) - ESR
r3047       - 5.6.4    (2001-02-11) - ESR
r3036       - 5.6.3    (2001-02-07) - ESR
r3019       - 5.6.2    (2001-01-05) - ESR
r2999       - 5.6.1    (2000-12-12) - ESR
r2990       - 5.6.0    (2000-11-27) - ESR
r2983       - 5.5.6    (2000-11-11) - ESR
r2979       - 5.5.5    (2000-10-17) - ESR
r2976       - 5.5.4    (2000-10-08) - ESR
r2971       - 5.5.3    (2000-09-26) - ESR
r2963       - 5.5.2    (2000-09-08) - ESR
r2953       - 5.5.1    (2000-08-21) - ESR
r2950       - 5.5.0    (2000-08-12) - ESR
r2945       - 5.4.5    (2000-08-07) - ESR
r2935       - 5.4.4    (2000-07-23) - ESR
r2924       - 5.4.3    (2000-07-02) - ESR
r2903       - 5.4.1    (2000-06-07) - ESR
r2897       - 5.4.0    (2000-05-14) - ESR
r2885       - 5.3.8    (2000-04-21) - ESR
r2877       - 5.3.7    (2000-04-16) - ESR
r2870       - 5.3.6    (2000-04-08) - ESR
r2849       - 5.3.5    (2000-03-30) - ESR
r2835       - 5.3.4    (2000-03-22) - ESR
r2821       - 5.3.3    (2000-03-13) - ESR
r2807       - 5.3.2    (2000-03-07) - ESR
r2788       - 5.3.1    (2000-03-06) - ESR
r2768       - 5.3.0    (2000-02-22) - ESR
r2749       - 5.2.8    (2000-02-15) - ESR
r2734       - 5.2.7    (2000-02-07) - ESR
r2731       - 5.2.6    (2000-02-05) - ESR
r2724       - 5.2.5    (2000-01-31) - ESR
r2718       - 5.2.4    (2000-01-17) - ESR
r2702       - 5.2.3    (2000-01-04) -