comparison src/libsys/sprintf/integer.c @ 0:92470e5d0b9e

src: partial import from FC Selenite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 15 May 2020 01:28:16 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:92470e5d0b9e
1 /*
2 * Embedded [v]sprintf() implementation by Mychaela Falconia,
3 * loosely based on the 4.3BSD-Tahoe version.
4 *
5 * This module contains the integer conversion functions.
6 */
7
8 #include <sys/types.h>
9 #include <ctype.h>
10 #include "defs.h"
11
12 extern u_char * _sprintf_field(u_char *op, int width, int flags, int sign,
13 u_char *body, int size, int dprec, int fpprec);
14
15 static const char lcdigits[] = "0123456789abcdef";
16 static const char ucdigits[] = "0123456789ABCDEF";
17
18 u_char *
19 _sprintf_integer(u_char *op, int width, int flags, int sign,
20 unsigned number, int base, int prec)
21 {
22 const char *digits;
23 char buf[12];
24 char *t, *endp;
25
26 /*
27 * ``... diouXx conversions ... if a precision is
28 * specified, the 0 flag will be ignored.''
29 * -- ANSI X3J11
30 */
31 if (prec >= 0)
32 flags &= ~ZEROPAD;
33
34 if (flags & UPPERCASE)
35 digits = ucdigits;
36 else
37 digits = lcdigits;
38
39 /*
40 * ``The result of converting a zero value with an
41 * explicit precision of zero is no characters.''
42 * -- ANSI X3J11
43 */
44 t = endp = buf + sizeof(buf);
45 if (number != 0 || prec != 0) {
46 do {
47 *--t = digits[number % base];
48 number /= base;
49 } while (number);
50 if (flags & ALT && base == 8 && *t != '0')
51 *--t = '0'; /* octal leading 0 */
52 }
53 return _sprintf_field(op, width, flags, sign, t, endp - t, prec, 0);
54 }