xref: /freebsd/lib/libc/stdlib/l64a.c (revision d6b92ffa990dc57d8a969a98e3ca4e25ba39cbb2)
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 <stdint.h>
16 #include <stdlib.h>
17 
18 char *
19 l64a(long value)
20 {
21 	static char buf[7];
22 
23 	(void)l64a_r(value, buf, sizeof(buf));
24 	return (buf);
25 }
26 
27 int
28 l64a_r(long value, char *buffer, int buflen)
29 {
30 	static const char chars[] =
31 	    "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
32 	uint32_t v;
33 
34 	v = value;
35 	while (buflen-- > 0) {
36 		if (v == 0) {
37 			*buffer = '\0';
38 			return (0);
39 		}
40 		*buffer++ = chars[v & 0x3f];
41 		v >>= 6;
42 	}
43 	return (-1);
44 }
45