Module Name: src Committed By: christos Date: Fri May 1 14:17:56 UTC 2015
Modified Files: src/common/lib/libc/stdlib: strtoi.c strtou.c src/distrib/sets/lists/debug: mi src/distrib/sets/lists/tests: mi src/lib/libc/include: namespace.h src/lib/libc/stdlib: Makefile.inc strtol.3 strtoul.3 src/tests/lib/libc/stdlib: Makefile Added Files: src/lib/libc/stdlib: strtoi.3 strtou.3 src/tests/lib/libc/stdlib: t_strtoi.c Log Message: - new test for strtoi - namespace protection for strto{i,u} - separate manpages for strto{i,u} from the ones for strto{u,}l From: Kamil Rytarowski To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/common/lib/libc/stdlib/strtoi.c \ src/common/lib/libc/stdlib/strtou.c cvs rdiff -u -r1.112 -r1.113 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.616 -r1.617 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.177 -r1.178 src/lib/libc/include/namespace.h cvs rdiff -u -r1.89 -r1.90 src/lib/libc/stdlib/Makefile.inc cvs rdiff -u -r0 -r1.1 src/lib/libc/stdlib/strtoi.3 \ src/lib/libc/stdlib/strtou.3 cvs rdiff -u -r1.31 -r1.32 src/lib/libc/stdlib/strtol.3 cvs rdiff -u -r1.29 -r1.30 src/lib/libc/stdlib/strtoul.3 cvs rdiff -u -r1.24 -r1.25 src/tests/lib/libc/stdlib/Makefile cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/stdlib/t_strtoi.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/common/lib/libc/stdlib/strtoi.c diff -u src/common/lib/libc/stdlib/strtoi.c:1.1 src/common/lib/libc/stdlib/strtoi.c:1.2 --- src/common/lib/libc/stdlib/strtoi.c:1.1 Fri Jan 16 13:35:28 2015 +++ src/common/lib/libc/stdlib/strtoi.c Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: strtoi.c,v 1.1 2015/01/16 18:35:28 christos Exp $ */ +/* $NetBSD: strtoi.c,v 1.2 2015/05/01 14:17:56 christos Exp $ */ /*- * Copyright (c) 2005 The DragonFly Project. All rights reserved. @@ -35,7 +35,11 @@ #endif #include <sys/cdefs.h> -__RCSID("$NetBSD: strtoi.c,v 1.1 2015/01/16 18:35:28 christos Exp $"); +__RCSID("$NetBSD: strtoi.c,v 1.2 2015/05/01 14:17:56 christos Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif #if defined(_KERNEL) #include <sys/param.h> @@ -57,6 +61,9 @@ __RCSID("$NetBSD: strtoi.c,v 1.1 2015/01 #define __TYPE intmax_t #define __WRAPPED strtoimax -#if !HAVE_STRTOI #include "_strtoi.h" + +#ifdef _LIBC +__weak_alias(strtoi, _strtoi) +__weak_alias(strtoi_l, _strtoi_l) #endif Index: src/common/lib/libc/stdlib/strtou.c diff -u src/common/lib/libc/stdlib/strtou.c:1.1 src/common/lib/libc/stdlib/strtou.c:1.2 --- src/common/lib/libc/stdlib/strtou.c:1.1 Fri Jan 16 13:35:28 2015 +++ src/common/lib/libc/stdlib/strtou.c Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: strtou.c,v 1.1 2015/01/16 18:35:28 christos Exp $ */ +/* $NetBSD: strtou.c,v 1.2 2015/05/01 14:17:56 christos Exp $ */ /*- * Copyright (c) 2005 The DragonFly Project. All rights reserved. @@ -35,7 +35,11 @@ #endif #include <sys/cdefs.h> -__RCSID("$NetBSD: strtou.c,v 1.1 2015/01/16 18:35:28 christos Exp $"); +__RCSID("$NetBSD: strtou.c,v 1.2 2015/05/01 14:17:56 christos Exp $"); + +#ifdef _LIBC +#include "namespace.h" +#endif #if defined(_KERNEL) #include <sys/param.h> @@ -57,6 +61,9 @@ __RCSID("$NetBSD: strtou.c,v 1.1 2015/01 #define __TYPE uintmax_t #define __WRAPPED strtoumax -#if !HAVE_STRTOU #include "_strtoi.h" + +#ifdef _LIBC +__weak_alias(strtou, _strtou) +__weak_alias(strtou_l, _strtou_l) #endif Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.112 src/distrib/sets/lists/debug/mi:1.113 --- src/distrib/sets/lists/debug/mi:1.112 Fri Apr 17 06:00:02 2015 +++ src/distrib/sets/lists/debug/mi Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.112 2015/04/17 10:00:02 pgoyette Exp $ +# $NetBSD: mi,v 1.113 2015/05/01 14:17:56 christos Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib @@ -1947,6 +1947,7 @@ ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_posix_memalign.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_random.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtod.debug tests-lib-debug debug,atf +./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtoi.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtol.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_strtox.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/libc/stdlib/t_system.debug tests-lib-debug debug,atf Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.616 src/distrib/sets/lists/tests/mi:1.617 --- src/distrib/sets/lists/tests/mi:1.616 Thu Apr 9 12:47:57 2015 +++ src/distrib/sets/lists/tests/mi Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.616 2015/04/09 16:47:57 ginsbach Exp $ +# $NetBSD: mi,v 1.617 2015/05/01 14:17:56 christos Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -2610,6 +2610,7 @@ ./usr/tests/lib/libc/stdlib/t_posix_memalign tests-lib-tests atf ./usr/tests/lib/libc/stdlib/t_random tests-lib-tests atf ./usr/tests/lib/libc/stdlib/t_strtod tests-lib-tests atf +./usr/tests/lib/libc/stdlib/t_strtoi tests-lib-tests atf ./usr/tests/lib/libc/stdlib/t_strtol tests-lib-tests atf ./usr/tests/lib/libc/stdlib/t_strtox tests-obsolete obsolete ./usr/tests/lib/libc/stdlib/t_system tests-lib-tests atf Index: src/lib/libc/include/namespace.h diff -u src/lib/libc/include/namespace.h:1.177 src/lib/libc/include/namespace.h:1.178 --- src/lib/libc/include/namespace.h:1.177 Tue Feb 17 15:29:21 2015 +++ src/lib/libc/include/namespace.h Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: namespace.h,v 1.177 2015/02/17 20:29:21 joerg Exp $ */ +/* $NetBSD: namespace.h,v 1.178 2015/05/01 14:17:56 christos Exp $ */ /*- * Copyright (c) 1997-2004 The NetBSD Foundation, Inc. @@ -65,12 +65,16 @@ #define strtod_l _strtod_l #define strtof _strtof #define strtof_l _strtof_l +#define strtoi _strtoi +#define strtoi_l _strtoi_l #define strtoimax _strtoimax #define strtoimax_l _strtoimax_l #define strtold _strtold #define strtold_l _strtold_l #define strtoll _strtoll #define strtoll_l _strtoll_l +#define strtou _strtou +#define strtou_l _strtou_l #define strtoull _strtoull #define strtoull_l _strtoull_l #define strtoumax _strtoumax Index: src/lib/libc/stdlib/Makefile.inc diff -u src/lib/libc/stdlib/Makefile.inc:1.89 src/lib/libc/stdlib/Makefile.inc:1.90 --- src/lib/libc/stdlib/Makefile.inc:1.89 Tue Feb 17 15:29:21 2015 +++ src/lib/libc/stdlib/Makefile.inc Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.89 2015/02/17 20:29:21 joerg Exp $ +# $NetBSD: Makefile.inc,v 1.90 2015/05/01 14:17:56 christos Exp $ # from: @(#)Makefile.inc 8.3 (Berkeley) 2/4/95 # stdlib sources @@ -53,8 +53,8 @@ MAN+= a64l.3 abort.3 abs.3 alloca.3 atex posix_memalign.3 posix_openpt.3 ptsname.3 \ qabs.3 qdiv.3 quick_exit.3 qsort.3 \ radixsort.3 rand48.3 rand.3 random.3 reallocarr.3 reallocarray.3 \ - strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtoul.3 strtonum.3 \ - system.3 \ + strfmon.3 strsuftoll.3 strtod.3 strtoi.3 strtol.3 strtou.3 strtoul.3 \ + strtonum.3 system.3 \ tsearch.3 \ unlockpt.3 @@ -88,9 +88,7 @@ MLINKS+=strtod.3 strtof.3 strtod.3 strto MLINKS+=strtol.3 strtoimax.3 MLINKS+=strtol.3 strtoll.3 MLINKS+=strtol.3 strtoq.3 -MLINKS+=strtol.3 strtoi.3 MLINKS+=strtoul.3 strtoull.3 MLINKS+=strtoul.3 strtoumax.3 MLINKS+=strtoul.3 strtouq.3 -MLINKS+=strtoul.3 strtou.3 MLINKS+=tsearch.3 tfind.3 tsearch.3 twalk.3 tsearch.3 tdelete.3 Index: src/lib/libc/stdlib/strtol.3 diff -u src/lib/libc/stdlib/strtol.3:1.31 src/lib/libc/stdlib/strtol.3:1.32 --- src/lib/libc/stdlib/strtol.3:1.31 Wed Mar 11 05:57:35 2015 +++ src/lib/libc/stdlib/strtol.3 Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -.\" $NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp $ +.\" $NetBSD: strtol.3,v 1.32 2015/05/01 14:17:56 christos Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -33,11 +33,10 @@ .\" .\" from: @(#)strtol.3 8.1 (Berkeley) 6/4/93 .\" -.Dd March 10, 2015 +.Dd April 30, 2015 .Dt STRTOL 3 .Os .Sh NAME -.Nm strtoi , .Nm strtol , .Nm strtoll , .Nm strtoimax , @@ -55,8 +54,6 @@ .Pp .In inttypes.h .Ft intmax_t -.Fn strtoi "const char * restrict nptr" "char ** restrict endptr" "int base" "intmax_t lo" "intmax_t hi" "int *rstatus" -.Ft intmax_t .Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base" .Pp .In sys/types.h @@ -90,30 +87,6 @@ to an .Ft intmax_t value. The -.Fn strtoi -function -uses internally -.Fn strtoimax -and ensures that the result is always in the range [ -.Fa lo .. -.Fa hi -]. -In adddition it always places -.Dv 0 -on success or a conversion status in the -.Fa rstatus -argument, avoiding the -.Dv errno -gymnastics the other functions require. -The -.Fn strtoi -function doesn't affect errno on exit. -The -.Fa rstatus -argument can be -.Dv NULL -if conversion status is to be ignored. -The .Fn strtoq function converts the string in @@ -121,6 +94,7 @@ converts the string in to a .Ft quad_t value. +.Pp The conversion is done according to the given .Fa base , which must be between 2 and 36 inclusive, @@ -146,11 +120,9 @@ is taken as 10 (decimal) unless the next .Ql 0 , in which case it is taken as 8 (octal). .Pp -The remainder of the string is converted to a -.Em long -value in the obvious manner, -stopping at the first character which is not a valid digit -in the given base. +The remainder of the string is converted to an appropriate value +in the obvious manner, +stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter .Ql A in either upper or lower case @@ -162,13 +134,10 @@ representing 35.) .Pp If .Fa endptr -is non-nil, -.Fn strtol -stores the address of the first invalid character in +is non-nil, the functions store the address of the first invalid character in .Fa *endptr . If there were no digits at all, however, -.Fn strtol -stores the original value of +the functions store the original value of .Fa nptr in .Fa *endptr . @@ -183,15 +152,6 @@ is on return, the entire string was valid.) .Sh RETURN VALUES The -.Fn strtoi -function -always returns the closest value in the range specified by -the -.Fa lo -and -.Fa hi -arguments. -The .Fn strtol function returns the result of the conversion, @@ -236,21 +196,6 @@ is left unchanged. This behavior (which is unlike most library functions) is guaranteed by the pertinent standards. .Sh EXAMPLES -The -.Fn strtoi -function is the simplest to use: -.Bd -literal -offset indent -int e; -intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e); -if (e) - warnc(e, "conversion of `%s' to a number failed, using %jd", - buf, lval); -.Ed -.Pp -This will always return a number in -.Dv [1..99] -range no matter what the input is, and warn if the conversion failed. -.Pp Because the return value of .Fn strtol cannot be used unambiguously to detect an error, @@ -320,24 +265,6 @@ is not between 2 and 36 and does not con .It Bq Er ERANGE The given string was out of range; the value converted has been clamped. .El -.Pp -In addition to the above errors -.Fn strtoi -returns: -.Bl -tag -width Er -.It Bq Er ECANCELED -The string did not contain any characters that were converted. -.It Bq Er ENOTSUP -The string contained non-numeric characters that did not get converted. -In this case, -.Fa endptr -points to the first unconverted character. -.It Bq Er ERANGE -The range given was invalid, i.e. -.Fa lo -\*[Gt] -.Fa hi . -.El .Sh SEE ALSO .Xr atof 3 , .Xr atoi 3 , @@ -360,9 +287,13 @@ and .Fn strtoimax functions conform to .St -isoC-99 . +.Pp The -.Fn strtoi -function appeared in -.Nx 8 . +.Fn strtoq +function is a +.Bx +legacy function equivalent to +.Fn strtoll +and should not be used in a new code. .Sh BUGS Ignores the current locale. Index: src/lib/libc/stdlib/strtoul.3 diff -u src/lib/libc/stdlib/strtoul.3:1.29 src/lib/libc/stdlib/strtoul.3:1.30 --- src/lib/libc/stdlib/strtoul.3:1.29 Tue Mar 10 09:00:58 2015 +++ src/lib/libc/stdlib/strtoul.3 Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -.\" $NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp $ +.\" $NetBSD: strtoul.3,v 1.30 2015/05/01 14:17:56 christos Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -33,11 +33,10 @@ .\" .\" from: @(#)strtoul.3 8.1 (Berkeley) 6/4/93 .\" -.Dd March 10, 2015 +.Dd April 30, 2015 .Dt STRTOUL 3 .Os .Sh NAME -.Nm strtou , .Nm strtoul , .Nm strtoull , .Nm strtoumax , @@ -55,8 +54,6 @@ .Pp .In inttypes.h .Ft uintmax_t -.Fn strtou "const char * restrict nptr" "char ** restrict endptr" "int base" "uintmax_t lo" "uintmax_t hi" "int *rstatus" -.Ft uintmax_t .Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base" .Pp .In sys/types.h @@ -89,26 +86,6 @@ converts the string in to an .Ft uintmax_t value. -.Fn strtou -function -uses internally -.Fn strtoumax -and ensures that the result is always in the range [ -.Fa lo .. -.Fa hi -]. -In adddition it always places -.Dv 0 -on success or a conversion status in the -.Fa rstatus -argument, avoiding the -.Dv errno -gymnastics the other functions require. -The -.Fa rstatus -argument can be -.Dv NULL -if conversion status is to be ignored. The .Fn strtouq function @@ -117,6 +94,7 @@ converts the string in to a .Ft u_quad_t value. +.Pp The conversion is done according to the given .Fa base , which must be between 2 and 36 inclusive, @@ -142,8 +120,7 @@ is taken as 10 (decimal) unless the next .Ql 0 , in which case it is taken as 8 (octal). .Pp -The remainder of the string is converted to an -.Em unsigned long +The remainder of the string is converted to an appropriate value in the obvious manner, stopping at the end of the string or at the first character that does not produce a valid digit @@ -159,13 +136,10 @@ representing 35.) .Pp If .Fa endptr -is non-nil, -.Fn strtoul -stores the address of the first invalid character in +is non-nil, the functions store the address of the first invalid character in .Fa *endptr . If there were no digits at all, however, -.Fn strtoul -stores the original value of +the functions store the original value of .Fa nptr in .Fa *endptr . @@ -180,15 +154,6 @@ is on return, the entire string was valid.) .Sh RETURN VALUES The -.Fn strtou -function -always returns the closest value in the range specified by -the -.Fa lo -and -.Fa hi -arguments. -The .Fn strtoul function returns either the result of the conversion @@ -233,21 +198,6 @@ is left unchanged. This behavior (which is unlike most library functions) is guaranteed by the pertinent standards. .Sh EXAMPLES -The -.Fn strtou -function is the simplest to use: -.Bd -literal -offset indent -int e; -uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e); -if (e) - warnc(e, "conversion of `%s' to a number failed, using %ju", - buf, lval); -.Ed -.Pp -This will always return a number in -.Dv [1..99] -range no matter what the input is, and warn if the conversion failed. -.Pp Because the return value of .Fn strtoul cannot be used unambiguously to detect an error, @@ -290,29 +240,17 @@ is not between 2 and 36 and does not con .It Bq Er ERANGE The given string was out of range; the value converted has been clamped. .El -.Pp -In addition to the above errors -.Fn strtou -returns: -.Bl -tag -width Er -.It Bq Er ECANCELED -The string did not contain any characters that were converted. -.It Bq Er ENOTSUP -The string contained non-numeric characters that did not get converted. -In this case, -.Fa endptr -points to the first unconverted character. -.It Bq Er ERANGE -The range given was invalid, i.e. -.Fa lo -\*[Gt] -.Fa hi . -.El .Sh SEE ALSO +.Xr atof 3 , +.Xr atoi 3 , +.Xr atol 3 , +.Xr atoll 3 , +.Xr strtod 3 , .Xr strtoi 3 , -.Xr strtoimax 3 , .Xr strtol 3 , -.Xr strtoll 3 +.Xr strtoll 3 , +.Xr strtoimax 3 , +.Xr strtou 3 , .Sh STANDARDS The .Fn strtoul @@ -325,9 +263,13 @@ and .Fn strtoumax functions conform to .St -isoC-99 . +.Pp The -.Fn strtou -function appeared in -.Nx 8 . +.Fn strtouq +function is a +.Bx +legacy function equivalent to +.Fn strtoull +and should not be used in a new code. .Sh BUGS Ignores the current locale. Index: src/tests/lib/libc/stdlib/Makefile diff -u src/tests/lib/libc/stdlib/Makefile:1.24 src/tests/lib/libc/stdlib/Makefile:1.25 --- src/tests/lib/libc/stdlib/Makefile:1.24 Sat Dec 27 13:03:41 2014 +++ src/tests/lib/libc/stdlib/Makefile Fri May 1 10:17:56 2015 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.24 2014/12/27 18:03:41 martin Exp $ +# $NetBSD: Makefile,v 1.25 2015/05/01 14:17:56 christos Exp $ .include <bsd.own.mk> @@ -16,6 +16,7 @@ TESTS_C+= t_posix_memalign TESTS_C+= t_random TESTS_C+= t_strtod TESTS_C+= t_strtol +TESTS_C+= t_strtoi TESTS_C+= t_system TESTS_SH+= t_atexit Added files: Index: src/lib/libc/stdlib/strtoi.3 diff -u /dev/null src/lib/libc/stdlib/strtoi.3:1.1 --- /dev/null Fri May 1 10:17:56 2015 +++ src/lib/libc/stdlib/strtoi.3 Fri May 1 10:17:56 2015 @@ -0,0 +1,223 @@ +.\" $NetBSD: strtoi.3,v 1.1 2015/05/01 14:17:56 christos Exp $ +.\" +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Chris Torek and the American National Standards Committee X3, +.\" on Information Processing Systems. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" from: @(#)strtol.3 8.1 (Berkeley) 6/4/93 +.\" +.\" Created by Kamil Rytarowski, based on ID: +.\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp +.\" +.Dd April 30, 2015 +.Dt STRTOI 3 +.Os +.Sh NAME +.Nm strtoi +.Nd convert string value to an intmax_t integer +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In inttypes.h +.Ft intmax_t +.Fo strtoi +.Fa "const char * restrict nptr" +.Fa "char ** restrict endptr" +.Fa "int base" +.Fa "intmax_t lo" +.Fa "intmax_t hi" +.Fa "int *rstatus" +.Sh DESCRIPTION +The +.Fn strtoi +function +converts the string in +.Fa nptr +to an +.Ft intmax_t +value. +The +.Fn strtoi +function uses internally +.Xr strtoimax 3 +and ensures that the result is always in the range [ +.Fa lo .. +.Fa hi +]. +In adddition it always places +.Dv 0 +on success or a conversion status in the +.Fa rstatus +argument, avoiding the +.Dv errno +gymnastics the other functions require. +The +.Fa rstatus +argument can be +.Dv NULL +if conversion status is to be ignored. +.Pp +The string may begin with an arbitrary amount of white space +(as determined by +.Xr isspace 3 ) +followed by a single optional +.Ql + +or +.Ql - +sign. +If +.Fa base +is zero or 16, +the string may then include a +.Ql 0x +prefix, +and the number will be read in base 16; otherwise, a zero +.Fa base +is taken as 10 (decimal) unless the next character is +.Ql 0 , +in which case it is taken as 8 (octal). +.Pp +The remainder of the string is converted to a +.Em intmax_t +value in the obvious manner, +stopping at the first character which is not a valid digit +in the given base. +(In bases above 10, the letter +.Ql A +in either upper or lower case +represents 10, +.Ql B +represents 11, and so forth, with +.Ql Z +representing 35.) +.Pp +If +.Fa endptr +is non-nil, +.Fn strtoi +stores the address of the first invalid character in +.Fa *endptr . +If there were no digits at all, however, +.Fn strtoi +stores the original value of +.Fa nptr +in +.Fa *endptr . +(Thus, if +.Fa *nptr +is not +.Ql \e0 +but +.Fa **endptr +is +.Ql \e0 +on return, the entire string was valid.) +.Sh RETURN VALUES +The +.Fn strtoi +function +always returns the closest value in the range specified by +the +.Fa lo +and +.Fa hi +arguments. +.Pp +The +.Va errno +value is guaranteed to be left unchanged. +.Pp +Errors are stored as the conversion status in the +.Fa rstatus +argument. +.Sh EXAMPLES +The following example will always return a number in +.Dv [1..99] +range no matter what the input is, and warn if the conversion failed. +.Pp +.Bd -literal -offset indent +int e; +intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e); +if (e) + warnc(e, "conversion of `%s' to a number failed, using %jd", + buf, lval); +.Ed +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Ar base +is not between 2 and 36 and does not contain the special value 0. +.It Bq Er ERANGE +The given string was out of range; the value converted has been clamped. +.It Bq Er ECANCELED +The string did not contain any characters that were converted. +.It Bq Er ENOTSUP +The string contained non-numeric characters that did not get converted. +In this case, +.Fa endptr +points to the first unconverted character. +.It Bq Er ERANGE +The range given was invalid, i.e. +.Fa lo +\*[Gt] +.Fa hi . +.El +.Sh SEE ALSO +.Xr atof 3 , +.Xr atoi 3 , +.Xr atol 3 , +.Xr atoll 3 , +.Xr strtod 3 , +.Xr strtol 3 , +.Xr strtoll 3 , +.Xr strtoimax 3 , +.Xr strtou 3 , +.Xr strtoul 3 , +.Xr strtoull 3 , +.Xr strtoumax 3 +.Sh STANDARDS +The +.Fn strtoi +function is a +.Nx +extension. +.Sh HISTORY +The +.Fn strtoi +function first appeared in +.Nx 7 . +.Ox +introduced the +.Fn strtonum 3 +function for the same purpose, but the interface makes it impossible to +properly differentiate illegal returns. +.Sh BUGS +Ignores the current locale. Index: src/lib/libc/stdlib/strtou.3 diff -u /dev/null src/lib/libc/stdlib/strtou.3:1.1 --- /dev/null Fri May 1 10:17:56 2015 +++ src/lib/libc/stdlib/strtou.3 Fri May 1 10:17:56 2015 @@ -0,0 +1,222 @@ +.\" $NetBSD: strtou.3,v 1.1 2015/05/01 14:17:56 christos Exp $ +.\" +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Chris Torek and the American National Standards Committee X3, +.\" on Information Processing Systems. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" from: @(#)strtoul.3 8.1 (Berkeley) 6/4/93 +.\" +.\" Created by Kamil Rytarowski, based on ID: +.\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp +.\" +.Dd April 30, 2015 +.Dt STRTOU 3 +.Os +.Sh NAME +.Nm strtou +.Nd convert a string to an uintmax_t integer +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In inttypes.h +.Ft uintmax_t +.Fo strtou +.Fa "const char * restrict nptr" +.Fa "char ** restrict endptr" +.Fa "int base" +.Fa "uintmax_t lo" +.Fa "uintmax_t hi" +.Fa "int *rstatus" +.Sh DESCRIPTION +The +.Fn strtou +function converts the string in +.Fa nptr +to an +.Ft uintmax_t +value. +The +.Fn strtou +function uses internally +.Xr strtoumax 3 +and ensures that the result is always in the range [ +.Fa lo .. +.Fa hi +]. +In adddition it always places +.Dv 0 +on success or a conversion status in the +.Fa rstatus +argument, avoiding the +.Dv errno +gymnastics the other functions require. +The +.Fa rstatus +argument can be +.Dv NULL +if conversion status is to be ignored. +.Pp +The string may begin with an arbitrary amount of white space +(as determined by +.Xr isspace 3 ) +followed by a single optional +.Ql + +or +.Ql - +sign. +If +.Fa base +is zero or 16, +the string may then include a +.Ql 0x +prefix, +and the number will be read in base 16; otherwise, a zero +.Fa base +is taken as 10 (decimal) unless the next character is +.Ql 0 , +in which case it is taken as 8 (octal). +.Pp +The remainder of the string is converted to an +.Em uintmax_t +value in the obvious manner, +stopping at the end of the string +or at the first character that does not produce a valid digit +in the given base. +(In bases above 10, the letter +.Ql A +in either upper or lower case +represents 10, +.Ql B +represents 11, and so forth, with +.Ql Z +representing 35.) +.Pp +If +.Fa endptr +is non-nil, +.Fn strtou +stores the address of the first invalid character in +.Fa *endptr . +If there were no digits at all, however, +.Fn strtou +stores the original value of +.Fa nptr +in +.Fa *endptr . +(Thus, if +.Fa *nptr +is not +.Ql \e0 +but +.Fa **endptr +is +.Ql \e0 +on return, the entire string was valid.) +.Sh RETURN VALUES +The +.Fn strtou +function +always returns the closest value in the range specified by +the +.Fa lo +and +.Fa hi +arguments. +.Pp +The +.Va errno +value is guaranteed to be left unchanged. +.Pp +Errors are stored as the conversion status in the +.Fa rstatus +argument. +.Sh EXAMPLES +The following example will always return a number in +.Dv [1..99] +range no matter what the input is, and warn if the conversion failed. +.Bd -literal -offset indent +int e; +uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e); +if (e) + warnc(e, "conversion of `%s' to a number failed, using %ju", + buf, lval); +.Ed +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EINVAL +The +.Ar base +is not between 2 and 36 and does not contain the special value 0. +.It Bq Er ERANGE +The given string was out of range; the value converted has been clamped. +.It Bq Er ECANCELED +The string did not contain any characters that were converted. +.It Bq Er ENOTSUP +The string contained non-numeric characters that did not get converted. +In this case, +.Fa endptr +points to the first unconverted character. +.It Bq Er ERANGE +The range given was invalid, i.e. +.Fa lo +\*[Gt] +.Fa hi . +.El +.Sh SEE ALSO +.Xr atof 3 , +.Xr atoi 3 , +.Xr atol 3 , +.Xr atoll 3 , +.Xr strtod 3 , +.Xr strtoi 3 , +.Xr strtol 3 , +.Xr strtoll 3 , +.Xr strtoimax 3 , +.Xr strtoul 3 , +.Xr strtoull 3 , +.Xr strtoumax 3 +.Sh STANDARDS +The +.Fn strtou +function is a +.Nx +extension. +.Sh HISTORY +The +.Fn strtou +function first appeared in +.Nx 7 . +.Ox +introduced the +.Fn strtonum 3 +function for the same purpose, but the interface makes it impossible to +properly differentiate illegal returns. +.Sh BUGS +Ignores the current locale. Index: src/tests/lib/libc/stdlib/t_strtoi.c diff -u /dev/null src/tests/lib/libc/stdlib/t_strtoi.c:1.1 --- /dev/null Fri May 1 10:17:56 2015 +++ src/tests/lib/libc/stdlib/t_strtoi.c Fri May 1 10:17:56 2015 @@ -0,0 +1,304 @@ +/* $NetBSD: t_strtoi.c,v 1.1 2015/05/01 14:17:56 christos Exp $ */ + +/*- + * Copyright (c) 2015 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jukka Ruohonen. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created by Kamil Rytarowski, vesed on ID: + * NetBSD: t_strtol.c,v 1.5 2011/06/14 02:45:58 jruoho Exp + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_strtoi.c,v 1.1 2015/05/01 14:17:56 christos Exp $"); + +#include <atf-c.h> +#include <errno.h> +#include <inttypes.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> + +struct test { + const char *str; + intmax_t res; + int base; + const char *end; + intmax_t lo; + intmax_t hi; + int rstatus; +}; + +static void check(struct test *, intmax_t, char *, int); + +static void +check(struct test *t, intmax_t rv, char *end, int rstatus) +{ + + if (rv != t->res) + atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)" + " failed (rv = %jd)", t->str, t->base, t->lo, t->hi, rv); + + if (rstatus != t->rstatus) + atf_tc_fail_nonfatal("strtoi(%s, &end, %d, %jd, %jd, &rstatus)" + " failed (rstatus: %d ('%s'))", + t->str, t->base, t->lo, t->hi, rstatus, strerror(rstatus)); + + if ((t->end != NULL && strcmp(t->end, end) != 0) || + (t->end == NULL && *end != '\0')) + atf_tc_fail_nonfatal("invalid end pointer ('%s') from " + "strtoi(%s, &end, %d, %jd, %jd, &rstatus)", + end, t->str, t->base, t->lo, t->hi); +} + +ATF_TC(strtoi_base); +ATF_TC_HEAD(strtoi_base, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test strtoi(3) with different bases"); +} + +ATF_TC_BODY(strtoi_base, tc) +{ + struct test t[] = { + { "123456789", 123456789, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "111010110111100110100010101",123456789, 2, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "22121022020212200", 123456789, 3, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "13112330310111", 123456789, 4, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "223101104124", 123456789, 5, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "20130035113", 123456789, 6, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "3026236221", 123456789, 7, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "726746425", 123456789, 8, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "277266780", 123456789, 9, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "123456789", 123456789, 10, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "63762A05", 123456789, 11, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "35418A99", 123456789, 12, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "1C767471", 123456789, 13, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "12579781", 123456789, 14, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "AC89BC9", 123456789, 15, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "75BCD15", 123456789, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "1234567", 342391, 8, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "01234567", 342391, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "0123456789", 123456789, 10, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "0x75bcd15", 123456789, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + }; + + intmax_t rv; + char *end; + int e; + size_t i; + + for (i = 0; i < __arraycount(t); i++) { + + errno = 0; + rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e); + + if (errno != 0) + atf_tc_fail("strtoi(3) changed errno to %d ('%s')", + e, strerror(e)); + + check(&t[i], rv, end, e); + } +} + +ATF_TC(strtoi_case); +ATF_TC_HEAD(strtoi_case, tc) +{ + atf_tc_set_md_var(tc, "descr", "Case insensitivity with strtoi(3)"); +} + +ATF_TC_BODY(strtoi_case, tc) +{ + struct test t[] = { + { "abcd", 0xabcd, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { " dcba", 0xdcba, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "abcd dcba", 0xabcd, 16, " dcba", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + { "abc0x123", 0xabc0, 16, "x123", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + { "abcd\0x123", 0xabcd, 16, "\0x123", + INTMAX_MIN, INTMAX_MAX, 0 }, + { "ABCD", 0xabcd, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "aBcD", 0xabcd, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "0xABCD", 0xabcd, 16, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "0xABCDX", 0xabcd, 16, "X", + INTMAX_MIN, INTMAX_MAX, ENOTSUP}, + }; + + intmax_t rv; + char *end; + int e; + size_t i; + + for (i = 0; i < __arraycount(t); i++) { + + errno = 0; + rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e); + + if (errno != 0) + atf_tc_fail("strtoi(3) changed errno to %d ('%s')", + e, strerror(e)); + + check(&t[i], rv, end, e); + } +} + +ATF_TC(strtoi_range); +ATF_TC_HEAD(strtoi_range, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3)"); +} + +ATF_TC_BODY(strtoi_range, tc) +{ + struct test t[] = { +#if INTMAX_MAX == 0x7fffffffffffffff + { "1000000000000000000000", INTMAX_MAX, 8, NULL, + INTMAX_MIN, INTMAX_MAX, ERANGE }, + { "9223372036854775808", INTMAX_MAX, 10, NULL, + INTMAX_MIN, INTMAX_MAX, ERANGE }, + { "8000000000000000", INTMAX_MAX, 16, NULL, + INTMAX_MIN, INTMAX_MAX, ERANGE }, +#else +#error extend this test to your platform! +#endif + { "10", 1, 10, NULL, + -1, 1, ERANGE }, + { "10", 11, 10, NULL, + 11, 20, ERANGE }, + }; + + intmax_t rv; + char *end; + int e; + size_t i; + + for (i = 0; i < __arraycount(t); i++) { + + errno = 0; + rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e); + + if (errno != 0) + atf_tc_fail("strtoi(3) changed errno to %d ('%s')", + e, strerror(e)); + + check(&t[i], rv, end, e); + } +} + +ATF_TC(strtoi_signed); +ATF_TC_HEAD(strtoi_signed, tc) +{ + atf_tc_set_md_var(tc, "descr", "A basic test of strtoi(3)"); +} + +ATF_TC_BODY(strtoi_signed, tc) +{ + struct test t[] = { + { "1", 1, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { " 2", 2, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { " 3", 3, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { " -3", -3, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "--1", 0, 0, "--1", + INTMAX_MIN, INTMAX_MAX, ECANCELED }, + { "+-2", 0, 0, "+-2", + INTMAX_MIN, INTMAX_MAX, ECANCELED }, + { "++3", 0, 0, "++3", + INTMAX_MIN, INTMAX_MAX, ECANCELED }, + { "+9", 9, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "+123", 123, 0, NULL, + INTMAX_MIN, INTMAX_MAX, 0 }, + { "-1 3", -1, 0, " 3", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + { "-1.3", -1, 0, ".3", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + { "- 3", 0, 0, "- 3", + INTMAX_MIN, INTMAX_MAX, ECANCELED }, + { "+33.", 33, 0, ".", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + { "30x0", 30, 0, "x0", + INTMAX_MIN, INTMAX_MAX, ENOTSUP }, + }; + + intmax_t rv; + char *end; + int e; + size_t i; + + for (i = 0; i < __arraycount(t); i++) { + + errno = 0; + rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e); + + if (errno != 0) + atf_tc_fail("strtoi(3) changed errno to %d ('%s')", + e, strerror(e)); + + check(&t[i], rv, end, e); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, strtoi_base); + ATF_TP_ADD_TC(tp, strtoi_case); + ATF_TP_ADD_TC(tp, strtoi_range); + ATF_TP_ADD_TC(tp, strtoi_signed); + + return atf_no_error(); +}