summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorhoek <hoek@FreeBSD.org>2000-01-06 01:25:15 +0000
committerhoek <hoek@FreeBSD.org>2000-01-06 01:25:15 +0000
commit25fa278a589e06945a72a70e7823ccf87e819750 (patch)
tree8c44f9b9d310313cd2514f1d9011ccd3fbcd7ee2 /lib
parent256d6ab5abe585ec4bca4932aae06f494c5ba20a (diff)
downloadFreeBSD-src-25fa278a589e06945a72a70e7823ccf87e819750.zip
FreeBSD-src-25fa278a589e06945a72a70e7823ccf87e819750.tar.gz
Make example for handling "-##" work and comply with style(9). Still
doesn't handle nastier corner cases such as "-j3 -33" correctly. <shrug> PR: docs/12994 (James Howard <howardjp@wam.umd.edu>)
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdlib/getopt.317
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3
index 51f7ffb..7017c4f 100644
--- a/lib/libc/stdlib/getopt.3
+++ b/lib/libc/stdlib/getopt.3
@@ -243,10 +243,10 @@ as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
.Em only .
-The following code fragment works in most cases.
+The following code fragment works in most (but not all) cases.
.Bd -literal -offset indent
int length;
-char *p;
+char *p, *ep;
while ((ch = getopt(argc, argv, "0123456789")) != -1)
switch (ch) {
@@ -254,9 +254,16 @@ while ((ch = getopt(argc, argv, "0123456789")) != -1)
case '5': case '6': case '7': case '8': case '9':
p = argv[optind - 1];
if (p[0] == '-' && p[1] == ch && !p[2])
- length = atoi(++p);
- else
- length = atoi(argv[optind] + 1);
+ length = strtol(++p, &ep, 10);
+ else if (argv[optind] && argv[optind][1] == ch) {
+ length = strtol((p = argv[optind] + 1),
+ &ep, 10);
+ optind++;
+ optreset = 1;
+ } else
+ usage();
+ if (*ep != '\0')
+ errx(EX_USAGE, "illegal number -- %s", p);
break;
}
.Ed
OpenPOWER on IntegriCloud