summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2006-01-13 17:58:09 +0000
committermillert <>2006-01-13 17:58:09 +0000
commit978c3a7e8d11b1e84147c6da7fa5908182dbf6f1 (patch)
tree302bb25ba83f38b5dc042943f23d0ff80c5d29d5
parent1e745892f72a6490f4783b86b64e88e0c5c4bf61 (diff)
downloadopenbsd-978c3a7e8d11b1e84147c6da7fa5908182dbf6f1.tar.gz
openbsd-978c3a7e8d11b1e84147c6da7fa5908182dbf6f1.tar.bz2
openbsd-978c3a7e8d11b1e84147c6da7fa5908182dbf6f1.zip
Add lldiv(), imaxabs(), imaxdiv(), strtoimax() and strtoumax()
-rw-r--r--src/lib/libc/stdlib/Makefile.inc21
-rw-r--r--src/lib/libc/stdlib/abs.33
-rw-r--r--src/lib/libc/stdlib/div.34
-rw-r--r--src/lib/libc/stdlib/imaxabs.366
-rw-r--r--src/lib/libc/stdlib/imaxabs.c38
-rw-r--r--src/lib/libc/stdlib/imaxdiv.367
-rw-r--r--src/lib/libc/stdlib/imaxdiv.c50
-rw-r--r--src/lib/libc/stdlib/labs.33
-rw-r--r--src/lib/libc/stdlib/ldiv.34
-rw-r--r--src/lib/libc/stdlib/lldiv.367
-rw-r--r--src/lib/libc/stdlib/lldiv.c50
-rw-r--r--src/lib/libc/stdlib/qabs.33
-rw-r--r--src/lib/libc/stdlib/qdiv.34
-rw-r--r--src/lib/libc/stdlib/strtoimax.c140
-rw-r--r--src/lib/libc/stdlib/strtol.368
-rw-r--r--src/lib/libc/stdlib/strtoul.369
-rw-r--r--src/lib/libc/stdlib/strtoumax.c102
17 files changed, 685 insertions, 74 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index ba81d5ee08..c7ee0a80ec 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,14 +1,14 @@
1# $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ 1# $OpenBSD: Makefile.inc,v 1.35 2006/01/13 17:58:09 millert Exp $
2 2
3# stdlib sources 3# stdlib sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib
5 5
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ 6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ 7 calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
8 getsubopt.c hcreate.c heapsort.c l64a.c llabs.c lsearch.c malloc.c \ 8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \
9 merge.c putenv.c qsort.c radixsort.c rand.c random.c \ 9 lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \
10 realpath.c setenv.c strtod.c strtol.c strtoll.c strtonum.c strtoul.c \ 10 random.c realpath.c setenv.c strtoimax.c strtod.c strtol.c strtoll.c \
11 strtoull.c system.c \ 11 strtonum.c strtoul.c strtoull.c strtoumax.c system.c \
12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ 12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \
13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c 13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c
14 14
@@ -41,9 +41,10 @@ SRCS+= insque.c remque.c
41 41
42MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ 42MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
43 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ 43 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
44 getsubopt.3 hcreate.3 insque.3 labs.3 ldiv.3 lsearch.3 malloc.3 qabs.3 \ 44 getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \
45 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ 45 lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \
46 strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3 46 rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \
47 system.3 tsearch.3
47 48
48MLINKS+=exit.3 _Exit.3 49MLINKS+=exit.3 _Exit.3
49MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 50MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3
@@ -63,8 +64,8 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3
63MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 64MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
64MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 65MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
65MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 66MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
66MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 67MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
67MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 68MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
68MLINKS+=tsearch.3 tfind.3 69MLINKS+=tsearch.3 tfind.3
69MLINKS+=tsearch.3 tdelete.3 70MLINKS+=tsearch.3 tdelete.3
70MLINKS+=tsearch.3 twalk.3 71MLINKS+=tsearch.3 twalk.3
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3
index f0a6b8a23a..fc0b20b7d5 100644
--- a/src/lib/libc/stdlib/abs.3
+++ b/src/lib/libc/stdlib/abs.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: abs.3,v 1.6 2003/06/02 20:18:37 millert Exp $ 32.\" $OpenBSD: abs.3,v 1.7 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 29, 1991 34.Dd June 29, 1991
35.Dt ABS 3 35.Dt ABS 3
@@ -54,6 +54,7 @@ function returns the absolute value.
54.Xr cabs 3 , 54.Xr cabs 3 ,
55.Xr floor 3 , 55.Xr floor 3 ,
56.Xr hypot 3 , 56.Xr hypot 3 ,
57.Xr imaxabs 3 ,
57.Xr labs 3 , 58.Xr labs 3 ,
58.Xr math 3 59.Xr math 3
59.Sh STANDARDS 60.Sh STANDARDS
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3
index a75614310e..d6a9a0029d 100644
--- a/src/lib/libc/stdlib/div.3
+++ b/src/lib/libc/stdlib/div.3
@@ -27,7 +27,7 @@
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE. 28.\" SUCH DAMAGE.
29.\" 29.\"
30.\" $OpenBSD: div.3,v 1.7 2004/01/23 23:08:46 jmc Exp $ 30.\" $OpenBSD: div.3,v 1.8 2006/01/13 17:58:09 millert Exp $
31.\" 31.\"
32.Dd April 19, 1991 32.Dd April 19, 1991
33.Dt DIV 3 33.Dt DIV 3
@@ -53,7 +53,9 @@ members named
53and 53and
54.Fa rem . 54.Fa rem .
55.Sh SEE ALSO 55.Sh SEE ALSO
56.Xr imaxdiv 3 ,
56.Xr ldiv 3 , 57.Xr ldiv 3 ,
58.Xr lldiv 3 ,
57.Xr math 3 , 59.Xr math 3 ,
58.Xr qdiv 3 60.Xr qdiv 3
59.Sh STANDARDS 61.Sh STANDARDS
diff --git a/src/lib/libc/stdlib/imaxabs.3 b/src/lib/libc/stdlib/imaxabs.3
new file mode 100644
index 0000000000..5384d0c9a6
--- /dev/null
+++ b/src/lib/libc/stdlib/imaxabs.3
@@ -0,0 +1,66 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: imaxabs.3,v 1.1 2006/01/13 17:58:09 millert Exp $
33.\"
34.Dd January 6, 2005
35.Dt IMAXABS 3
36.Os
37.Sh NAME
38.Nm imaxabs
39.Nd integer absolute value function
40.Sh SYNOPSIS
41.Fd #include <inttypes.h>
42.Ft int
43.Fn imaxabs "int j"
44.Sh DESCRIPTION
45The
46.Fn imaxabs
47function computes the absolute value of the intmax_t variable
48.Fa j .
49.Sh RETURN VALUES
50The
51.Fn imaxabs
52function returns the absolute value.
53.Sh SEE ALSO
54.Xr abs 3 ,
55.Xr cabs 3 ,
56.Xr floor 3 ,
57.Xr hypot 3 ,
58.Xr labs 3 ,
59.Xr math 3
60.Sh STANDARDS
61The
62.Fn abs
63function conforms to
64.St -ansiC-99 .
65.Sh BUGS
66The absolute value of the most negative integer remains negative.
diff --git a/src/lib/libc/stdlib/imaxabs.c b/src/lib/libc/stdlib/imaxabs.c
new file mode 100644
index 0000000000..b7e910eefd
--- /dev/null
+++ b/src/lib/libc/stdlib/imaxabs.c
@@ -0,0 +1,38 @@
1/* $OpenBSD: imaxabs.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <inttypes.h>
33
34intmax_t
35imaxabs(intmax_t j)
36{
37 return (j < 0 ? -j : j);
38}
diff --git a/src/lib/libc/stdlib/imaxdiv.3 b/src/lib/libc/stdlib/imaxdiv.3
new file mode 100644
index 0000000000..183cc91ddd
--- /dev/null
+++ b/src/lib/libc/stdlib/imaxdiv.3
@@ -0,0 +1,67 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: imaxdiv.3,v 1.1 2006/01/13 17:58:09 millert Exp $
33.\"
34.Dd January 3, 2006
35.Dt IMAXDIV 3
36.Os
37.Sh NAME
38.Nm imaxdiv
39.Nd return quotient and remainder from division
40.Sh SYNOPSIS
41.Fd #include <inttypes.h>
42.Ft imaxdiv_t
43.Fn ldiv "intmax_t num" "intmax_t denom"
44.Sh DESCRIPTION
45The
46.Fn imaxdiv
47function computes the value
48.Fa num Ns / Ns Fa denom
49and returns the quotient and remainder in a structure named
50.Li imaxdiv_t
51that contains two
52.Li intmax_t
53members named
54.Fa quot
55and
56.Fa rem .
57.Sh SEE ALSO
58.Xr div 3 ,
59.Xr ldiv 3 ,
60.Xr lldiv 3 ,
61.Xr math 3 ,
62.Xr qdiv 3
63.Sh STANDARDS
64The
65.Fn imaxdiv
66function conforms to
67.St -ansiC-99 .
diff --git a/src/lib/libc/stdlib/imaxdiv.c b/src/lib/libc/stdlib/imaxdiv.c
new file mode 100644
index 0000000000..0515a94b96
--- /dev/null
+++ b/src/lib/libc/stdlib/imaxdiv.c
@@ -0,0 +1,50 @@
1/* $OpenBSD: imaxdiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <inttypes.h> /* imaxdiv_t */
35
36imaxdiv_t
37imaxdiv(intmax_t num, intmax_t denom)
38{
39 imaxdiv_t r;
40
41 /* see div.c for comments */
42
43 r.quot = num / denom;
44 r.rem = num % denom;
45 if (num >= 0 && r.rem < 0) {
46 r.quot++;
47 r.rem -= denom;
48 }
49 return (r);
50}
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3
index 742f3eedba..ab1ccaaf53 100644
--- a/src/lib/libc/stdlib/labs.3
+++ b/src/lib/libc/stdlib/labs.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: labs.3,v 1.7 2003/07/21 20:20:04 millert Exp $ 32.\" $OpenBSD: labs.3,v 1.8 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd May 14, 2003 34.Dd May 14, 2003
35.Dt LABS 3 35.Dt LABS 3
@@ -56,6 +56,7 @@ function returns the absolute value of the long long integer
56.Xr abs 3 , 56.Xr abs 3 ,
57.Xr cabs 3 , 57.Xr cabs 3 ,
58.Xr floor 3 , 58.Xr floor 3 ,
59.Xr imaxabs 3 ,
59.Xr math 3 60.Xr math 3
60.Sh STANDARDS 61.Sh STANDARDS
61The 62The
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3
index 63e1f2165c..951a5807e0 100644
--- a/src/lib/libc/stdlib/ldiv.3
+++ b/src/lib/libc/stdlib/ldiv.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: ldiv.3,v 1.7 2004/01/23 23:08:46 jmc Exp $ 32.\" $OpenBSD: ldiv.3,v 1.8 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 29, 1991 34.Dd June 29, 1991
35.Dt LDIV 3 35.Dt LDIV 3
@@ -56,6 +56,8 @@ and
56.Fa rem . 56.Fa rem .
57.Sh SEE ALSO 57.Sh SEE ALSO
58.Xr div 3 , 58.Xr div 3 ,
59.Xr imaxdiv 3 ,
60.Xr lldiv 3 ,
59.Xr math 3 , 61.Xr math 3 ,
60.Xr qdiv 3 62.Xr qdiv 3
61.Sh STANDARDS 63.Sh STANDARDS
diff --git a/src/lib/libc/stdlib/lldiv.3 b/src/lib/libc/stdlib/lldiv.3
new file mode 100644
index 0000000000..53bb3d35ae
--- /dev/null
+++ b/src/lib/libc/stdlib/lldiv.3
@@ -0,0 +1,67 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: lldiv.3,v 1.1 2006/01/13 17:58:09 millert Exp $
33.\"
34.Dd January 3, 2006
35.Dt LLDIV 3
36.Os
37.Sh NAME
38.Nm lldiv
39.Nd return quotient and remainder from division
40.Sh SYNOPSIS
41.Fd #include <stdlib.h>
42.Ft lldiv_t
43.Fn lldiv "long long num" "long long denom"
44.Sh DESCRIPTION
45The
46.Fn lldiv
47function computes the value
48.Fa num Ns / Ns Fa denom
49and returns the quotient and remainder in a structure named
50.Li lldiv_t
51that contains two
52.Li long long integer
53members named
54.Fa quot
55and
56.Fa rem .
57.Sh SEE ALSO
58.Xr div 3 ,
59.Xr imaxdiv 3 ,
60.Xr ldiv 3 ,
61.Xr math 3 ,
62.Xr qdiv 3
63.Sh STANDARDS
64The
65.Fn lldiv
66function conforms to
67.St -ansiC-99 .
diff --git a/src/lib/libc/stdlib/lldiv.c b/src/lib/libc/stdlib/lldiv.c
new file mode 100644
index 0000000000..e8d6c978b3
--- /dev/null
+++ b/src/lib/libc/stdlib/lldiv.c
@@ -0,0 +1,50 @@
1/* $OpenBSD: lldiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <stdlib.h> /* lldiv_t */
35
36lldiv_t
37lldiv(long long num, long long denom)
38{
39 lldiv_t r;
40
41 /* see div.c for comments */
42
43 r.quot = num / denom;
44 r.rem = num % denom;
45 if (num >= 0 && r.rem < 0) {
46 r.quot++;
47 r.rem -= denom;
48 }
49 return (r);
50}
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3
index 093212edf5..2a1ae0d202 100644
--- a/src/lib/libc/stdlib/qabs.3
+++ b/src/lib/libc/stdlib/qabs.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: qabs.3,v 1.6 2003/06/02 20:18:38 millert Exp $ 32.\" $OpenBSD: qabs.3,v 1.7 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 29, 1991 34.Dd June 29, 1991
35.Dt QABS 3 35.Dt QABS 3
@@ -48,6 +48,7 @@ function returns the absolute value of the quad integer
48.Fa j . 48.Fa j .
49.Sh SEE ALSO 49.Sh SEE ALSO
50.Xr abs 3 , 50.Xr abs 3 ,
51.Xr imaxabs 3 ,
51.Xr cabs 3 , 52.Xr cabs 3 ,
52.Xr floor 3 , 53.Xr floor 3 ,
53.Xr labs 3 , 54.Xr labs 3 ,
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3
index 5f17ec17d4..643672ea5b 100644
--- a/src/lib/libc/stdlib/qdiv.3
+++ b/src/lib/libc/stdlib/qdiv.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: qdiv.3,v 1.6 2004/01/23 23:08:46 jmc Exp $ 32.\" $OpenBSD: qdiv.3,v 1.7 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 29, 1991 34.Dd June 29, 1991
35.Dt QDIV 3 35.Dt QDIV 3
@@ -56,5 +56,7 @@ and
56.Fa rem . 56.Fa rem .
57.Sh SEE ALSO 57.Sh SEE ALSO
58.Xr div 3 , 58.Xr div 3 ,
59.Xr imaxdiv 3 ,
59.Xr ldiv 3 , 60.Xr ldiv 3 ,
61.Xr lldiv 3 ,
60.Xr math 3 62.Xr math 3
diff --git a/src/lib/libc/stdlib/strtoimax.c b/src/lib/libc/stdlib/strtoimax.c
new file mode 100644
index 0000000000..2c77f41650
--- /dev/null
+++ b/src/lib/libc/stdlib/strtoimax.c
@@ -0,0 +1,140 @@
1/* $OpenBSD: strtoimax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2
3/*-
4 * Copyright (c) 1992 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <ctype.h>
33#include <errno.h>
34#include <inttypes.h>
35
36/*
37 * Convert a string to an intmax_t
38 *
39 * Ignores `locale' stuff. Assumes that the upper and lower case
40 * alphabets and digits are each contiguous.
41 */
42intmax_t
43strtoimax(const char *nptr, char **endptr, int base)
44{
45 const char *s;
46 intmax_t acc, cutoff;
47 int c;
48 int neg, any, cutlim;
49
50 /*
51 * Skip white space and pick up leading +/- sign if any.
52 * If base is 0, allow 0x for hex and 0 for octal, else
53 * assume decimal; if base is already 16, allow 0x.
54 */
55 s = nptr;
56 do {
57 c = (unsigned char) *s++;
58 } while (isspace(c));
59 if (c == '-') {
60 neg = 1;
61 c = *s++;
62 } else {
63 neg = 0;
64 if (c == '+')
65 c = *s++;
66 }
67 if ((base == 0 || base == 16) &&
68 c == '0' && (*s == 'x' || *s == 'X')) {
69 c = s[1];
70 s += 2;
71 base = 16;
72 }
73 if (base == 0)
74 base = c == '0' ? 8 : 10;
75
76 /*
77 * Compute the cutoff value between legal numbers and illegal
78 * numbers. That is the largest legal value, divided by the
79 * base. An input number that is greater than this value, if
80 * followed by a legal input character, is too big. One that
81 * is equal to this value may be valid or not; the limit
82 * between valid and invalid numbers is then based on the last
83 * digit. For instance, if the range for intmax_t is
84 * [-9223372036854775808..9223372036854775807] and the input base
85 * is 10, cutoff will be set to 922337203685477580 and cutlim to
86 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
87 * accumulated a value > 922337203685477580, or equal but the
88 * next digit is > 7 (or 8), the number is too big, and we will
89 * return a range error.
90 *
91 * Set any if any `digits' consumed; make it negative to indicate
92 * overflow.
93 */
94 cutoff = neg ? INTMAX_MIN : INTMAX_MAX;
95 cutlim = cutoff % base;
96 cutoff /= base;
97 if (neg) {
98 if (cutlim > 0) {
99 cutlim -= base;
100 cutoff += 1;
101 }
102 cutlim = -cutlim;
103 }
104 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
105 if (isdigit(c))
106 c -= '0';
107 else if (isalpha(c))
108 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
109 else
110 break;
111 if (c >= base)
112 break;
113 if (any < 0)
114 continue;
115 if (neg) {
116 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
117 any = -1;
118 acc = INTMAX_MIN;
119 errno = ERANGE;
120 } else {
121 any = 1;
122 acc *= base;
123 acc -= c;
124 }
125 } else {
126 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
127 any = -1;
128 acc = INTMAX_MAX;
129 errno = ERANGE;
130 } else {
131 any = 1;
132 acc *= base;
133 acc += c;
134 }
135 }
136 }
137 if (endptr != 0)
138 *endptr = (char *) (any ? s - 1 : nptr);
139 return (acc);
140}
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3
index 3f5375c5e3..1ba936dd0d 100644
--- a/src/lib/libc/stdlib/strtol.3
+++ b/src/lib/libc/stdlib/strtol.3
@@ -29,27 +29,30 @@
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: strtol.3,v 1.13 2003/06/02 20:18:38 millert Exp $ 32.\" $OpenBSD: strtol.3,v 1.14 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 25, 1992 34.Dd January 3, 2006
35.Dt STRTOL 3 35.Dt STRTOL 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strtol , 38.Nm strtol ,
39.Nm strtoll , 39.Nm strtoll ,
40.Nm strtoq 40.Nm strtoimax ,
41.Nd "convert string value to a long or long long integer" 41.Nm strtoq ,
42.Nd "convert string value to a long, long long or intmax_t integer"
42.Sh SYNOPSIS 43.Sh SYNOPSIS
43.Fd #include <stdlib.h> 44.Fd #include <stdlib.h>
44.Fd #include <limits.h> 45.Fd #include <limits.h>
45.Ft long 46.Ft long
46.Fn strtol "const char *nptr" "char **endptr" "int base" 47.Fn strtol "const char *nptr" "char **endptr" "int base"
47.Pp 48.Pp
48.Fd #include <stdlib.h>
49.Fd #include <limits.h>
50.Ft long long 49.Ft long long
51.Fn strtoll "const char *nptr" "char **endptr" "int base" 50.Fn strtoll "const char *nptr" "char **endptr" "int base"
52.Pp 51.Pp
52.Fd #include <inttypes.h>
53.Ft intmax_t
54.Fn strtoimax "const char *nptr" "char **endptr" "int base"
55.Pp
53.Fd #include <sys/types.h> 56.Fd #include <sys/types.h>
54.Fd #include <stdlib.h> 57.Fd #include <stdlib.h>
55.Fd #include <limits.h> 58.Fd #include <limits.h>
@@ -71,6 +74,13 @@ to a
71.Li long long 74.Li long long
72value. 75value.
73The 76The
77.Fn strtoimax
78function converts the string in
79.Fa nptr
80to an
81.Li intmax_t
82value.
83The
74.Fn strtoq 84.Fn strtoq
75function is a deprecated equivalent of 85function is a deprecated equivalent of
76.Fn strtoll 86.Fn strtoll
@@ -98,7 +108,10 @@ is taken as 10 (decimal) unless the next character is
98in which case it is taken as 8 (octal). 108in which case it is taken as 8 (octal).
99.Pp 109.Pp
100The remainder of the string is converted to a 110The remainder of the string is converted to a
101.Li long 111.Li long ,
112.Li long long ,
113or
114.Li intmax_t ,
102value in the obvious manner, 115value in the obvious manner,
103stopping at the first character which is not a valid digit 116stopping at the first character which is not a valid digit
104in the given base. 117in the given base.
@@ -133,29 +146,25 @@ is
133on return, the entire string was valid.) 146on return, the entire string was valid.)
134.Sh RETURN VALUES 147.Sh RETURN VALUES
135The 148The
136.Fn strtol 149.Fn strtol ,
137function returns the result of the conversion, 150.Fn strtoll ,
151.Fn strtoimax ,
152and
153.Fn strtoq
154functions returns the result of the conversion,
138unless the value would underflow or overflow. 155unless the value would underflow or overflow.
139If an underflow occurs, 156If overflow or underflow occurs,
140.Fn strtol
141returns
142.Dv LONG_MIN .
143If an overflow occurs,
144.Fn strtol
145returns
146.Dv LONG_MAX .
147In both cases,
148.Va errno 157.Va errno
149is set to 158is set to
150.Er ERANGE . 159.Er ERANGE
151.Pp 160and the function return value is as follows:
152The 161.Bl -column -offset indent "strtoimax" "overflow" "underflow"
153.Fn strtoll 162.It Sy Function Ta Sy overflow Ta Sy underflow
154function has identical return values except that 163.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
155.Dv LLONG_MIN 164.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
156and 165.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
157.Dv LLONG_MAX 166.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
158are used to indicate underflow and overflow respectively. 167.El
159.Sh EXAMPLES 168.Sh EXAMPLES
160Ensuring that a string is a valid number (i.e., in range and containing no 169Ensuring that a string is a valid number (i.e., in range and containing no
161trailing characters) requires clearing 170trailing characters) requires clearing
@@ -233,9 +242,10 @@ The given string was out of range; the value converted has been clamped.
233.Xr strtoul 3 242.Xr strtoul 3
234.Sh STANDARDS 243.Sh STANDARDS
235The 244The
236.Fn strtol 245.Fn strtol ,
246.Fn strtoll ,
237and 247and
238.Fn strtoll 248.Fn strtoimax
239functions conform to 249functions conform to
240.St -ansiC-99 . 250.St -ansiC-99 .
241The 251The
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
index 4f6d6a51f9..2ea5e83297 100644
--- a/src/lib/libc/stdlib/strtoul.3
+++ b/src/lib/libc/stdlib/strtoul.3
@@ -29,27 +29,30 @@
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: strtoul.3,v 1.15 2003/06/02 20:18:38 millert Exp $ 32.\" $OpenBSD: strtoul.3,v 1.16 2006/01/13 17:58:09 millert Exp $
33.\" 33.\"
34.Dd June 25, 1992 34.Dd January 3, 2006
35.Dt STRTOUL 3 35.Dt STRTOUL 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strtoul , 38.Nm strtoul ,
39.Nm strtoull , 39.Nm strtoull ,
40.Nm strtoumax ,
40.Nm strtouq 41.Nm strtouq
41.Nd "convert a string to an unsigned long or unsigned long long integer" 42.Nd "convert a string to an unsigned long, unsigned long long or uintmax_t integer"
42.Sh SYNOPSIS 43.Sh SYNOPSIS
43.Fd #include <stdlib.h> 44.Fd #include <stdlib.h>
44.Fd #include <limits.h> 45.Fd #include <limits.h>
45.Ft unsigned long 46.Ft unsigned long
46.Fn strtoul "const char *nptr" "char **endptr" "int base" 47.Fn strtoul "const char *nptr" "char **endptr" "int base"
47.Pp 48.Pp
48.Fd #include <stdlib.h>
49.Fd #include <limits.h>
50.Ft unsigned long long 49.Ft unsigned long long
51.Fn strtoull "const char *nptr" "char **endptr" "int base" 50.Fn strtoull "const char *nptr" "char **endptr" "int base"
52.Pp 51.Pp
52.Fd #include <inttypes.h>
53.Ft uintmax_t
54.Fn strtoumax "const char *nptr" "char **endptr" "int base"
55.Pp
53.Fd #include <sys/types.h> 56.Fd #include <sys/types.h>
54.Fd #include <stdlib.h> 57.Fd #include <stdlib.h>
55.Fd #include <limits.h> 58.Fd #include <limits.h>
@@ -71,6 +74,13 @@ to an
71.Li unsigned long long 74.Li unsigned long long
72value. 75value.
73The 76The
77.Fn strtoumax
78function converts the string in
79.Fa nptr
80to a
81.Li umaxint_t
82value.
83The
74.Fn strtouq 84.Fn strtouq
75function is a deprecated equivalent of 85function is a deprecated equivalent of
76.Fn strtoull 86.Fn strtoull
@@ -139,32 +149,32 @@ is
139on return, the entire string was valid.) 149on return, the entire string was valid.)
140.Sh RETURN VALUES 150.Sh RETURN VALUES
141The 151The
142.Fn strtoul 152.Fn strtoul ,
143function returns the result of the conversion, 153.Fn strtoull ,
144unless the value would overflow, in which case 154.Fn strtoumax
145.Dv ULONG_MAX 155and
146is returned and 156.Fn strtouq
147.Va errno 157functions return either the result of the conversion or,
148is set to 158if there was a leading minus sign,
149.Er ERANGE . 159the negation of the result of the conversion,
150If there was a leading minus sign, 160unless the original (non-negated) value would overflow.
151.Fn strtoul 161If overflow occurs,
152returns the (unsigned) negation of the absolute value of the number, unless
153the absolute value would overflow.
154In this case,
155.Fn strtoul 162.Fn strtoul
156returns 163returns
157.Dv ULONG_MAX 164.Dv ULONG_MAX ,
158and sets the global variable
159.Va errno
160to
161.Er ERANGE .
162.Pp
163The
164.Fn strtoull 165.Fn strtoull
165function has identical return values except that 166returns
167.Dv ULLONG_MAX ,
168.Fn strtoumax
169returns
170.Dv UINTMAX_MAX ,
171.Fn strtouq
172returns
166.Dv ULLONG_MAX 173.Dv ULLONG_MAX
167is used to indicate overflow. 174and the global variable
175.Va errno
176is set to
177.Er ERANGE .
168.Pp 178.Pp
169There is no way to determine if 179There is no way to determine if
170.Fn strtoul 180.Fn strtoul
@@ -217,9 +227,10 @@ The given string was out of range; the value converted has been clamped.
217.Xr strtol 3 227.Xr strtol 3
218.Sh STANDARDS 228.Sh STANDARDS
219The 229The
220.Fn strtoul 230.Fn strtoul ,
231.Fn strtoull ,
221and 232and
222.Fn strtoull 233.Fn strtoumax
223functions conform to 234functions conform to
224.St -ansiC-99 . 235.St -ansiC-99 .
225The 236The
diff --git a/src/lib/libc/stdlib/strtoumax.c b/src/lib/libc/stdlib/strtoumax.c
new file mode 100644
index 0000000000..ce6e2c00f1
--- /dev/null
+++ b/src/lib/libc/stdlib/strtoumax.c
@@ -0,0 +1,102 @@
1/* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2
3/*-
4 * Copyright (c) 1992 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <ctype.h>
33#include <errno.h>
34#include <inttypes.h>
35
36/*
37 * Convert a string to a uintmax_t.
38 *
39 * Ignores `locale' stuff. Assumes that the upper and lower case
40 * alphabets and digits are each contiguous.
41 */
42uintmax_t
43strtoumax(const char *nptr, char **endptr, int base)
44{
45 const char *s;
46 uintmax_t acc, cutoff;
47 int c;
48 int neg, any, cutlim;
49
50 /*
51 * See strtoq for comments as to the logic used.
52 */
53 s = nptr;
54 do {
55 c = (unsigned char) *s++;
56 } while (isspace(c));
57 if (c == '-') {
58 neg = 1;
59 c = *s++;
60 } else {
61 neg = 0;
62 if (c == '+')
63 c = *s++;
64 }
65 if ((base == 0 || base == 16) &&
66 c == '0' && (*s == 'x' || *s == 'X')) {
67 c = s[1];
68 s += 2;
69 base = 16;
70 }
71 if (base == 0)
72 base = c == '0' ? 8 : 10;
73
74 cutoff = UINTMAX_MAX / (uintmax_t)base;
75 cutlim = UINTMAX_MAX % (uintmax_t)base;
76 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
77 if (isdigit(c))
78 c -= '0';
79 else if (isalpha(c))
80 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
81 else
82 break;
83 if (c >= base)
84 break;
85 if (any < 0)
86 continue;
87 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
88 any = -1;
89 acc = UINTMAX_MAX;
90 errno = ERANGE;
91 } else {
92 any = 1;
93 acc *= (uintmax_t)base;
94 acc += c;
95 }
96 }
97 if (neg && any > 0)
98 acc = -acc;
99 if (endptr != 0)
100 *endptr = (char *) (any ? s - 1 : nptr);
101 return (acc);
102}