diff options
author | millert <> | 2005-07-01 02:11:55 +0000 |
---|---|---|
committer | millert <> | 2005-07-01 02:11:55 +0000 |
commit | 9e09735358675e8697c29770817711e42f07441b (patch) | |
tree | 67582c2711d55ae8d04b1a36af983e1930b4dd12 /src | |
parent | dd611c3aaf84a5329ce7642bf97e61f4fa7f2d65 (diff) | |
download | openbsd-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.3 | 34 |
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. | |||
351 | This practice is wrong, and should not be used in any current development. | 351 | This practice is wrong, and should not be used in any current development. |
352 | It is provided for backward compatibility | 352 | It is provided for backward compatibility |
353 | .Em only . | 353 | .Em only . |
354 | The following code fragment works in most cases. | 354 | The following code fragment works in most cases and can handle mixed |
355 | number and letter arguments. | ||
356 | |||
355 | .Bd -literal -offset indent | 357 | .Bd -literal -offset indent |
356 | int ch; | 358 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; |
357 | long length; | 359 | int length = -1, newarg = 1, prevoptind = 1; |
358 | char *p; | ||
359 | 360 | ||
360 | while ((ch = getopt(argc, argv, "0123456789")) != -1) { | 361 | while ((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 |