History log of /openbsd-current/lib/libc/stdlib/getopt_long.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.32 27-May-2020 schwarze

This patch fixes one bug and one instance of undesirable behaviour.

The bug, present since 4.4BSD, was that a trailing dash in an option
group, when the dash is not permitted as an option letter, resulted
in the whole option group being returned as an argument, even though
the previous option in the group was already parsed as an option:
OPTS=abc ./getopt-test -a- -c arg ===>> OPT(a)ARG(-a-)ARG(-c)ARG(arg).
Instead, treat the dash as an invalid option and continue parsing
options: ===>> OPT(a)ERR(?-)OPT(c)ARG(arg).

The undesirable behaviour was that allowing the dash as an option
letter only allowed isolated dashes ("-") and trailing dashes in
groups ("-a-"), but neither middle dashes in groups ("-a-b"), even
though that already partially worked in 4.4BSD, nor leading dashes
in groups ("--a"), even though that works on all other BSDs and on
glibc. Also, while POSIX does not require that the dash can be
used as an option letter at all, arguably, it encourages that letters
either be fully supported or not supported at all. It is dubious
whether supporting an option letter in some positions but not in
others can be considered conforming.

This patch makes OpenBSD behaviour identical to FreeBSD and NetBSD,
improves compatibility with glibc (except that glibc does not support
isolated "-"), improves compatibility with DragonFly (except that
DragonFly is buggy when the dash option letter can take an optional
argument but that argument is not present), improves compatibility
with Illumos and Solaris 11 (except those do not support "-" and
mishandle "--a"), and restores 4.4BSD behaviour for "-a-b". In no
respect i'm aware of is compatibility with any other systems reduced.

For the full rationale, see my mail to tech@
on 30 Mar 2020 14:26:41 +0200.

Part of the problem was originally reported by an anonymous coward
on tech@ on 12 Mar 2020 03:40:24 +0200, additional analysis was
contributed by martijn@, and then the OP sent the final version of
the patch i'm now committing on 17 Mar 2020 19:17:56 +0200.

No licensing problem here because after the commit, the file does
not contain a single word written by the OP. Also, the OP told me
in private mail that he intends to publish the patch under the ISC
license already contained in the file and that he wishes to be known
by the pseudonym "0xef967c36".

OK martijn@, and no objection when shown on tech@,
but commit delayed to stay clear of the release.


Revision tags: OPENBSD_6_7_BASE
# 1.31 30-Mar-2020 martijn

"eventually" came and went back in 2004.

OK schwarze@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.30 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.29 01-Oct-2015 guenther

Eliminate the last of the LINTEDn and PRINTFLIKEn comments. In one
case, by deleting some useless '& of an array' we also eliminate the need
for the casts which prompted the original lint warnings

ok deraadt@


# 1.28 19-Sep-2015 guenther

Don't wrap initialized variables: binutils appears to be mishandling them
on arm and m88k

problems with optind observed by jsg@


# 1.27 12-Sep-2015 guenther

Wrap <getopt.h> to make the functions weak and make access to the initialized
variables go direct. (Common variables cannot be aliased.)


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.26 08-Jun-2013 millert

Fix parsing of ambiguous options, the whole loop must be processed.
From FreeBSD. OK miod@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.25 05-Mar-2011 guenther

Fix PR 6267: recheck POSIXLY_CORRECT each time getopt_long() starts a new
argv and don't suppress the handling of leading '-' in optstring when
POSIXLY_CORRECT is set.

Based on patch from Eric Blake.
ok and manpage update from millert@, manpage ok jmc@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.24 22-Jul-2010 blambert

getopt_long.c replaced getopt.c 6+ years ago; we can retire
the REPLACE_GETOPT macro, at long last

ok millert@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.23 31-Oct-2007 chl

Add parentheses to avoid warning:
"suggest parentheses around && within ||"

ok millert@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.22 04-Oct-2006 jmc

from wiz@netbsd:

Log Message:
Switch from 4-clause to 2-clause BSD license.
Ok dillo@, board@.


# 1.21 22-Sep-2006 millert

Back out change in rev 1.15 of to allow whitespace to separate a
flag from its optional argument if permutation is disabled. The
idea was that since "r:" would match "-r foo" then "r::" should
match "-r foo" too as long as we know that things have not been
reshuffled. Unfortunately, this causes incompatibilities with GNU
getopt_long when the POSIXLY_CORRECT environment variable is set.
OK deraadt@


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.20 25-Oct-2005 jmc

dont' -> don't in comments;
from ray lai;


Revision tags: OPENBSD_3_8_BASE
# 1.19 08-Aug-2005 espie

zap remaining rcsid.

Kill old files that are no longer compiled.

okay theo


# 1.18 30-Mar-2005 pat

ansi + de-register

ok otto deraadt


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.17 03-Jun-2004 millert

An optional argument cannot start with '-', even if permutation is
disabled. Magne Oestlyngen.


Revision tags: OPENBSD_3_5_BASE
# 1.16 04-Feb-2004 millert

Traditionally, getopt(3) has treated "--foo" the same as "--". However,
this can cause confusion when a user tries to use a long option with
a program that only supports short options. Furthermore, it appears
to be in violation of POSIX, which states that "--" shall indicate
the end of argument processing, not any string that begins with "--".
OK otto@ and closes PR 3666.


# 1.15 22-Sep-2003 millert

If permutation is disabled (as it is via getopt(3) or when POSIXLY_CORRECT
is set), we can accept an optional arg separated by whitespace. Previously,
the optional arg needed to be adjacent to the option character.
deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.14 17-Jun-2003 millert

branches: 1.14.2;
Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.13 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.12 02-Jun-2003 millert

Use the getopt() in getopt_long.c which supports some GNU extensions.
getopt.c is no longer compiled and will eventually be removed.


Revision tags: OPENBSD_3_3_BASE
# 1.11 10-Dec-2002 millert

When doing permutation, only treat "-" as an option if it was specified
in optstring. Problem noticed by Theo.


# 1.10 08-Dec-2002 millert

SUS (and apparently 1003.1-2001) say to check optstring for NULL


# 1.9 08-Dec-2002 millert

If we are passed "-" in argv and the user didn't specify '-' in optstring,
return -1 like POSIX requires.


# 1.8 08-Dec-2002 millert

BSD getopt() supports '-' in the optstring so we should too.
This is used by a few programs such as man and su.


# 1.7 07-Dec-2002 millert

Fix pasto in last commit.


# 1.6 07-Dec-2002 millert

For getopt_long_only() we *do* want to match single-character options
as shortcuts for long ones, but only if this would not conflict with
a short option in optstring. Now binutils gas works.


# 1.5 06-Dec-2002 millert

Fix two compatibility issues with our getopt_long_only() vs. the GNU version:
o Check for long options even when not at the beginning of an option.
For instance, if -a is a short option w/o an arg and -static is a
boolean long option then -astatic is valid for getopt_long_only().
o If a potential long argument does not match longopts and the first
character is not a short option, print a warning and skip the rest
of the argument.

Also clean up some trailing whitespace and change return value of
parse_long_options() from -2 to -1 when unmatched and in long_only mode.

With these fixes the binutils ld seems happy with our getopt_long_only()


# 1.4 05-Dec-2002 millert

Correctly handle -Wlong_arg (no space between -W and long_arg) when "W;"
is in optstring.


# 1.3 05-Dec-2002 millert

Reorganize this so that getopt_only_only() works correctly.
In order for getopt_only_only() to work we need to check for long
options before short ones. I have merged getopt_internal and
getopt_long_internal into a single function with the actual long
args parsing broken out into a separate function.
This also simplifies the flow of control.


# 1.2 03-Dec-2002 millert

Whoops, add missing #ifdef REPLACE_GETOPT


# 1.1 03-Dec-2002 millert

GNU-like getopt_long() from NetBSD with changes by me to support
getopt_long_only(). At some point this should replace the BSD
getopt(3) but we are not there yet.

While I am here add protection from the multiple getopt() definitions
due to conflicting standards.


# 1.31 30-Mar-2020 martijn

"eventually" came and went back in 2004.

OK schwarze@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.30 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.29 01-Oct-2015 guenther

Eliminate the last of the LINTEDn and PRINTFLIKEn comments. In one
case, by deleting some useless '& of an array' we also eliminate the need
for the casts which prompted the original lint warnings

ok deraadt@


# 1.28 19-Sep-2015 guenther

Don't wrap initialized variables: binutils appears to be mishandling them
on arm and m88k

problems with optind observed by jsg@


# 1.27 12-Sep-2015 guenther

Wrap <getopt.h> to make the functions weak and make access to the initialized
variables go direct. (Common variables cannot be aliased.)


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.26 08-Jun-2013 millert

Fix parsing of ambiguous options, the whole loop must be processed.
From FreeBSD. OK miod@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.25 05-Mar-2011 guenther

Fix PR 6267: recheck POSIXLY_CORRECT each time getopt_long() starts a new
argv and don't suppress the handling of leading '-' in optstring when
POSIXLY_CORRECT is set.

Based on patch from Eric Blake.
ok and manpage update from millert@, manpage ok jmc@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.24 22-Jul-2010 blambert

getopt_long.c replaced getopt.c 6+ years ago; we can retire
the REPLACE_GETOPT macro, at long last

ok millert@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.23 31-Oct-2007 chl

Add parentheses to avoid warning:
"suggest parentheses around && within ||"

ok millert@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.22 04-Oct-2006 jmc

from wiz@netbsd:

Log Message:
Switch from 4-clause to 2-clause BSD license.
Ok dillo@, board@.


# 1.21 22-Sep-2006 millert

Back out change in rev 1.15 of to allow whitespace to separate a
flag from its optional argument if permutation is disabled. The
idea was that since "r:" would match "-r foo" then "r::" should
match "-r foo" too as long as we know that things have not been
reshuffled. Unfortunately, this causes incompatibilities with GNU
getopt_long when the POSIXLY_CORRECT environment variable is set.
OK deraadt@


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.20 25-Oct-2005 jmc

dont' -> don't in comments;
from ray lai;


Revision tags: OPENBSD_3_8_BASE
# 1.19 08-Aug-2005 espie

zap remaining rcsid.

Kill old files that are no longer compiled.

okay theo


# 1.18 30-Mar-2005 pat

ansi + de-register

ok otto deraadt


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.17 03-Jun-2004 millert

An optional argument cannot start with '-', even if permutation is
disabled. Magne Oestlyngen.


Revision tags: OPENBSD_3_5_BASE
# 1.16 04-Feb-2004 millert

Traditionally, getopt(3) has treated "--foo" the same as "--". However,
this can cause confusion when a user tries to use a long option with
a program that only supports short options. Furthermore, it appears
to be in violation of POSIX, which states that "--" shall indicate
the end of argument processing, not any string that begins with "--".
OK otto@ and closes PR 3666.


# 1.15 22-Sep-2003 millert

If permutation is disabled (as it is via getopt(3) or when POSIXLY_CORRECT
is set), we can accept an optional arg separated by whitespace. Previously,
the optional arg needed to be adjacent to the option character.
deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.14 17-Jun-2003 millert

branches: 1.14.2;
Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.13 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.12 02-Jun-2003 millert

Use the getopt() in getopt_long.c which supports some GNU extensions.
getopt.c is no longer compiled and will eventually be removed.


Revision tags: OPENBSD_3_3_BASE
# 1.11 10-Dec-2002 millert

When doing permutation, only treat "-" as an option if it was specified
in optstring. Problem noticed by Theo.


# 1.10 08-Dec-2002 millert

SUS (and apparently 1003.1-2001) say to check optstring for NULL


# 1.9 08-Dec-2002 millert

If we are passed "-" in argv and the user didn't specify '-' in optstring,
return -1 like POSIX requires.


# 1.8 08-Dec-2002 millert

BSD getopt() supports '-' in the optstring so we should too.
This is used by a few programs such as man and su.


# 1.7 07-Dec-2002 millert

Fix pasto in last commit.


# 1.6 07-Dec-2002 millert

For getopt_long_only() we *do* want to match single-character options
as shortcuts for long ones, but only if this would not conflict with
a short option in optstring. Now binutils gas works.


# 1.5 06-Dec-2002 millert

Fix two compatibility issues with our getopt_long_only() vs. the GNU version:
o Check for long options even when not at the beginning of an option.
For instance, if -a is a short option w/o an arg and -static is a
boolean long option then -astatic is valid for getopt_long_only().
o If a potential long argument does not match longopts and the first
character is not a short option, print a warning and skip the rest
of the argument.

Also clean up some trailing whitespace and change return value of
parse_long_options() from -2 to -1 when unmatched and in long_only mode.

With these fixes the binutils ld seems happy with our getopt_long_only()


# 1.4 05-Dec-2002 millert

Correctly handle -Wlong_arg (no space between -W and long_arg) when "W;"
is in optstring.


# 1.3 05-Dec-2002 millert

Reorganize this so that getopt_only_only() works correctly.
In order for getopt_only_only() to work we need to check for long
options before short ones. I have merged getopt_internal and
getopt_long_internal into a single function with the actual long
args parsing broken out into a separate function.
This also simplifies the flow of control.


# 1.2 03-Dec-2002 millert

Whoops, add missing #ifdef REPLACE_GETOPT


# 1.1 03-Dec-2002 millert

GNU-like getopt_long() from NetBSD with changes by me to support
getopt_long_only(). At some point this should replace the BSD
getopt(3) but we are not there yet.

While I am here add protection from the multiple getopt() definitions
due to conflicting standards.


# 1.30 25-Jan-2019 millert

I am retiring my old email address; replace it with my OpenBSD one.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.29 01-Oct-2015 guenther

Eliminate the last of the LINTEDn and PRINTFLIKEn comments. In one
case, by deleting some useless '& of an array' we also eliminate the need
for the casts which prompted the original lint warnings

ok deraadt@


# 1.28 19-Sep-2015 guenther

Don't wrap initialized variables: binutils appears to be mishandling them
on arm and m88k

problems with optind observed by jsg@


# 1.27 12-Sep-2015 guenther

Wrap <getopt.h> to make the functions weak and make access to the initialized
variables go direct. (Common variables cannot be aliased.)


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.26 08-Jun-2013 millert

Fix parsing of ambiguous options, the whole loop must be processed.
From FreeBSD. OK miod@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.25 05-Mar-2011 guenther

Fix PR 6267: recheck POSIXLY_CORRECT each time getopt_long() starts a new
argv and don't suppress the handling of leading '-' in optstring when
POSIXLY_CORRECT is set.

Based on patch from Eric Blake.
ok and manpage update from millert@, manpage ok jmc@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.24 22-Jul-2010 blambert

getopt_long.c replaced getopt.c 6+ years ago; we can retire
the REPLACE_GETOPT macro, at long last

ok millert@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.23 31-Oct-2007 chl

Add parentheses to avoid warning:
"suggest parentheses around && within ||"

ok millert@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.22 04-Oct-2006 jmc

from wiz@netbsd:

Log Message:
Switch from 4-clause to 2-clause BSD license.
Ok dillo@, board@.


# 1.21 22-Sep-2006 millert

Back out change in rev 1.15 of to allow whitespace to separate a
flag from its optional argument if permutation is disabled. The
idea was that since "r:" would match "-r foo" then "r::" should
match "-r foo" too as long as we know that things have not been
reshuffled. Unfortunately, this causes incompatibilities with GNU
getopt_long when the POSIXLY_CORRECT environment variable is set.
OK deraadt@


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.20 25-Oct-2005 jmc

dont' -> don't in comments;
from ray lai;


Revision tags: OPENBSD_3_8_BASE
# 1.19 08-Aug-2005 espie

zap remaining rcsid.

Kill old files that are no longer compiled.

okay theo


# 1.18 30-Mar-2005 pat

ansi + de-register

ok otto deraadt


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.17 03-Jun-2004 millert

An optional argument cannot start with '-', even if permutation is
disabled. Magne Oestlyngen.


Revision tags: OPENBSD_3_5_BASE
# 1.16 04-Feb-2004 millert

Traditionally, getopt(3) has treated "--foo" the same as "--". However,
this can cause confusion when a user tries to use a long option with
a program that only supports short options. Furthermore, it appears
to be in violation of POSIX, which states that "--" shall indicate
the end of argument processing, not any string that begins with "--".
OK otto@ and closes PR 3666.


# 1.15 22-Sep-2003 millert

If permutation is disabled (as it is via getopt(3) or when POSIXLY_CORRECT
is set), we can accept an optional arg separated by whitespace. Previously,
the optional arg needed to be adjacent to the option character.
deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.14 17-Jun-2003 millert

branches: 1.14.2;
Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.13 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.12 02-Jun-2003 millert

Use the getopt() in getopt_long.c which supports some GNU extensions.
getopt.c is no longer compiled and will eventually be removed.


Revision tags: OPENBSD_3_3_BASE
# 1.11 10-Dec-2002 millert

When doing permutation, only treat "-" as an option if it was specified
in optstring. Problem noticed by Theo.


# 1.10 08-Dec-2002 millert

SUS (and apparently 1003.1-2001) say to check optstring for NULL


# 1.9 08-Dec-2002 millert

If we are passed "-" in argv and the user didn't specify '-' in optstring,
return -1 like POSIX requires.


# 1.8 08-Dec-2002 millert

BSD getopt() supports '-' in the optstring so we should too.
This is used by a few programs such as man and su.


# 1.7 07-Dec-2002 millert

Fix pasto in last commit.


# 1.6 07-Dec-2002 millert

For getopt_long_only() we *do* want to match single-character options
as shortcuts for long ones, but only if this would not conflict with
a short option in optstring. Now binutils gas works.


# 1.5 06-Dec-2002 millert

Fix two compatibility issues with our getopt_long_only() vs. the GNU version:
o Check for long options even when not at the beginning of an option.
For instance, if -a is a short option w/o an arg and -static is a
boolean long option then -astatic is valid for getopt_long_only().
o If a potential long argument does not match longopts and the first
character is not a short option, print a warning and skip the rest
of the argument.

Also clean up some trailing whitespace and change return value of
parse_long_options() from -2 to -1 when unmatched and in long_only mode.

With these fixes the binutils ld seems happy with our getopt_long_only()


# 1.4 05-Dec-2002 millert

Correctly handle -Wlong_arg (no space between -W and long_arg) when "W;"
is in optstring.


# 1.3 05-Dec-2002 millert

Reorganize this so that getopt_only_only() works correctly.
In order for getopt_only_only() to work we need to check for long
options before short ones. I have merged getopt_internal and
getopt_long_internal into a single function with the actual long
args parsing broken out into a separate function.
This also simplifies the flow of control.


# 1.2 03-Dec-2002 millert

Whoops, add missing #ifdef REPLACE_GETOPT


# 1.1 03-Dec-2002 millert

GNU-like getopt_long() from NetBSD with changes by me to support
getopt_long_only(). At some point this should replace the BSD
getopt(3) but we are not there yet.

While I am here add protection from the multiple getopt() definitions
due to conflicting standards.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE
# 1.29 01-Oct-2015 guenther

Eliminate the last of the LINTEDn and PRINTFLIKEn comments. In one
case, by deleting some useless '& of an array' we also eliminate the need
for the casts which prompted the original lint warnings

ok deraadt@


# 1.28 19-Sep-2015 guenther

Don't wrap initialized variables: binutils appears to be mishandling them
on arm and m88k

problems with optind observed by jsg@


# 1.27 12-Sep-2015 guenther

Wrap <getopt.h> to make the functions weak and make access to the initialized
variables go direct. (Common variables cannot be aliased.)


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.26 08-Jun-2013 millert

Fix parsing of ambiguous options, the whole loop must be processed.
From FreeBSD. OK miod@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.25 05-Mar-2011 guenther

Fix PR 6267: recheck POSIXLY_CORRECT each time getopt_long() starts a new
argv and don't suppress the handling of leading '-' in optstring when
POSIXLY_CORRECT is set.

Based on patch from Eric Blake.
ok and manpage update from millert@, manpage ok jmc@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.24 22-Jul-2010 blambert

getopt_long.c replaced getopt.c 6+ years ago; we can retire
the REPLACE_GETOPT macro, at long last

ok millert@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.23 31-Oct-2007 chl

Add parentheses to avoid warning:
"suggest parentheses around && within ||"

ok millert@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.22 04-Oct-2006 jmc

from wiz@netbsd:

Log Message:
Switch from 4-clause to 2-clause BSD license.
Ok dillo@, board@.


# 1.21 22-Sep-2006 millert

Back out change in rev 1.15 of to allow whitespace to separate a
flag from its optional argument if permutation is disabled. The
idea was that since "r:" would match "-r foo" then "r::" should
match "-r foo" too as long as we know that things have not been
reshuffled. Unfortunately, this causes incompatibilities with GNU
getopt_long when the POSIXLY_CORRECT environment variable is set.
OK deraadt@


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.20 25-Oct-2005 jmc

dont' -> don't in comments;
from ray lai;


Revision tags: OPENBSD_3_8_BASE
# 1.19 08-Aug-2005 espie

zap remaining rcsid.

Kill old files that are no longer compiled.

okay theo


# 1.18 30-Mar-2005 pat

ansi + de-register

ok otto deraadt


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.17 03-Jun-2004 millert

An optional argument cannot start with '-', even if permutation is
disabled. Magne Oestlyngen.


Revision tags: OPENBSD_3_5_BASE
# 1.16 04-Feb-2004 millert

Traditionally, getopt(3) has treated "--foo" the same as "--". However,
this can cause confusion when a user tries to use a long option with
a program that only supports short options. Furthermore, it appears
to be in violation of POSIX, which states that "--" shall indicate
the end of argument processing, not any string that begins with "--".
OK otto@ and closes PR 3666.


# 1.15 22-Sep-2003 millert

If permutation is disabled (as it is via getopt(3) or when POSIXLY_CORRECT
is set), we can accept an optional arg separated by whitespace. Previously,
the optional arg needed to be adjacent to the option character.
deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.14 17-Jun-2003 millert

branches: 1.14.2;
Sync with share/misc/license.template and add missing DARPA credit
where applicable.


# 1.13 03-Jun-2003 millert

Use an ISC-tyle license for all my code; it is simpler and more permissive.


# 1.12 02-Jun-2003 millert

Use the getopt() in getopt_long.c which supports some GNU extensions.
getopt.c is no longer compiled and will eventually be removed.


Revision tags: OPENBSD_3_3_BASE
# 1.11 10-Dec-2002 millert

When doing permutation, only treat "-" as an option if it was specified
in optstring. Problem noticed by Theo.


# 1.10 08-Dec-2002 millert

SUS (and apparently 1003.1-2001) say to check optstring for NULL


# 1.9 08-Dec-2002 millert

If we are passed "-" in argv and the user didn't specify '-' in optstring,
return -1 like POSIX requires.


# 1.8 08-Dec-2002 millert

BSD getopt() supports '-' in the optstring so we should too.
This is used by a few programs such as man and su.


# 1.7 07-Dec-2002 millert

Fix pasto in last commit.


# 1.6 07-Dec-2002 millert

For getopt_long_only() we *do* want to match single-character options
as shortcuts for long ones, but only if this would not conflict with
a short option in optstring. Now binutils gas works.


# 1.5 06-Dec-2002 millert

Fix two compatibility issues with our getopt_long_only() vs. the GNU version:
o Check for long options even when not at the beginning of an option.
For instance, if -a is a short option w/o an arg and -static is a
boolean long option then -astatic is valid for getopt_long_only().
o If a potential long argument does not match longopts and the first
character is not a short option, print a warning and skip the rest
of the argument.

Also clean up some trailing whitespace and change return value of
parse_long_options() from -2 to -1 when unmatched and in long_only mode.

With these fixes the binutils ld seems happy with our getopt_long_only()


# 1.4 05-Dec-2002 millert

Correctly handle -Wlong_arg (no space between -W and long_arg) when "W;"
is in optstring.


# 1.3 05-Dec-2002 millert

Reorganize this so that getopt_only_only() works correctly.
In order for getopt_only_only() to work we need to check for long
options before short ones. I have merged getopt_internal and
getopt_long_internal into a single function with the actual long
args parsing broken out into a separate function.
This also simplifies the flow of control.


# 1.2 03-Dec-2002 millert

Whoops, add missing #ifdef REPLACE_GETOPT


# 1.1 03-Dec-2002 millert

GNU-like getopt_long() from NetBSD with changes by me to support
getopt_long_only(). At some point this should replace the BSD
getopt(3) but we are not there yet.

While I am here add protection from the multiple getopt() definitions
due to conflicting standards.