diff options
author | millert <> | 2002-06-29 00:20:11 +0000 |
---|---|---|
committer | millert <> | 2002-06-29 00:20:11 +0000 |
commit | 6adefe10f90940abb77fbd207bd42c11497679b9 (patch) | |
tree | b09c3b0aa815c3c77e8a7c20492b1d18817cd32b /src | |
parent | e08f871ca434f7bd4986a8b654059f32fda1e02a (diff) | |
download | openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.tar.gz openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.tar.bz2 openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.zip |
Replace strtou?q() with the more standard strtou?ll(), using weak
aliases to fake up strtou?q(). espie@ OK.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 8 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtol.3 | 27 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoll.c (renamed from src/lib/libc/stdlib/strtoq.c) | 44 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 29 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoull.c (renamed from src/lib/libc/stdlib/strtouq.c) | 42 |
5 files changed, 108 insertions, 42 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 55fb00fcf4..4b4c88b9af 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -6,8 +6,8 @@ | |||
6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | 6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ |
7 | cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ | 7 | cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ |
8 | malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \ | 8 | malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \ |
9 | random.c realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \ | 9 | random.c realpath.c setenv.c strtod.c strtol.c strtoll.c strtoul.c \ |
10 | strtouq.c system.c tfind.c tsearch.c \ | 10 | strtoull.c system.c tfind.c tsearch.c \ |
11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ | 11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ |
12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | 12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c |
13 | 13 | ||
@@ -48,8 +48,8 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3 | |||
48 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | 48 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 |
49 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | 49 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 |
50 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | 50 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 |
51 | MLINKS+=strtol.3 strtoq.3 | 51 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 |
52 | MLINKS+=strtoul.3 strtouq.3 | 52 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 |
53 | MLINKS+=tsearch.3 tfind.3 | 53 | MLINKS+=tsearch.3 tfind.3 |
54 | MLINKS+=tsearch.3 tdelete.3 | 54 | MLINKS+=tsearch.3 tdelete.3 |
55 | MLINKS+=tsearch.3 twalk.3 | 55 | MLINKS+=tsearch.3 twalk.3 |
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 index afdb84f19b..a5bdff0b81 100644 --- a/src/lib/libc/stdlib/strtol.3 +++ b/src/lib/libc/stdlib/strtol.3 | |||
@@ -33,21 +33,27 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
35 | .\" | 35 | .\" |
36 | .\" $OpenBSD: strtol.3,v 1.9 2000/08/09 15:51:21 aaron Exp $ | 36 | .\" $OpenBSD: strtol.3,v 1.10 2002/06/29 00:20:11 millert Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 25, 1992 | 38 | .Dd June 25, 1992 |
39 | .Dt STRTOL 3 | 39 | .Dt STRTOL 3 |
40 | .Os | 40 | .Os |
41 | .Sh NAME | 41 | .Sh NAME |
42 | .Nm strtol , | 42 | .Nm strtol , |
43 | .Nm strtoll , | ||
43 | .Nm strtoq | 44 | .Nm strtoq |
44 | .Nd convert string value to a long or quad_t integer | 45 | .Nd convert string value to a long or long long integer |
45 | .Sh SYNOPSIS | 46 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 47 | .Fd #include <stdlib.h> |
47 | .Fd #include <limits.h> | 48 | .Fd #include <limits.h> |
48 | .Ft long | 49 | .Ft long |
49 | .Fn strtol "const char *nptr" "char **endptr" "int base" | 50 | .Fn strtol "const char *nptr" "char **endptr" "int base" |
50 | .Pp | 51 | .Pp |
52 | .Fd #include <stdlib.h> | ||
53 | .Fd #include <limits.h> | ||
54 | .Ft long long | ||
55 | .Fn strtoll "const char *nptr" "char **endptr" "int base" | ||
56 | .Pp | ||
51 | .Fd #include <sys/types.h> | 57 | .Fd #include <sys/types.h> |
52 | .Fd #include <stdlib.h> | 58 | .Fd #include <stdlib.h> |
53 | .Fd #include <limits.h> | 59 | .Fd #include <limits.h> |
@@ -62,12 +68,17 @@ to a | |||
62 | .Li long | 68 | .Li long |
63 | value. | 69 | value. |
64 | The | 70 | The |
65 | .Fn strtoq | 71 | .Fn strtoll |
66 | function converts the string in | 72 | function converts the string in |
67 | .Fa nptr | 73 | .Fa nptr |
68 | to a | 74 | to a |
69 | .Li quad_t | 75 | .Li long long |
70 | value. | 76 | value. |
77 | The | ||
78 | .Fn strtoq | ||
79 | function is a deprecated equivalent of | ||
80 | .Fn strtoll | ||
81 | and is provided for backwards compatibility with legacy programs. | ||
71 | The conversion is done according to the given | 82 | The conversion is done according to the given |
72 | .Fa base , | 83 | .Fa base , |
73 | which must be a number between 2 and 36 inclusive or the special value 0. | 84 | which must be a number between 2 and 36 inclusive or the special value 0. |
@@ -141,6 +152,14 @@ In both cases, | |||
141 | .Va errno | 152 | .Va errno |
142 | is set to | 153 | is set to |
143 | .Er ERANGE . | 154 | .Er ERANGE . |
155 | .Pp | ||
156 | The | ||
157 | .Fn strtoll | ||
158 | function has identical return values except that | ||
159 | .Dv LLONG_MIN | ||
160 | and | ||
161 | .Dv LLONG_MAX | ||
162 | are used to indicate underflow and overflow respectively. | ||
144 | .Sh EXAMPLES | 163 | .Sh EXAMPLES |
145 | Ensuring that a string is a valid number (i.e., in range and containing no | 164 | Ensuring that a string is a valid number (i.e., in range and containing no |
146 | trailing characters) requires clearing | 165 | trailing characters) requires clearing |
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoll.c index 44aabd73f0..b0eb6d198c 100644 --- a/src/lib/libc/stdlib/strtoq.c +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | static char rcsid[] = "$OpenBSD: strtoq.c,v 1.4 1996/08/19 08:33:52 tholo Exp $"; | 35 | static const char rcsid[] = "$OpenBSD: strtoll.c,v 1.1 2002/06/29 00:20:11 millert Exp $"; |
36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
37 | 37 | ||
38 | #include <sys/types.h> | 38 | #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 $" | |||
43 | #include <stdlib.h> | 43 | #include <stdlib.h> |
44 | 44 | ||
45 | /* | 45 | /* |
46 | * Convert a string to a quad integer. | 46 | * Convert a string to a long long. |
47 | * | 47 | * |
48 | * Ignores `locale' stuff. Assumes that the upper and lower case | 48 | * Ignores `locale' stuff. Assumes that the upper and lower case |
49 | * alphabets and digits are each contiguous. | 49 | * alphabets and digits are each contiguous. |
50 | */ | 50 | */ |
51 | quad_t | 51 | long long |
52 | strtoq(nptr, endptr, base) | 52 | strtoll(nptr, endptr, base) |
53 | const char *nptr; | 53 | const char *nptr; |
54 | char **endptr; | 54 | char **endptr; |
55 | register int base; | 55 | int base; |
56 | { | 56 | { |
57 | register const char *s; | 57 | const char *s; |
58 | register quad_t acc, cutoff; | 58 | long long acc, cutoff; |
59 | register int c; | 59 | int c; |
60 | register int neg, any, cutlim; | 60 | int neg, any, cutlim; |
61 | 61 | ||
62 | /* | 62 | /* |
63 | * Skip white space and pick up leading +/- sign if any. | 63 | * Skip white space and pick up leading +/- sign if any. |
@@ -92,7 +92,7 @@ strtoq(nptr, endptr, base) | |||
92 | * followed by a legal input character, is too big. One that | 92 | * followed by a legal input character, is too big. One that |
93 | * is equal to this value may be valid or not; the limit | 93 | * is equal to this value may be valid or not; the limit |
94 | * between valid and invalid numbers is then based on the last | 94 | * between valid and invalid numbers is then based on the last |
95 | * digit. For instance, if the range for quads is | 95 | * digit. For instance, if the range for long longs is |
96 | * [-9223372036854775808..9223372036854775807] and the input base | 96 | * [-9223372036854775808..9223372036854775807] and the input base |
97 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | 97 | * is 10, cutoff will be set to 922337203685477580 and cutlim to |
98 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | 98 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have |
@@ -103,7 +103,7 @@ strtoq(nptr, endptr, base) | |||
103 | * Set any if any `digits' consumed; make it negative to indicate | 103 | * Set any if any `digits' consumed; make it negative to indicate |
104 | * overflow. | 104 | * overflow. |
105 | */ | 105 | */ |
106 | cutoff = neg ? QUAD_MIN : QUAD_MAX; | 106 | cutoff = neg ? LLONG_MIN : LLONG_MAX; |
107 | cutlim = cutoff % base; | 107 | cutlim = cutoff % base; |
108 | cutoff /= base; | 108 | cutoff /= base; |
109 | if (neg) { | 109 | if (neg) { |
@@ -125,9 +125,9 @@ strtoq(nptr, endptr, base) | |||
125 | if (any < 0) | 125 | if (any < 0) |
126 | continue; | 126 | continue; |
127 | if (neg) { | 127 | if (neg) { |
128 | if (acc < cutoff || acc == cutoff && c > cutlim) { | 128 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { |
129 | any = -1; | 129 | any = -1; |
130 | acc = QUAD_MIN; | 130 | acc = LLONG_MIN; |
131 | errno = ERANGE; | 131 | errno = ERANGE; |
132 | } else { | 132 | } else { |
133 | any = 1; | 133 | any = 1; |
@@ -135,9 +135,9 @@ strtoq(nptr, endptr, base) | |||
135 | acc -= c; | 135 | acc -= c; |
136 | } | 136 | } |
137 | } else { | 137 | } else { |
138 | if (acc > cutoff || acc == cutoff && c > cutlim) { | 138 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { |
139 | any = -1; | 139 | any = -1; |
140 | acc = QUAD_MAX; | 140 | acc = LLONG_MAX; |
141 | errno = ERANGE; | 141 | errno = ERANGE; |
142 | } else { | 142 | } else { |
143 | any = 1; | 143 | any = 1; |
@@ -150,3 +150,17 @@ strtoq(nptr, endptr, base) | |||
150 | *endptr = (char *) (any ? s - 1 : nptr); | 150 | *endptr = (char *) (any ? s - 1 : nptr); |
151 | return (acc); | 151 | return (acc); |
152 | } | 152 | } |
153 | |||
154 | #ifdef __weak_alias | ||
155 | __weak_alias(strtoq, strtoll); | ||
156 | #else | ||
157 | quad_t | ||
158 | strtoq(nptr, endptr, base) | ||
159 | const char *nptr; | ||
160 | char **endptr; | ||
161 | int base; | ||
162 | { | ||
163 | |||
164 | return ((quad_t)strtoll(nptr, endptr, base); | ||
165 | } | ||
166 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 index 61269bc900..6d55de4d7a 100644 --- a/src/lib/libc/stdlib/strtoul.3 +++ b/src/lib/libc/stdlib/strtoul.3 | |||
@@ -33,21 +33,27 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
35 | .\" | 35 | .\" |
36 | .\" $OpenBSD: strtoul.3,v 1.9 2002/04/30 16:31:42 mpech Exp $ | 36 | .\" $OpenBSD: strtoul.3,v 1.10 2002/06/29 00:20:11 millert Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 25, 1992 | 38 | .Dd June 25, 1992 |
39 | .Dt STRTOUL 3 | 39 | .Dt STRTOUL 3 |
40 | .Os | 40 | .Os |
41 | .Sh NAME | 41 | .Sh NAME |
42 | .Nm strtoul , | 42 | .Nm strtoul , |
43 | .Nm strtoull , | ||
43 | .Nm strtouq | 44 | .Nm strtouq |
44 | .Nd convert a string to an unsigned long or uquad_t integer | 45 | .Nd convert a string to an unsigned long or unsigned long long integer |
45 | .Sh SYNOPSIS | 46 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 47 | .Fd #include <stdlib.h> |
47 | .Fd #include <limits.h> | 48 | .Fd #include <limits.h> |
48 | .Ft unsigned long | 49 | .Ft unsigned long |
49 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | 50 | .Fn strtoul "const char *nptr" "char **endptr" "int base" |
50 | .Pp | 51 | .Pp |
52 | .Fd #include <stdlib.h> | ||
53 | .Fd #include <limits.h> | ||
54 | .Ft unsigned long long | ||
55 | .Fn strtoull "const char *nptr" "char **endptr" "int base" | ||
56 | .Pp | ||
51 | .Fd #include <sys/types.h> | 57 | .Fd #include <sys/types.h> |
52 | .Fd #include <stdlib.h> | 58 | .Fd #include <stdlib.h> |
53 | .Fd #include <limits.h> | 59 | .Fd #include <limits.h> |
@@ -62,12 +68,17 @@ to an | |||
62 | .Li unsigned long | 68 | .Li unsigned long |
63 | value. | 69 | value. |
64 | The | 70 | The |
65 | .Fn strtouq | 71 | .Fn strtoull |
66 | function converts the string in | 72 | function converts the string in |
67 | .Fa nptr | 73 | .Fa nptr |
68 | to a | 74 | to an |
69 | .Li u_quad_t | 75 | .Li unsigned long long |
70 | value. | 76 | value. |
77 | The | ||
78 | .Fn strtouq | ||
79 | function is a deprecated equivalent of | ||
80 | .Fn strtoull | ||
81 | and is provided for backwards compatibility with legacy programs. | ||
71 | The conversion is done according to the given | 82 | The conversion is done according to the given |
72 | .Fa base , | 83 | .Fa base , |
73 | which must be a number between 2 and 36 inclusive | 84 | which must be a number between 2 and 36 inclusive |
@@ -148,6 +159,14 @@ and sets the global variable | |||
148 | to | 159 | to |
149 | .Er ERANGE . | 160 | .Er ERANGE . |
150 | .Pp | 161 | .Pp |
162 | The | ||
163 | .Fn strtoull | ||
164 | function has identical return values except that | ||
165 | .Dv ULLONG_MIN | ||
166 | and | ||
167 | .Dv ULLONG_MAX | ||
168 | are used to indicate underflow and overflow respectively. | ||
169 | .Pp | ||
151 | There is no way to determine if | 170 | There is no way to determine if |
152 | .Fn strtoul | 171 | .Fn strtoul |
153 | has processed a negative number (and returned an unsigned value) short of | 172 | has processed a negative number (and returned an unsigned value) short of |
diff --git a/src/lib/libc/stdlib/strtouq.c b/src/lib/libc/stdlib/strtoull.c index 1f29a22f33..7b4dd56c01 100644 --- a/src/lib/libc/stdlib/strtouq.c +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | static char rcsid[] = "$OpenBSD: strtouq.c,v 1.4 1996/08/19 08:33:53 tholo Exp $"; | 35 | static const char rcsid[] = "$OpenBSD: strtoull.c,v 1.1 2002/06/29 00:20:11 millert Exp $"; |
36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
37 | 37 | ||
38 | #include <sys/types.h> | 38 | #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 $ | |||
43 | #include <stdlib.h> | 43 | #include <stdlib.h> |
44 | 44 | ||
45 | /* | 45 | /* |
46 | * Convert a string to an unsigned quad integer. | 46 | * Convert a string to an unsigned long long. |
47 | * | 47 | * |
48 | * Ignores `locale' stuff. Assumes that the upper and lower case | 48 | * Ignores `locale' stuff. Assumes that the upper and lower case |
49 | * alphabets and digits are each contiguous. | 49 | * alphabets and digits are each contiguous. |
50 | */ | 50 | */ |
51 | u_quad_t | 51 | unsigned long long |
52 | strtouq(nptr, endptr, base) | 52 | strtoull(nptr, endptr, base) |
53 | const char *nptr; | 53 | const char *nptr; |
54 | char **endptr; | 54 | char **endptr; |
55 | register int base; | 55 | int base; |
56 | { | 56 | { |
57 | register const char *s; | 57 | const char *s; |
58 | register u_quad_t acc, cutoff; | 58 | unsigned long long acc, cutoff; |
59 | register int c; | 59 | int c; |
60 | register int neg, any, cutlim; | 60 | int neg, any, cutlim; |
61 | 61 | ||
62 | /* | 62 | /* |
63 | * See strtoq for comments as to the logic used. | 63 | * See strtoq for comments as to the logic used. |
@@ -83,8 +83,8 @@ strtouq(nptr, endptr, base) | |||
83 | if (base == 0) | 83 | if (base == 0) |
84 | base = c == '0' ? 8 : 10; | 84 | base = c == '0' ? 8 : 10; |
85 | 85 | ||
86 | cutoff = UQUAD_MAX / (u_quad_t)base; | 86 | cutoff = ULLONG_MAX / (unsigned long long)base; |
87 | cutlim = UQUAD_MAX % (u_quad_t)base; | 87 | cutlim = ULLONG_MAX % (unsigned long long)base; |
88 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | 88 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { |
89 | if (isdigit(c)) | 89 | if (isdigit(c)) |
90 | c -= '0'; | 90 | c -= '0'; |
@@ -96,13 +96,13 @@ strtouq(nptr, endptr, base) | |||
96 | break; | 96 | break; |
97 | if (any < 0) | 97 | if (any < 0) |
98 | continue; | 98 | continue; |
99 | if (acc > cutoff || acc == cutoff && c > cutlim) { | 99 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { |
100 | any = -1; | 100 | any = -1; |
101 | acc = UQUAD_MAX; | 101 | acc = ULLONG_MAX; |
102 | errno = ERANGE; | 102 | errno = ERANGE; |
103 | } else { | 103 | } else { |
104 | any = 1; | 104 | any = 1; |
105 | acc *= (u_quad_t)base; | 105 | acc *= (unsigned long long)base; |
106 | acc += c; | 106 | acc += c; |
107 | } | 107 | } |
108 | } | 108 | } |
@@ -112,3 +112,17 @@ strtouq(nptr, endptr, base) | |||
112 | *endptr = (char *) (any ? s - 1 : nptr); | 112 | *endptr = (char *) (any ? s - 1 : nptr); |
113 | return (acc); | 113 | return (acc); |
114 | } | 114 | } |
115 | |||
116 | #ifdef __weak_alias | ||
117 | __weak_alias(strtouq, strtoull); | ||
118 | #else | ||
119 | u_quad_t | ||
120 | strtouq(nptr, endptr, base) | ||
121 | const char *nptr; | ||
122 | char **endptr; | ||
123 | int base; | ||
124 | { | ||
125 | |||
126 | return ((u_quad_t)strtoull(nptr, endptr, base); | ||
127 | } | ||
128 | #endif | ||