diff options
author | miod <> | 2002-02-23 19:50:01 +0000 |
---|---|---|
committer | miod <> | 2002-02-23 19:50:01 +0000 |
commit | c3abb344d7a4b377a5ea92a04bacb558cba39b56 (patch) | |
tree | d591f6fa298a734eed6caef5434147bfea574dd1 | |
parent | c9b5634be1ddde16d746c406d532006235cae9db (diff) | |
download | openbsd-c3abb344d7a4b377a5ea92a04bacb558cba39b56.tar.gz openbsd-c3abb344d7a4b377a5ea92a04bacb558cba39b56.tar.bz2 openbsd-c3abb344d7a4b377a5ea92a04bacb558cba39b56.zip |
Add a caveat section pointing out that people affecting the return value
of getopt() to char variables instead of int lose on arches where char is
unsigned by default.
Clean the example by not pasting parts of <unistd.h> into it, and by not
using atoi(3).
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index 878290f542..4f6fe9749b 100644 --- a/src/lib/libc/stdlib/getopt.3 +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -29,7 +29,7 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
31 | .\" | 31 | .\" |
32 | .\" $OpenBSD: getopt.3,v 1.14 2000/12/15 14:15:27 aaron Exp $ | 32 | .\" $OpenBSD: getopt.3,v 1.15 2002/02/23 19:50:01 miod Exp $ |
33 | .\" | 33 | .\" |
34 | .Dd April 19, 1994 | 34 | .Dd April 19, 1994 |
35 | .Dt GETOPT 3 | 35 | .Dt GETOPT 3 |
@@ -128,8 +128,6 @@ argument), | |||
128 | returns \-1. | 128 | returns \-1. |
129 | .Sh EXAMPLES | 129 | .Sh EXAMPLES |
130 | .Bd -literal -compact | 130 | .Bd -literal -compact |
131 | extern char *optarg; | ||
132 | extern int optind; | ||
133 | int bflag, ch, fd; | 131 | int bflag, ch, fd; |
134 | 132 | ||
135 | bflag = 0; | 133 | bflag = 0; |
@@ -194,6 +192,16 @@ The | |||
194 | .Fn getopt | 192 | .Fn getopt |
195 | function appeared in | 193 | function appeared in |
196 | .Bx 4.3 . | 194 | .Bx 4.3 . |
195 | .Sh CAVEATS | ||
196 | Some software use the very bad practice of affecting the return value of | ||
197 | .Fn getopt | ||
198 | to a | ||
199 | .Ft char | ||
200 | variable. | ||
201 | On platforms where | ||
202 | .Ft char | ||
203 | is unsigned by default, a comparison of this variable to \-1 to detect the | ||
204 | end of the argument list will never succeed. | ||
197 | .Sh BUGS | 205 | .Sh BUGS |
198 | The | 206 | The |
199 | .Fn getopt | 207 | .Fn getopt |
@@ -247,9 +255,9 @@ while ((c = getopt(argc, argv, "0123456789")) != -1) { | |||
247 | case '5': case '6': case '7': case '8': case '9': | 255 | case '5': case '6': case '7': case '8': case '9': |
248 | p = argv[optind - 1]; | 256 | p = argv[optind - 1]; |
249 | if (p[0] == '-' && p[1] == ch && !p[2]) | 257 | if (p[0] == '-' && p[1] == ch && !p[2]) |
250 | length = atoi(++p); | 258 | length = ch - '0'; |
251 | else | 259 | else |
252 | length = atoi(argv[optind] + 1); | 260 | length = strtol(argv[optind] + 1, NULL, 10); |
253 | break; | 261 | break; |
254 | } | 262 | } |
255 | } | 263 | } |