Home | History | Annotate | Line # | Download | only in stdlib
      1 /*	$NetBSD: l64a.c,v 1.14 2012/03/13 21:13:48 christos Exp $	*/
      2 
      3 /*
      4  * Written by J.T. Conklin <jtc (at) NetBSD.org>.
      5  * Public domain.
      6  */
      7 
      8 #include <sys/cdefs.h>
      9 #if defined(LIBC_SCCS) && !defined(lint)
     10 __RCSID("$NetBSD: l64a.c,v 1.14 2012/03/13 21:13:48 christos Exp $");
     11 #endif
     12 
     13 #include "namespace.h"
     14 
     15 #include <assert.h>
     16 #include <errno.h>
     17 #include <stdlib.h>
     18 
     19 #ifdef __weak_alias
     20 __weak_alias(l64a,_l64a)
     21 __weak_alias(l64a_r,_l64a_r)
     22 #endif
     23 
     24 char *
     25 l64a(long value)
     26 {
     27 	static char buf[8];
     28 
     29 	(void)l64a_r(value, buf, (int)sizeof (buf));
     30 	return buf;
     31 }
     32 
     33 int
     34 l64a_r(long value, char *buffer, int buflen)
     35 {
     36 	char *s = buffer;
     37 	int digit;
     38 	unsigned long v = value;
     39 
     40 	_DIAGASSERT(buffer != NULL);
     41 
     42 	if (value == 0UL)
     43 		goto out;
     44 
     45 	for (; v != 0 && buflen > 1; s++, buflen--) {
     46 		digit = (int)(v & 0x3f);
     47 
     48 		if (digit < 2)
     49 			*s = digit + '.';
     50 		else if (digit < 12)
     51 			*s = digit + '0' - 2;
     52 		else if (digit < 38)
     53 			*s = digit + 'A' - 12;
     54 		else
     55 			*s = digit + 'a' - 38;
     56 		v >>= 6;
     57 	}
     58 
     59 out:
     60 	*s = '\0';
     61 
     62 	return (v == 0UL ? 0 : -1);
     63 }
     64