Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src add strsuftoll(3) and strsuftollx(3) - parse a string for a ...



details:   https://anonhg.NetBSD.org/src/rev/ba8d7e006683
branches:  trunk
changeset: 539909:ba8d7e006683
user:      lukem <lukem%NetBSD.org@localhost>
date:      Fri Nov 29 12:58:14 2002 +0000

description:
add strsuftoll(3) and strsuftollx(3) - parse a string for a number
with an optional suffix, product (two numbers separated by `x'), and
provide range checking

diffstat:

 include/stdlib.h             |    8 +-
 lib/libc/include/namespace.h |    4 +-
 lib/libc/shlib_version       |    4 +-
 lib/libc/stdlib/Makefile.inc |   10 +-
 lib/libc/stdlib/strsuftoll.3 |  117 +++++++++++++++++++
 lib/libc/stdlib/strsuftoll.c |  254 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 389 insertions(+), 8 deletions(-)

diffs (truncated from 472 to 300 lines):

diff -r a7afcecbedd1 -r ba8d7e006683 include/stdlib.h
--- a/include/stdlib.h  Fri Nov 29 11:56:36 2002 +0000
+++ b/include/stdlib.h  Fri Nov 29 12:58:14 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stdlib.h,v 1.56 2002/05/24 04:01:43 itojun Exp $       */
+/*     $NetBSD: stdlib.h,v 1.57 2002/11/29 12:58:14 lukem Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -270,6 +270,12 @@
 quad_t  strtoq __P((const char * __restrict, char ** __restrict, int));
 u_quad_t strtouq __P((const char * __restrict, char ** __restrict, int));
 
+       /* LONGLONG */
+long long strsuftoll(const char *, const char *, long long, long long);
+       /* LONGLONG */
+long long strsuftollx(const char *, const char *, long long, long long,
+                       char *, size_t);
+
 int     l64a_r __P((long, char *, int));
 
 size_t shquote __P((const char *arg, char *buf, size_t bufsize));
diff -r a7afcecbedd1 -r ba8d7e006683 lib/libc/include/namespace.h
--- a/lib/libc/include/namespace.h      Fri Nov 29 11:56:36 2002 +0000
+++ b/lib/libc/include/namespace.h      Fri Nov 29 12:58:14 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: namespace.h,v 1.72 2002/11/08 00:13:29 fvdl Exp $      */
+/*     $NetBSD: namespace.h,v 1.73 2002/11/29 12:58:16 lukem Exp $     */
 
 /*-
  * Copyright (c) 1997-2002 The NetBSD Foundation, Inc.
@@ -436,6 +436,8 @@
 #define strptime               _strptime
 #define strsep                 _strsep
 #define strsignal              _strsignal
+#define strsuftoll             _strsuftoll
+#define strsuftollx            _strsuftollx
 #define strsvis                        _strsvis
 #define strsvisx               _strsvisx
 #define strtok_r               _strtok_r
diff -r a7afcecbedd1 -r ba8d7e006683 lib/libc/shlib_version
--- a/lib/libc/shlib_version    Fri Nov 29 11:56:36 2002 +0000
+++ b/lib/libc/shlib_version    Fri Nov 29 12:58:14 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: shlib_version,v 1.122 2002/11/08 00:13:51 fvdl Exp $
+#      $NetBSD: shlib_version,v 1.123 2002/11/29 12:58:16 lukem Exp $
 #      Remember to update distrib/sets/lists/base/shl.* when changing
 #
 # things we wish to do on next major version bump:
@@ -11,4 +11,4 @@
 # - libc/net/getaddrinfo.c, netdb.h: remove __ai_pad0
 #
 major=12
-minor=89
+minor=90
diff -r a7afcecbedd1 -r ba8d7e006683 lib/libc/stdlib/Makefile.inc
--- a/lib/libc/stdlib/Makefile.inc      Fri Nov 29 11:56:36 2002 +0000
+++ b/lib/libc/stdlib/Makefile.inc      Fri Nov 29 12:58:14 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.52 2001/04/28 15:41:30 kleink Exp $
+#      $NetBSD: Makefile.inc,v 1.53 2002/11/29 12:58:17 lukem Exp $
 #      from: @(#)Makefile.inc  8.3 (Berkeley) 2/4/95
 
 # stdlib sources
@@ -11,7 +11,8 @@
        hcreate.c heapsort.c jrand48.c \
        l64a.c lldiv.c lcong48.c lrand48.c malloc.c merge.c mrand48.c \
        nrand48.c putenv.c qabs.c qdiv.c qsort.c radixsort.c \
-       rand.c rand_r.c random.c seed48.c setenv.c srand48.c strtod.c \
+       rand.c rand_r.c random.c seed48.c setenv.c srand48.c \
+       strsuftoll.c strtod.c \
        strtoimax.c strtol.c strtoll.c strtoq.c strtoul.c strtoull.c \
        strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c
 
@@ -24,8 +25,8 @@
 MAN+=  a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
        bsearch.3 div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \
        hcreate.3 labs.3 ldiv.3 llabs.3 lldiv.3 malloc.3 memory.3 qabs.3 \
-       qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 strtod.3 \
-       strtol.3 strtoul.3 system.3 tsearch.3
+       qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 \
+       strsuftoll.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3
 
 MLINKS+=a64l.3 l64a.3
 MLINKS+=a64l.3 l64a_r.3
@@ -40,6 +41,7 @@
 MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
 MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3
 MLINKS+=radixsort.3 sradixsort.3
+MLINKS+=strsuftoll.3 strsuftollx.3
 MLINKS+=strtol.3 strtoimax.3
 MLINKS+=strtol.3 strtoll.3
 MLINKS+=strtol.3 strtoq.3
diff -r a7afcecbedd1 -r ba8d7e006683 lib/libc/stdlib/strsuftoll.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdlib/strsuftoll.3      Fri Nov 29 12:58:14 2002 +0000
@@ -0,0 +1,117 @@
+.\"    $NetBSD: strsuftoll.3,v 1.1 2002/11/29 12:58:17 lukem Exp $
+.\"
+.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Luke Mewburn.
+.\"
+.\" 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. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"        This product includes software developed by the NetBSD
+.\"        Foundation, Inc. and its contributors.
+.\" 4. Neither the name of The NetBSD Foundation 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 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.
+.\"
+.Dd November 29, 2002
+.Dt STRSUFTOLL 3
+.Os
+.Sh NAME
+.Nm strsuftoll ,
+.Nm strsuftollx
+.Nd "convert a string to an long long, with suffix parsing"
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.Fd #include \*[Lt]stdlib.h\*[Gt]
+.Ft long long
+.Fn strsuftoll "const char *desc" "const char *val" "long long min" "long long max"
+.Ft long long
+.Fn strsuftollx "const char *desc" "const char *val" "long long min" "long long max" "char *errbuf" "size_t errbuflen"
+.Sh DESCRIPTION
+The functions
+.Fn strsuftoll
+and
+.Fn strsuftollx
+convert
+.Fa val
+into a long long number,
+checking that the result is not smaller than
+.Fa min
+or larger than
+.Fa max .
+Two or more numbers may be separated by an
+.Dq x
+to indicate a product.
+Each number may have one of the following optional suffices:
+.Bl -tag -width 3n -offset indent -compact
+.It b
+Block; multiply by 512
+.It k
+Kilo; multiply by 1024 (1 KB)
+.It m
+Mega; multiply by 1048576 (1 MB)
+.It g
+Giga; multiply by 1073741824 (1 GB)
+.It t
+Tera; multiply by 1099511627776 (1 TB)
+.It w
+Word; multiply by the number of bytes in an integer
+.El
+.Pp
+In the case of an error (range overflow or an invalid number),
+.Fn strsuftollx
+places a error message into
+.Fa errbuf
+(which is
+.Fa errbuflen
+bytes long) and returns 0,
+and
+.Fn strsuftoll
+displays that error and terminates the process.
+.Sh RETURN VALUES
+The functions
+.Fn strsuftoll
+and
+.Fn strsuftollx
+return either the result of the conversion,
+unless the value overflows or is not a number;
+in the latter case,
+.Fn strsuftoll
+displays an error message and terminates the process with exit code 1,
+and
+.Fn strsuftollx
+returns with 0 and
+.Fa errbuf
+contains a non-empty error message.
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ERANGE
+The given string was out of range; the value converted has been clamped.
+.El
+.Sh SEE ALSO
+.Xr errx 3 ,
+.Xr strtoll 3
+.Sh BUGS
+Ignores the current locale.
diff -r a7afcecbedd1 -r ba8d7e006683 lib/libc/stdlib/strsuftoll.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdlib/strsuftoll.c      Fri Nov 29 12:58:14 2002 +0000
@@ -0,0 +1,254 @@
+/*     $NetBSD: strsuftoll.c,v 1.1 2002/11/29 12:58:17 lukem Exp $     */
+/*-
+ * Copyright (c) 2001-2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation 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 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.
+ */
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. 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.
+ */
+
+#include <sys/cdefs.h>
+
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: strsuftoll.c,v 1.1 2002/11/29 12:58:17 lukem Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#ifdef _LIBC
+#include "namespace.h"
+#endif



Home | Main Index | Thread Index | Old Index