Browse Source

Replace strtou?q() with the more standard strtou?ll(), using weak

aliases to fake up strtou?q().  espie@ OK.
OPENBSD_3_2
millert 22 years ago
parent
commit
8605e7d157
6 changed files with 113 additions and 43 deletions
  1. +5
    -1
      src/include/stdlib.h
  2. +4
    -4
      src/lib/libc/stdlib/Makefile.inc
  3. +23
    -4
      src/lib/libc/stdlib/strtol.3
  4. +29
    -15
      src/lib/libc/stdlib/strtoll.c
  5. +24
    -5
      src/lib/libc/stdlib/strtoul.3
  6. +28
    -14
      src/lib/libc/stdlib/strtoull.c

+ 5
- 1
src/include/stdlib.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: stdlib.h,v 1.19 2002/02/20 18:12:44 millert Exp $ */
/* $OpenBSD: stdlib.h,v 1.20 2002/06/29 00:20:11 millert Exp $ */
/* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */ /* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */
/*- /*-
@ -125,8 +125,12 @@ void *realloc(void *, size_t);
void srand(unsigned); void srand(unsigned);
double strtod(const char *, char **); double strtod(const char *, char **);
long strtol(const char *, char **, int); long strtol(const char *, char **, int);
long long
strtoll(const char *, char **, int);
unsigned long unsigned long
strtoul(const char *, char **, int); strtoul(const char *, char **, int);
unsigned long long
strtoull(const char *, char **, int);
int system(const char *); int system(const char *);
/* these are currently just stubs */ /* these are currently just stubs */


+ 4
- 4
src/lib/libc/stdlib/Makefile.inc View File

@ -6,8 +6,8 @@
SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \
cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \
malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \ malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \
random.c realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \
strtouq.c system.c tfind.c tsearch.c \
random.c realpath.c setenv.c strtod.c strtol.c strtoll.c strtoul.c \
strtoull.c system.c tfind.c tsearch.c \
_rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \
mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c
@ -48,8 +48,8 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3
MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
MLINKS+=strtol.3 strtoq.3
MLINKS+=strtoul.3 strtouq.3
MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3
MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3
MLINKS+=tsearch.3 tfind.3 MLINKS+=tsearch.3 tfind.3
MLINKS+=tsearch.3 tdelete.3 MLINKS+=tsearch.3 tdelete.3
MLINKS+=tsearch.3 twalk.3 MLINKS+=tsearch.3 twalk.3


+ 23
- 4
src/lib/libc/stdlib/strtol.3 View File

@ -33,21 +33,27 @@
.\" 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.9 2000/08/09 15:51:21 aaron Exp $
.\" $OpenBSD: strtol.3,v 1.10 2002/06/29 00:20:11 millert Exp $
.\" .\"
.Dd June 25, 1992 .Dd June 25, 1992
.Dt STRTOL 3 .Dt STRTOL 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strtol , .Nm strtol ,
.Nm strtoll ,
.Nm strtoq .Nm strtoq
.Nd convert string value to a long or quad_t integer
.Nd convert string value to a long or long long integer
.Sh SYNOPSIS .Sh SYNOPSIS
.Fd #include <stdlib.h> .Fd #include <stdlib.h>
.Fd #include <limits.h> .Fd #include <limits.h>
.Ft long .Ft long
.Fn strtol "const char *nptr" "char **endptr" "int base" .Fn strtol "const char *nptr" "char **endptr" "int base"
.Pp .Pp
.Fd #include <stdlib.h>
.Fd #include <limits.h>
.Ft long long
.Fn strtoll "const char *nptr" "char **endptr" "int base"
.Pp
.Fd #include <sys/types.h> .Fd #include <sys/types.h>
.Fd #include <stdlib.h> .Fd #include <stdlib.h>
.Fd #include <limits.h> .Fd #include <limits.h>
@ -62,12 +68,17 @@ to a
.Li long .Li long
value. value.
The The
.Fn strtoq
.Fn strtoll
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to a to a
.Li quad_t
.Li long long
value. value.
The
.Fn strtoq
function is a deprecated equivalent of
.Fn strtoll
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.
@ -141,6 +152,14 @@ In both cases,
.Va errno .Va errno
is set to is set to
.Er ERANGE . .Er ERANGE .
.Pp
The
.Fn strtoll
function has identical return values except that
.Dv LLONG_MIN
and
.Dv LLONG_MAX
are used to indicate underflow and overflow respectively.
.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


src/lib/libc/stdlib/strtoq.c → src/lib/libc/stdlib/strtoll.c View File

@ -32,7 +32,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: strtoq.c,v 1.4 1996/08/19 08:33:52 tholo Exp $";
static const char rcsid[] = "$OpenBSD: strtoll.c,v 1.1 2002/06/29 00:20:11 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
@ -43,21 +43,21 @@ static char rcsid[] = "$OpenBSD: strtoq.c,v 1.4 1996/08/19 08:33:52 tholo Exp $"
#include <stdlib.h> #include <stdlib.h>
/* /*
* Convert a string to a quad integer.
* Convert a string to a long long.
* *
* Ignores `locale' stuff. Assumes that the upper and lower case * Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous. * alphabets and digits are each contiguous.
*/ */
quad_t
strtoq(nptr, endptr, base)
long long
strtoll(nptr, endptr, base)
const char *nptr; const char *nptr;
char **endptr; char **endptr;
register int base;
int base;
{ {
register const char *s;
register quad_t acc, cutoff;
register int c;
register int neg, any, cutlim;
const char *s;
long long acc, cutoff;
int c;
int neg, any, cutlim;
/* /*
* Skip white space and pick up leading +/- sign if any. * Skip white space and pick up leading +/- sign if any.
@ -92,7 +92,7 @@ strtoq(nptr, endptr, base)
* followed by a legal input character, is too big. One that * followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit * is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last * between valid and invalid numbers is then based on the last
* digit. For instance, if the range for quads is
* digit. For instance, if the range for long longs is
* [-9223372036854775808..9223372036854775807] and the input base * [-9223372036854775808..9223372036854775807] and the input base
* is 10, cutoff will be set to 922337203685477580 and cutlim to * is 10, cutoff will be set to 922337203685477580 and cutlim to
* either 7 (neg==0) or 8 (neg==1), meaning that if we have * either 7 (neg==0) or 8 (neg==1), meaning that if we have
@ -103,7 +103,7 @@ strtoq(nptr, endptr, base)
* Set any if any `digits' consumed; make it negative to indicate * Set any if any `digits' consumed; make it negative to indicate
* overflow. * overflow.
*/ */
cutoff = neg ? QUAD_MIN : QUAD_MAX;
cutoff = neg ? LLONG_MIN : LLONG_MAX;
cutlim = cutoff % base; cutlim = cutoff % base;
cutoff /= base; cutoff /= base;
if (neg) { if (neg) {
@ -125,9 +125,9 @@ strtoq(nptr, endptr, base)
if (any < 0) if (any < 0)
continue; continue;
if (neg) { if (neg) {
if (acc < cutoff || acc == cutoff && c > cutlim) {
if (acc < cutoff || (acc == cutoff && c > cutlim)) {
any = -1; any = -1;
acc = QUAD_MIN;
acc = LLONG_MIN;
errno = ERANGE; errno = ERANGE;
} else { } else {
any = 1; any = 1;
@ -135,9 +135,9 @@ strtoq(nptr, endptr, base)
acc -= c; acc -= c;
} }
} else { } else {
if (acc > cutoff || acc == cutoff && c > cutlim) {
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1; any = -1;
acc = QUAD_MAX;
acc = LLONG_MAX;
errno = ERANGE; errno = ERANGE;
} else { } else {
any = 1; any = 1;
@ -150,3 +150,17 @@ strtoq(nptr, endptr, base)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);
return (acc); return (acc);
} }
#ifdef __weak_alias
__weak_alias(strtoq, strtoll);
#else
quad_t
strtoq(nptr, endptr, base)
const char *nptr;
char **endptr;
int base;
{
return ((quad_t)strtoll(nptr, endptr, base);
}
#endif

+ 24
- 5
src/lib/libc/stdlib/strtoul.3 View File

@ -33,21 +33,27 @@
.\" 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.9 2002/04/30 16:31:42 mpech Exp $
.\" $OpenBSD: strtoul.3,v 1.10 2002/06/29 00:20:11 millert Exp $
.\" .\"
.Dd June 25, 1992 .Dd June 25, 1992
.Dt STRTOUL 3 .Dt STRTOUL 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strtoul , .Nm strtoul ,
.Nm strtoull ,
.Nm strtouq .Nm strtouq
.Nd convert a string to an unsigned long or uquad_t integer
.Nd convert a string to an unsigned long or unsigned long long integer
.Sh SYNOPSIS .Sh SYNOPSIS
.Fd #include <stdlib.h> .Fd #include <stdlib.h>
.Fd #include <limits.h> .Fd #include <limits.h>
.Ft unsigned long .Ft unsigned long
.Fn strtoul "const char *nptr" "char **endptr" "int base" .Fn strtoul "const char *nptr" "char **endptr" "int base"
.Pp .Pp
.Fd #include <stdlib.h>
.Fd #include <limits.h>
.Ft unsigned long long
.Fn strtoull "const char *nptr" "char **endptr" "int base"
.Pp
.Fd #include <sys/types.h> .Fd #include <sys/types.h>
.Fd #include <stdlib.h> .Fd #include <stdlib.h>
.Fd #include <limits.h> .Fd #include <limits.h>
@ -62,12 +68,17 @@ to an
.Li unsigned long .Li unsigned long
value. value.
The The
.Fn strtouq
.Fn strtoull
function converts the string in function converts the string in
.Fa nptr .Fa nptr
to a
.Li u_quad_t
to an
.Li unsigned long long
value. value.
The
.Fn strtouq
function is a deprecated equivalent of
.Fn strtoull
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 which must be a number between 2 and 36 inclusive
@ -148,6 +159,14 @@ and sets the global variable
to to
.Er ERANGE . .Er ERANGE .
.Pp .Pp
The
.Fn strtoull
function has identical return values except that
.Dv ULLONG_MIN
and
.Dv ULLONG_MAX
are used to indicate underflow and overflow respectively.
.Pp
There is no way to determine if There is no way to determine if
.Fn strtoul .Fn strtoul
has processed a negative number (and returned an unsigned value) short of has processed a negative number (and returned an unsigned value) short of


src/lib/libc/stdlib/strtouq.c → src/lib/libc/stdlib/strtoull.c View File

@ -32,7 +32,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: strtouq.c,v 1.4 1996/08/19 08:33:53 tholo Exp $";
static const char rcsid[] = "$OpenBSD: strtoull.c,v 1.1 2002/06/29 00:20:11 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
@ -43,21 +43,21 @@ static char rcsid[] = "$OpenBSD: strtouq.c,v 1.4 1996/08/19 08:33:53 tholo Exp $
#include <stdlib.h> #include <stdlib.h>
/* /*
* Convert a string to an unsigned quad integer.
* Convert a string to an unsigned long long.
* *
* Ignores `locale' stuff. Assumes that the upper and lower case * Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous. * alphabets and digits are each contiguous.
*/ */
u_quad_t
strtouq(nptr, endptr, base)
unsigned long long
strtoull(nptr, endptr, base)
const char *nptr; const char *nptr;
char **endptr; char **endptr;
register int base;
int base;
{ {
register const char *s;
register u_quad_t acc, cutoff;
register int c;
register int neg, any, cutlim;
const char *s;
unsigned long long acc, cutoff;
int c;
int neg, any, cutlim;
/* /*
* See strtoq for comments as to the logic used. * See strtoq for comments as to the logic used.
@ -83,8 +83,8 @@ strtouq(nptr, endptr, base)
if (base == 0) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
cutoff = UQUAD_MAX / (u_quad_t)base;
cutlim = UQUAD_MAX % (u_quad_t)base;
cutoff = ULLONG_MAX / (unsigned long long)base;
cutlim = ULLONG_MAX % (unsigned long long)base;
for (acc = 0, any = 0;; c = (unsigned char) *s++) { for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (isdigit(c)) if (isdigit(c))
c -= '0'; c -= '0';
@ -96,13 +96,13 @@ strtouq(nptr, endptr, base)
break; break;
if (any < 0) if (any < 0)
continue; continue;
if (acc > cutoff || acc == cutoff && c > cutlim) {
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1; any = -1;
acc = UQUAD_MAX;
acc = ULLONG_MAX;
errno = ERANGE; errno = ERANGE;
} else { } else {
any = 1; any = 1;
acc *= (u_quad_t)base;
acc *= (unsigned long long)base;
acc += c; acc += c;
} }
} }
@ -112,3 +112,17 @@ strtouq(nptr, endptr, base)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);
return (acc); return (acc);
} }
#ifdef __weak_alias
__weak_alias(strtouq, strtoull);
#else
u_quad_t
strtouq(nptr, endptr, base)
const char *nptr;
char **endptr;
int base;
{
return ((u_quad_t)strtoull(nptr, endptr, base);
}
#endif

Loading…
Cancel
Save