xref: /openbsd-src/lib/libc/stdlib/l64a.c (revision d8bc04e435448c902a8c004581e7486635bbff02)
1 /*
2  * Written by J.T. Conklin <jtc@netbsd.org>.
3  * Public domain.
4  */
5 
6 #if defined(LIBC_SCCS) && !defined(lint)
7 static char *rcsid = "$OpenBSD: l64a.c,v 1.4 2005/03/30 18:51:49 pat Exp $";
8 #endif /* LIBC_SCCS and not lint */
9 
10 #include <errno.h>
11 #include <stdlib.h>
12 
13 char *
14 l64a(long value)
15 {
16 	static char buf[8];
17 	char *s = buf;
18 	int digit;
19 	int i;
20 
21 	if (value < 0) {
22 		errno = EINVAL;
23 		return(NULL);
24 	}
25 
26 	for (i = 0; value != 0 && i < 6; i++) {
27 		digit = value & 0x3f;
28 
29 		if (digit < 2)
30 			*s = digit + '.';
31 		else if (digit < 12)
32 			*s = digit + '0' - 2;
33 		else if (digit < 38)
34 			*s = digit + 'A' - 12;
35 		else
36 			*s = digit + 'a' - 38;
37 
38 		value >>= 6;
39 		s++;
40 	}
41 
42 	*s = '\0';
43 
44 	return(buf);
45 }
46