xref: /freebsd/lib/libc/stdlib/l64a.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Written by J.T. Conklin <jtc@NetBSD.org>.
3  * Public domain.
4  */
5 
6 #if 0
7 #if defined(LIBC_SCCS) && !defined(lint)
8 __RCSID("$NetBSD: l64a.c,v 1.13 2003/07/26 19:24:54 salo Exp $");
9 #endif /* not lint */
10 #endif
11 
12 #include <sys/cdefs.h>
13 __FBSDID("$FreeBSD$");
14 
15 #include <stdlib.h>
16 
17 #define	ADOT	46		/* ASCII '.' */
18 #define	ASLASH	ADOT + 1	/* ASCII '/' */
19 #define	A0	48		/* ASCII '0' */
20 #define	AA	65		/* ASCII 'A' */
21 #define	Aa	97		/* ASCII 'a' */
22 
23 char *
24 l64a(long value)
25 {
26 	static char buf[8];
27 
28 	(void)l64a_r(value, buf, sizeof(buf));
29 	return (buf);
30 }
31 
32 int
33 l64a_r(long value, char *buffer, int buflen)
34 {
35 	long v;
36 	int digit;
37 
38 	v = value & (long)0xffffffff;
39 	for (; v != 0 && buflen > 1; buffer++, buflen--) {
40 		digit = v & 0x3f;
41 		if (digit < 2)
42 			*buffer = digit + ADOT;
43 		else if (digit < 12)
44 			*buffer = digit + A0 - 2;
45 		else if (digit < 38)
46 			*buffer = digit + AA - 12;
47 		else
48 			*buffer = digit + Aa - 38;
49 		v >>= 6;
50 	}
51 	return (v == 0 ? 0 : -1);
52 }
53