Browse Source

Do not claim that empty numbers set EINVAL, our implementation doesn't.

Mention that invalid bases do set EINVAL (as required by POSIX);
this part of the change uses part of an earlier patch by millert@.
Minor mdoc(7) cleanup and sync between the two pages while here.
Feedback and ok jmc@ and millert@.
OPENBSD_5_7
schwarze 10 years ago
parent
commit
947c617f02
2 changed files with 61 additions and 37 deletions
  1. +30
    -21
      src/lib/libc/stdlib/strtol.3
  2. +31
    -16
      src/lib/libc/stdlib/strtoul.3

+ 30
- 21
src/lib/libc/stdlib/strtol.3 View File

@ -29,9 +29,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: strtol.3,v 1.24 2013/08/14 06:32:28 jmc Exp $
.\" $OpenBSD: strtol.3,v 1.25 2014/09/14 14:32:44 schwarze Exp $
.\" .\"
.Dd $Mdocdate: August 14 2013 $
.Dd $Mdocdate: September 14 2014 $
.Dt STRTOL 3 .Dt STRTOL 3
.Os .Os
.Sh NAME .Sh NAME
@ -45,14 +45,11 @@
.In stdlib.h .In stdlib.h
.Ft long .Ft long
.Fn strtol "const char *nptr" "char **endptr" "int base" .Fn strtol "const char *nptr" "char **endptr" "int base"
.Pp
.Ft long long .Ft long long
.Fn strtoll "const char *nptr" "char **endptr" "int base" .Fn strtoll "const char *nptr" "char **endptr" "int base"
.Pp
.In inttypes.h .In inttypes.h
.Ft intmax_t .Ft intmax_t
.Fn strtoimax "const char *nptr" "char **endptr" "int base" .Fn strtoimax "const char *nptr" "char **endptr" "int base"
.Pp
.In sys/types.h .In sys/types.h
.In limits.h .In limits.h
.In stdlib.h .In stdlib.h
@ -64,21 +61,21 @@ The
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to a to a
.Li long
.Vt long
value. value.
The The
.Fn strtoll .Fn strtoll
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to a to a
.Li long long
.Vt long long
value. value.
The The
.Fn strtoimax .Fn strtoimax
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to an to an
.Li intmax_t
.Vt intmax_t
value. value.
The The
.Fn strtoq .Fn strtoq
@ -108,10 +105,10 @@ is taken as 10 (decimal) unless the next character is
in which case it is taken as 8 (octal). in which case it is taken as 8 (octal).
.Pp .Pp
The remainder of the string is converted to a The remainder of the string is converted to a
.Li long ,
.Li long long ,
.Vt long ,
.Vt long long ,
or or
.Li intmax_t ,
.Vt intmax_t
value in the obvious manner, value in the obvious manner,
stopping at the first character which is not a valid digit stopping at the first character which is not a valid digit
in the given base. in the given base.
@ -151,14 +148,7 @@ The
.Fn strtoimax , .Fn strtoimax ,
and and
.Fn strtoq .Fn strtoq
functions return the result of the conversion,
unless the value would underflow or overflow.
If no conversion could be performed, 0 is returned;
the global variable
.Va errno
is also set to
.Er EINVAL ,
though this is not portable across all platforms.
functions return the result of the conversion.
If overflow or underflow occurs, If overflow or underflow occurs,
.Va errno .Va errno
is set to is set to
@ -171,6 +161,14 @@ and the function return value is as follows:
.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX .It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX .It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
.El .El
.Pp
If there is no valid digit, 0 is returned.
If
.Ar base
is invalid, 0 is returned and the global variable
.Va errno
is set to
.Er EINVAL .
.Sh EXAMPLES .Sh EXAMPLES
Ensuring that a string is a valid number (i.e., in range and containing no Ensuring that a string is a valid number (i.e., in range and containing no
trailing characters) requires clearing trailing characters) requires clearing
@ -212,9 +210,9 @@ If
is being used instead of is being used instead of
.Xr atoi 3 , .Xr atoi 3 ,
error checking is further complicated because the desired return value is an error checking is further complicated because the desired return value is an
.Li int
.Vt int
rather than a rather than a
.Li long ;
.Vt long ;
however, on some architectures integers and long integers are the same size. however, on some architectures integers and long integers are the same size.
Thus the following is necessary: Thus the following is necessary:
.Bd -literal -offset indent .Bd -literal -offset indent
@ -235,6 +233,10 @@ ival = lval;
.Ed .Ed
.Sh ERRORS .Sh ERRORS
.Bl -tag -width Er .Bl -tag -width Er
.It Bq Er EINVAL
The value of
.Ar base
was neither between 2 and 36 inclusive nor the special value 0.
.It Bq Er ERANGE .It Bq Er ERANGE
The given string was out of range; the value converted has been clamped. The given string was out of range; the value converted has been clamped.
.El .El
@ -255,6 +257,13 @@ and
.Fn strtoimax .Fn strtoimax
functions conform to functions conform to
.St -ansiC-99 . .St -ansiC-99 .
Setting
.Va errno
to
.Dv EINVAL
is an extension to that standard required by
.St -p1003.1-2008 .
.Pp
The The
.Fn strtoq .Fn strtoq
function is a function is a


+ 31
- 16
src/lib/libc/stdlib/strtoul.3 View File

@ -29,9 +29,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: strtoul.3,v 1.22 2013/08/14 06:32:28 jmc Exp $
.\" $OpenBSD: strtoul.3,v 1.23 2014/09/14 14:32:44 schwarze Exp $
.\" .\"
.Dd $Mdocdate: August 14 2013 $
.Dd $Mdocdate: September 14 2014 $
.Dt STRTOUL 3 .Dt STRTOUL 3
.Os .Os
.Sh NAME .Sh NAME
@ -61,21 +61,21 @@ The
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to an to an
.Li unsigned long
.Vt unsigned long
value. value.
The The
.Fn strtoull .Fn strtoull
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to an to an
.Li unsigned long long
.Vt unsigned long long
value. value.
The The
.Fn strtoumax .Fn strtoumax
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to a to a
.Li umaxint_t
.Vt umaxint_t
value. value.
The The
.Fn strtouq .Fn strtouq
@ -84,8 +84,7 @@ function is a deprecated equivalent of
and is provided for backwards compatibility with legacy programs. and is provided for backwards compatibility with legacy programs.
The conversion is done according to the given The conversion is done according to the given
.Fa base , .Fa base ,
which must be a number between 2 and 36 inclusive
or the special value 0.
which must be a number between 2 and 36 inclusive or the special value 0.
If the string in If the string in
.Fa nptr .Fa nptr
represents a negative number, it will be converted to its unsigned equivalent. represents a negative number, it will be converted to its unsigned equivalent.
@ -111,9 +110,12 @@ is taken as 10 (decimal) unless the next character is
in which case it is taken as 8 (octal). in which case it is taken as 8 (octal).
.Pp .Pp
The remainder of the string is converted to an The remainder of the string is converted to an
.Li unsigned long
value in the obvious manner, stopping at the end of the string
or at the first character that does not produce a valid digit
.Vt unsigned long ,
.Vt unsigned long long ,
or
.Vt uintmax_t
value in the obvious manner,
stopping at the first character which is not a valid digit
in the given base. in the given base.
(In bases above 10, the letter (In bases above 10, the letter
.Ql A .Ql A
@ -172,12 +174,6 @@ and the global variable
.Va errno .Va errno
is set to is set to
.Er ERANGE . .Er ERANGE .
If no conversion could be performed, 0 is returned;
the global variable
.Va errno
is also set to
.Er EINVAL ,
though this is not portable across all platforms.
.Pp .Pp
There is no way to determine if There is no way to determine if
.Fn strtoul .Fn strtoul
@ -185,6 +181,14 @@ has processed a negative number (and returned an unsigned value) short of
examining the string in examining the string in
.Fa nptr .Fa nptr
directly. directly.
.Pp
If there is no valid digit, 0 is returned.
If
.Ar base
is invalid, 0 is returned and the global variable
.Va errno
is set to
.Er EINVAL .
.Sh EXAMPLES .Sh EXAMPLES
Ensuring that a string is a valid number (i.e., in range and containing no Ensuring that a string is a valid number (i.e., in range and containing no
trailing characters) requires clearing trailing characters) requires clearing
@ -222,6 +226,10 @@ alternately, use
.Xr sscanf 3 . .Xr sscanf 3 .
.Sh ERRORS .Sh ERRORS
.Bl -tag -width Er .Bl -tag -width Er
.It Bq Er EINVAL
The value of
.Ar base
was neither between 2 and 36 inclusive nor the special value 0.
.It Bq Er ERANGE .It Bq Er ERANGE
The given string was out of range; the value converted has been clamped. The given string was out of range; the value converted has been clamped.
.El .El
@ -236,6 +244,13 @@ and
.Fn strtoumax .Fn strtoumax
functions conform to functions conform to
.St -ansiC-99 . .St -ansiC-99 .
Setting
.Va errno
to
.Dv EINVAL
is an extension to that standard required by
.St -p1003.1-2008 .
.Pp
The The
.Fn strtouq .Fn strtouq
function is a function is a


Loading…
Cancel
Save