summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorguenther <>2011-03-05 22:10:11 +0000
committerguenther <>2011-03-05 22:10:11 +0000
commit9d057237f371a465ccec3f1a7a68c19af8920051 (patch)
tree448ef6b71a537dcd5d0e6ce2a7893a402dc7c930 /src
parent40abb7da20d82cdfbe24fc623f51d100e7ae76d4 (diff)
downloadopenbsd-9d057237f371a465ccec3f1a7a68c19af8920051.tar.gz
openbsd-9d057237f371a465ccec3f1a7a68c19af8920051.tar.bz2
openbsd-9d057237f371a465ccec3f1a7a68c19af8920051.zip
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@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/getopt.313
-rw-r--r--src/lib/libc/stdlib/getopt_long.323
-rw-r--r--src/lib/libc/stdlib/getopt_long.c24
3 files changed, 16 insertions, 44 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index 82f74c2c1a..ecdf42ab76 100644
--- a/src/lib/libc/stdlib/getopt.3
+++ b/src/lib/libc/stdlib/getopt.3
@@ -25,9 +25,9 @@
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE. 26.\" SUCH DAMAGE.
27.\" 27.\"
28.\" $OpenBSD: getopt.3,v 1.41 2009/04/12 23:13:36 okan Exp $ 28.\" $OpenBSD: getopt.3,v 1.42 2011/03/05 22:10:11 guenther Exp $
29.\" 29.\"
30.Dd $Mdocdate: April 12 2009 $ 30.Dd $Mdocdate: March 5 2011 $
31.Dt GETOPT 3 31.Dt GETOPT 3
32.Os 32.Os
33.Sh NAME 33.Sh NAME
@@ -158,15 +158,6 @@ is set to the character that caused the error.
158The 158The
159.Fn getopt 159.Fn getopt
160function returns \-1 when the argument list is exhausted. 160function returns \-1 when the argument list is exhausted.
161.Sh ENVIRONMENT
162.Bl -tag -width POSIXLY_CORRECTXX
163.It Ev POSIXLY_CORRECT
164If set, a leading
165.Sq -
166in
167.Ar optstring
168is ignored.
169.El
170.Sh EXAMPLES 161.Sh EXAMPLES
171The following code accepts the options 162The following code accepts the options
172.Fl b 163.Fl b
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3
index c2b0db6452..fe3a54bbcd 100644
--- a/src/lib/libc/stdlib/getopt_long.3
+++ b/src/lib/libc/stdlib/getopt_long.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: getopt_long.3,v 1.17 2011/01/24 01:50:25 schwarze Exp $ 1.\" $OpenBSD: getopt_long.3,v 1.18 2011/03/05 22:10:11 guenther Exp $
2.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ 2.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $
3.\" 3.\"
4.\" Copyright (c) 1988, 1991, 1993 4.\" Copyright (c) 1988, 1991, 1993
@@ -30,7 +30,7 @@
30.\" 30.\"
31.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 31.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
32.\" 32.\"
33.Dd $Mdocdate: January 24 2011 $ 33.Dd $Mdocdate: March 5 2011 $
34.Dt GETOPT_LONG 3 34.Dt GETOPT_LONG 3
35.Os 35.Os
36.Sh NAME 36.Sh NAME
@@ -211,23 +211,6 @@ found in glibc-2.1.3:
211.It 211.It
212handling of 212handling of
213.Ql - 213.Ql -
214as the first character of the option string in the presence of the
215environment variable
216.Ev POSIXLY_CORRECT :
217.Bl -tag -width "OpenBSD"
218.It GNU
219ignores
220.Ev POSIXLY_CORRECT
221and returns non-options as arguments to option
222.Ql \e1 .
223.It OpenBSD
224honors
225.Ev POSIXLY_CORRECT
226and stops at the first non-option.
227.El
228.It
229handling of
230.Ql -
231within the option string (not the first character): 214within the option string (not the first character):
232.Bl -tag -width "OpenBSD" 215.Bl -tag -width "OpenBSD"
233.It GNU 216.It GNU
@@ -402,8 +385,6 @@ relative to current positions) are the same, though.
402.It Ev POSIXLY_CORRECT 385.It Ev POSIXLY_CORRECT
403If set, option processing stops when the first non-option is found and 386If set, option processing stops when the first non-option is found and
404a leading 387a leading
405.Sq -
406or
407.Sq + 388.Sq +
408in the 389in the
409.Ar optstring 390.Ar optstring
diff --git a/src/lib/libc/stdlib/getopt_long.c b/src/lib/libc/stdlib/getopt_long.c
index eb1e3ef4be..e149fe0ace 100644
--- a/src/lib/libc/stdlib/getopt_long.c
+++ b/src/lib/libc/stdlib/getopt_long.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getopt_long.c,v 1.24 2010/07/22 19:31:53 blambert Exp $ */ 1/* $OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $ */
2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ 2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3 3
4/* 4/*
@@ -285,25 +285,25 @@ getopt_internal(int nargc, char * const *nargv, const char *options,
285 return (-1); 285 return (-1);
286 286
287 /* 287 /*
288 * XXX Some GNU programs (like cvs) set optind to 0 instead of
289 * XXX using optreset. Work around this braindamage.
290 */
291 if (optind == 0)
292 optind = optreset = 1;
293
294 /*
288 * Disable GNU extensions if POSIXLY_CORRECT is set or options 295 * Disable GNU extensions if POSIXLY_CORRECT is set or options
289 * string begins with a '+'. 296 * string begins with a '+'.
290 */ 297 */
291 if (posixly_correct == -1) 298 if (posixly_correct == -1 || optreset)
292 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 299 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
293 if (posixly_correct || *options == '+') 300 if (*options == '-')
294 flags &= ~FLAG_PERMUTE;
295 else if (*options == '-')
296 flags |= FLAG_ALLARGS; 301 flags |= FLAG_ALLARGS;
302 else if (posixly_correct || *options == '+')
303 flags &= ~FLAG_PERMUTE;
297 if (*options == '+' || *options == '-') 304 if (*options == '+' || *options == '-')
298 options++; 305 options++;
299 306
300 /*
301 * XXX Some GNU programs (like cvs) set optind to 0 instead of
302 * XXX using optreset. Work around this braindamage.
303 */
304 if (optind == 0)
305 optind = optreset = 1;
306
307 optarg = NULL; 307 optarg = NULL;
308 if (optreset) 308 if (optreset)
309 nonopt_start = nonopt_end = -1; 309 nonopt_start = nonopt_end = -1;