standards/51209: [PATCH] add a64l()/l64a/l64a_r functions (obtained from NetBSD)

Sergey A.Osokin osa at FreeBSD.org.ru
Mon Apr 21 04:00:20 PDT 2003


>Number:         51209
>Category:       standards
>Synopsis:       [PATCH] add a64l()/l64a/l64a_r functions (obtained from NetBSD)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 21 04:00:18 PDT 2003
>Closed-Date:
>Last-Modified:
>Originator:     Sergey A. Osokin
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
n/a
>Environment:
System: FreeBSD 5.0-CURRENT i386

>Description:
	add a64l()/l64a/l64a_r functions (obtained from NetBSD)
	From SUSv3 documentation:
	Issue 5: Moved from X/OPEN UNIX extension to BASE.

>How-To-Repeat:
	
>Fix:

Index: src/lib/libc/stdlib/Makefile.inc
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/Makefile.inc,v
retrieving revision 1.45
diff -u -r1.45 Makefile.inc
--- src/lib/libc/stdlib/Makefile.inc	5 Apr 2003 07:33:46 -0000	1.45
+++ src/lib/libc/stdlib/Makefile.inc	21 Apr 2003 09:52:58 -0000
@@ -4,26 +4,28 @@
 # machine-independent stdlib sources
 .PATH: ${.CURDIR}/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib
 
-MISRCS+=_Exit.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
+MISRCS+=_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
 	bsearch.c calloc.c div.c exit.c getenv.c getopt.c getopt_long.c \
 	getsubopt.c grantpt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c \
-	insque.c labs.c ldiv.c llabs.c lldiv.c lsearch.c malloc.c merge.c \
-	putenv.c qsort.c qsort_r.c radixsort.c rand.c random.c reallocf.c \
-	realpath.c remque.c setenv.c strfmon.c strhash.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
+	insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c malloc.c \
+	merge.c putenv.c qsort.c qsort_r.c radixsort.c rand.c random.c \
+	reallocf.c realpath.c remque.c setenv.c strfmon.c strhash.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
 
 # machine-dependent stdlib sources
 .if exists(${.CURDIR}/${MACHINE_ARCH}/stdlib/Makefile.inc)
 .include "${.CURDIR}/${MACHINE_ARCH}/stdlib/Makefile.inc"
 .endif
 
-MAN+=	abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
-	div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 grantpt.3 \
-	hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \
-	lsearch.3 malloc.3 memory.3 qsort.3 radixsort.3 rand.3 random.3 \
-	realpath.3 strfmon.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3
+MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 \
+	bsearch.3 div.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
+	getsubopt.3 grantpt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 \
+	labs.3 ldiv.3 llabs.3 lldiv.3 lsearch.3 malloc.3 memory.3 qsort.3 \
+	radixsort.3 rand.3 random.3 realpath.3 strfmon.3 strtod.3 strtol.3 \
+	strtoul.3 system.3 tsearch.3
 
+MLINKS+=a64l.3 l64a.3 l64a_r.3
 MLINKS+=atol.3 atoll.3
 MLINKS+=exit.3 _Exit.3
 MLINKS+=getenv.3 putenv.3 getenv.3 setenv.3 getenv.3 unsetenv.3
--- /dev/null	Mon Apr 21 03:02:32 2003
+++ src/lib/libc/stdlib/a64l.3	Wed Apr 16 17:34:45 2003
@@ -0,0 +1,140 @@
+.\" $NetBSD: a64l.3,v 1.8 2003/04/16 13:34:45 wiz Exp $
+.\"
+.\" Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Klaus Klein.
+.\"
+.\" 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.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd April 21, 2003
+.Dt A64L 3
+.Os
+.Sh NAME
+.Nm a64l ,
+.Nm l64a ,
+.Nm l64a_r
+.Nd "convert between a long integer and a base-64 ASCII string"
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft long
+.Fn a64l "const char *s"
+.Ft char *
+.Fn l64a "long int l"
+.Ft int
+.Fn l64a_r "long int l" "char *buffer" "int buflen"
+.Sh DESCRIPTION
+The
+.Fn a64l
+and
+.Fn l64a
+functions convert between a long integer and its base-64 ASCII string
+representation.
+.Pp
+The characters used to represent ``digits'' are
+`.' for 0,
+`/' for 1,
+`0' - `9' for 2 - 11,
+`A' - `Z' for 12 - 37, and
+`a' - `z' for 38 - 63.
+.Pp
+.Fn a64l
+takes a pointer to a NUL-terminated base-64 ASCII string
+representation,
+.Fa s ,
+and returns the corresponding long integer value.
+.Pp
+.Fn l64a
+takes a long integer value,
+.Fa l ,
+and returns a pointer to the corresponding NUL-terminated base-64
+ASCII string representation.
+.Pp
+.Fn l64a_r
+performs a conversion identical to that of
+.Fn l64a
+and stores the resulting representation in the memory area pointed to by
+.Fa buffer ,
+consuming at most
+.Fa buflen
+characters including the terminating NUL character.
+.Sh RETURN VALUES
+On successful completion,
+.Fn a64l
+returns the long integer value corresponding to the input string.
+If the string pointed to by
+.Fa s
+is an empty string,
+.Fn a64l
+returns a value of 0L.
+.Pp
+.Fn l64a
+returns a pointer to the base-64 ASCII string representation corresponding to
+the input value.
+If
+.Fa l
+is 0L,
+.Fn l64a
+returns a pointer to an empty string.
+.Pp
+On successful completion,
+.Fn l64a_r
+returns 0; if
+.Fa buffer
+is of insufficient length, -1 is returned.
+.Sh SEE ALSO
+.Xr strtol 3
+.Sh STANDARDS
+The
+.Fn a64l
+and
+.Fn l64a
+functions conform to
+.St -xpg4.2 .
+The
+.Fn l64a_r
+function conforms to
+.St -svid4 ,
+Multithreading Extension.
+.Sh BUGS
+The
+.Fn l64a
+function is not reentrant.
+The value returned by it points into a static buffer area;
+subsequent calls to
+.Fn la64a
+may overwrite this buffer.
+In multi-threaded applications,
+.Fn l64a_r
+should be used instead.
--- /dev/null	Mon Apr 21 03:02:32 2003
+++ src/lib/libc/stdlib/a64l.c	Mon Apr 21 12:59:19 2003
@@ -0,0 +1,48 @@
+/*
+ * Written by J.T. Conklin <jtc at netbsd.org>.
+ * Public domain.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: a64l.c,v 1.8 2000/01/22 22:19:19 mycroft Exp $");
+#endif /* LIBC_SCCS and not lint */
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+
+#include <assert.h>
+#include <limits.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+#ifndef _DIAGASSERT
+#define _DIAGASSERT(X)
+#endif
+
+long
+a64l(const char *s)
+{
+	long value, digit, shift;
+	int i;
+
+	_DIAGASSERT(s != NULL);
+
+	value = 0;
+	shift = 0;
+	for (i = 0; *s && i < 6; i++, s++) {
+		if (*s <= '/')
+			digit = *s - '.';
+		else if (*s <= '9')
+			digit = *s - '0' + 2;
+		else if (*s <= 'Z')
+			digit = *s - 'A' + 12;
+		else
+			digit = *s - 'a' + 38; 
+
+		value |= digit << shift;
+		shift += 6;
+	}
+
+	return (value);
+}
--- /dev/null	Mon Apr 21 03:02:32 2003
+++ src/lib/libc/stdlib/l64a.c	Mon Apr 21 13:43:51 2003
@@ -0,0 +1,61 @@
+/*
+ * Written by J.T. Conklin <jtc at netbsd.org>.
+ * Public domain.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: l64a.c,v 1.12 2000/01/22 22:19:19 mycroft Exp $");
+#endif /* LIBC_SCCS and not lint */
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#ifndef _DIAGASSERT
+#define _DIAGASSERT(X)
+#endif
+
+char *
+l64a (long value)
+{
+	static char buf[8];
+
+	(void)l64a_r(value, buf, sizeof (buf));
+	return buf;
+}
+
+int
+l64a_r (long value, char *buffer, int buflen)
+{
+	char *s = buffer;
+	int digit;
+	unsigned long v = value;
+
+	_DIAGASSERT(buffer != NULL);
+
+	if (value == 0UL) 
+		goto out;
+
+	for (; v != 0 && buflen > 1; s++, buflen--) {
+		digit = (int)(v & 0x3f);
+
+		if (digit < 2) 
+			*s = digit + '.';
+		else if (digit < 12)
+			*s = digit + '0' - 2;
+		else if (digit < 38)
+			*s = digit + 'A' - 12;
+		else
+			*s = digit + 'a' - 38;
+		v >>= 6;
+	}
+
+out:
+	*s = '\0';
+
+	return (v == 0UL ? 0 : -1);
+}
Index: src/include/stdlib.h
===================================================================
RCS file: /home/ncvs/src/include/stdlib.h,v
retrieving revision 1.48
diff -u -r1.48 stdlib.h
--- src/include/stdlib.h	12 Mar 2003 20:29:58 -0000	1.48
+++ src/include/stdlib.h	21 Apr 2003 10:11:03 -0000
@@ -174,7 +174,7 @@
  */
 #if __XSI_VISIBLE
 /* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
-/* long	 a64l(const char *); */
+long	 a64l(const char *);
 double	 drand48(void);
 /* char	*ecvt(double, int, int * __restrict, int * __restrict); */
 double	 erand48(unsigned short[3]);
@@ -187,7 +187,7 @@
 int	 grantpt(int);
 char	*initstate(unsigned long /* XSI requires u_int */, char *, long);
 long	 jrand48(unsigned short[3]);
-/* char	*l64a(long); */
+char	*l64a(long);
 void	 lcong48(unsigned short[7]);
 long	 lrand48(void);
 #ifndef _MKSTEMP_DECLARED
@@ -247,6 +247,7 @@
 	 getprogname(void);
 
 int	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
+int	 l64a_r (long, char *, int);
 int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
 void	 qsort_r(void *, size_t, size_t, void *,
 	    int (*)(void *, const void *, const void *));
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-standards mailing list