summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormillert <>2005-07-01 02:11:55 +0000
committermillert <>2005-07-01 02:11:55 +0000
commit9e09735358675e8697c29770817711e42f07441b (patch)
tree67582c2711d55ae8d04b1a36af983e1930b4dd12 /src
parentdd611c3aaf84a5329ce7642bf97e61f4fa7f2d65 (diff)
downloadopenbsd-9e09735358675e8697c29770817711e42f07441b.tar.gz
openbsd-9e09735358675e8697c29770817711e42f07441b.tar.bz2
openbsd-9e09735358675e8697c29770817711e42f07441b.zip
More robust example of numeric argument handling. The old example
code would dereference NULL for mixed letter and number args. OK deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/getopt.334
1 files changed, 23 insertions, 11 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index e0dc3701f9..52c902d6a5 100644
--- a/src/lib/libc/stdlib/getopt.3
+++ b/src/lib/libc/stdlib/getopt.3
@@ -25,7 +25,7 @@
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.31 2005/03/26 22:02:15 millert Exp $ 28.\" $OpenBSD: getopt.3,v 1.32 2005/07/01 02:11:55 millert Exp $
29.\" 29.\"
30.Dd December 17, 2002 30.Dd December 17, 2002
31.Dt GETOPT 3 31.Dt GETOPT 3
@@ -351,22 +351,34 @@ as an option.
351This practice is wrong, and should not be used in any current development. 351This practice is wrong, and should not be used in any current development.
352It is provided for backward compatibility 352It is provided for backward compatibility
353.Em only . 353.Em only .
354The following code fragment works in most cases. 354The following code fragment works in most cases and can handle mixed
355number and letter arguments.
356
355.Bd -literal -offset indent 357.Bd -literal -offset indent
356int ch; 358int aflag = 0, bflag = 0, ch, lastch = '\e0';
357long length; 359int length = -1, newarg = 1, prevoptind = 1;
358char *p;
359 360
360while ((ch = getopt(argc, argv, "0123456789")) != -1) { 361while ((ch = getopt(argc, argv, "0123456789ab")) != -1) {
361 switch (ch) { 362 switch (ch) {
362 case '0': case '1': case '2': case '3': case '4': 363 case '0': case '1': case '2': case '3': case '4':
363 case '5': case '6': case '7': case '8': case '9': 364 case '5': case '6': case '7': case '8': case '9':
364 p = argv[optind - 1]; 365 if (newarg || !isdigit(lastch))
365 if (p[0] == '-' && p[1] == ch && !p[2]) 366 length = 0;
366 length = ch - '0'; 367 else if (length > INT_MAX / 10)
367 else 368 usage();
368 length = strtol(argv[optind] + 1, NULL, 10); 369 length = (length * 10) + (ch - '0');
370 break;
371 case 'a':
372 aflag = 1;
369 break; 373 break;
374 case 'b':
375 bflag = 1;
376 break;
377 default:
378 usage();
370 } 379 }
380 lastch = ch;
381 newarg = optind != prevoptind;
382 prevoptind = optind;
371} 383}
372.Ed 384.Ed