diff options
author | trhodes <trhodes@FreeBSD.org> | 2005-12-24 22:37:59 +0000 |
---|---|---|
committer | trhodes <trhodes@FreeBSD.org> | 2005-12-24 22:37:59 +0000 |
commit | 8190ceb049ac4ab6148966f67d239e0618b7114b (patch) | |
tree | 7e59823292e97167e21df1524210e5c0d4ccf3c0 /lib/libc/stdlib/l64a.c | |
parent | 412f766852e2da36b4f178232cadb20dda832f61 (diff) | |
download | FreeBSD-src-8190ceb049ac4ab6148966f67d239e0618b7114b.zip FreeBSD-src-8190ceb049ac4ab6148966f67d239e0618b7114b.tar.gz |
Add a64l(), l64a(), and l64a_r() XSI extentions. These functions convert
between a 32-bit integer and a radix-64 ASCII string. The l64a_r() function
is a NetBSD addition.
PR: 51209 (based on submission, but very different)
Reviewed by: bde, ru
Diffstat (limited to 'lib/libc/stdlib/l64a.c')
-rw-r--r-- | lib/libc/stdlib/l64a.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/libc/stdlib/l64a.c b/lib/libc/stdlib/l64a.c new file mode 100644 index 0000000..bc10553 --- /dev/null +++ b/lib/libc/stdlib/l64a.c @@ -0,0 +1,52 @@ +/* + * Written by J.T. Conklin <jtc@NetBSD.org>. + * Public domain. + */ + +#if 0 +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: l64a.c,v 1.13 2003/07/26 19:24:54 salo Exp $"); +#endif /* not lint */ +#endif + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <stdlib.h> + +#define ADOT 46 /* ASCII '.' */ +#define ASLASH ADOT + 1 /* ASCII '/' */ +#define A0 48 /* ASCII '0' */ +#define AA 65 /* ASCII 'A' */ +#define Aa 97 /* ASCII 'a' */ + +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) +{ + long v; + int digit; + + v = value & (long)0xffffffff; + for (; v != 0 && buflen > 1; buffer++, buflen--) { + digit = v & 0x3f; + if (digit < 2) + *buffer = digit + ADOT; + else if (digit < 12) + *buffer = digit + A0 - 2; + else if (digit < 38) + *buffer = digit + AA - 12; + else + *buffer = digit + Aa - 38; + v >>= 6; + } + return (v == 0 ? 0 : -1); +} |