annotate src/libsys/sprintf/vspcore.c @ 92:e42d528f3e6e

components/main_ir: ramvecs module now goes outside of libs
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 21 Jul 2018 00:22:35 +0000
parents 425ab6d987f3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Embedded [v]sprintf() implementation by Mychaela Falconia,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * loosely based on the 4.3BSD-Tahoe version.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 *
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * This module contains the core of the vsprintf() function, which may
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * be either used directly by user code or called by the sprintf()
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 * trivial wrapper.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/types.h>
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <ctype.h>
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdarg.h>
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "defs.h"
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern u_char * _sprintf_integer(u_char *op, int width, int flags, int sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned number, int base, int prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern u_char * _sprintf_percent_f(u_char *op, int width, int flags, int sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 double number, int prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char *
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 _sprintf_field(u_char *op, int width, int flags, int sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 u_char *body, int size, int dprec, int fpprec)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 int fieldsz; /* field size expanded by sign, etc */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 int realsz; /* field size expanded by decimal precision */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int n; /* scratch */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 * All reasonable formats wind up here. At this point,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 * `body' points to a string which (if not flags&LADJUST)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 * should be padded out to `width' places. If
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 * flags&ZEROPAD, it should first be prefixed by any
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 * sign or other prefix; otherwise, it should be blank
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 * padded before the prefix is emitted. After any
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 * left-hand padding and prefixing, emit zeroes
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 * required by a decimal [diouxX] precision, then print
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 * the string proper, then emit zeroes required by any
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 * leftover floating precision; finally, if LADJUST,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 * pad with blanks.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 * compute actual size, so we know how much to pad
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 * fieldsz excludes decimal prec; realsz includes it
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 fieldsz = size + fpprec;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (sign)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 fieldsz++;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (flags & HEXPREFIX)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 fieldsz += 2;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 realsz = dprec > fieldsz ? dprec : fieldsz;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 /* right-adjusting blank padding */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if ((flags & (LADJUST|ZEROPAD)) == 0 && width)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 for (n = realsz; n < width; n++)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 *op++ = ' ';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 /* prefix */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (sign)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 *op++ = sign;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (flags & HEXPREFIX) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 *op++ = '0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 *op++ = (flags & UPPERCASE) ? 'X' : 'x';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 /* right-adjusting zero padding */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 for (n = realsz; n < width; n++)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 *op++ = '0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 /* leading zeroes from decimal precision */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 for (n = fieldsz; n < dprec; n++)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 *op++ = '0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 /* the string or number proper */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 bcopy(body, op, size);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 op += size;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 /* trailing f.p. zeroes */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 while (--fpprec >= 0)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 *op++ = '0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 /* left-adjusting padding (always blank) */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (flags & LADJUST)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 for (n = realsz; n < width; n++)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 *op++ = ' ';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 return(op);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 int
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 vsprintf(buf0, fmt0, argp)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 u_char *buf0, *fmt0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 va_list argp;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 u_char *op; /* output buffer working ptr */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 u_char *fmt; /* format string working ptr */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 int ch; /* character from fmt */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 int n; /* scratch integer */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 char *t; /* scratch pointer */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 int flags; /* flags as above */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 int prec; /* precision from format (%.3d), or -1 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 int width; /* width from format (%8d), or 0 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 char sign; /* sign prefix (' ', '+', '-', or \0) */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 unsigned un; /* unsigned number for conversion */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 op = buf0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 for (fmt = fmt0; ; ++fmt) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 for (; (ch = *fmt) && ch != '%'; ++fmt)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 *op++ = ch;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (!ch) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 out: *op = '\0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return (op - buf0);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 flags = 0; width = 0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 prec = -1;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 sign = '\0';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 rflag: switch (*++fmt) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 case ' ':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 * ``If the space and + flags both appear, the space
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 * flag will be ignored.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (!sign)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 sign = ' ';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 case '#':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 flags |= ALT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 case '*':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 * ``A negative field width argument is taken as a
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 * - flag followed by a positive field width.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 * They don't exclude field widths read from args.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 if ((width = va_arg(argp, int)) >= 0)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 width = -width;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 /* FALLTHROUGH */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 case '-':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 flags |= LADJUST;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 case '+':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 sign = '+';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 case '.':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 if (*++fmt == '*')
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 n = va_arg(argp, int);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 else {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 n = 0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 while (isascii(*fmt) && isdigit(*fmt))
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 n = 10 * n + todigit(*fmt++);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 --fmt;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 prec = n < 0 ? -1 : n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 case '0':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 * ``Note that 0 is taken as a flag, not as the
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 * beginning of a field width.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 flags |= ZEROPAD;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 case '1': case '2': case '3': case '4':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 case '5': case '6': case '7': case '8': case '9':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 n = 0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 do {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 n = 10 * n + todigit(*fmt);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 } while (isascii(*++fmt) && isdigit(*fmt));
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 width = n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 --fmt;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 case 'L':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 flags |= LONGDBL;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 case 'h':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 flags |= SHORTINT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 case 'l':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 flags |= LONGINT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 goto rflag;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 case 'c':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 * XXX reusing a variable of type char
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 * for an unrelated purpose
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 sign = (char) va_arg(argp, int);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 op = _sprintf_field(op, width, flags, 0, &sign, 1,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 0, 0);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 case 'D':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 flags |= LONGINT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 /*FALLTHROUGH*/
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 case 'd':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 case 'i':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 n = va_arg(argp, int);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 if (n < 0) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 un = -n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 sign = '-';
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 } else
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 un = n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 op = _sprintf_integer(op, width, flags, sign, un, 10,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 case 'f':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 op = _sprintf_percent_f(op, width, flags, sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 va_arg(argp, double), prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 case 'n':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 n = op - buf0;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 if (flags & LONGINT)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 *va_arg(argp, long *) = n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 else if (flags & SHORTINT)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 *va_arg(argp, short *) = n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 else
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 *va_arg(argp, int *) = n;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 case 'O':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 flags |= LONGINT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 /*FALLTHROUGH*/
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 case 'o':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 un = va_arg(argp, unsigned);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 op = _sprintf_integer(op, width, flags, 0, un, 8, prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 case 'p':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 * ``The argument shall be a pointer to void. The
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 * value of the pointer is converted to a sequence
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 * of printable characters, in an implementation-
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 * defined manner.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 /* NOSTRICT */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 un = (unsigned)va_arg(argp, void *);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 op = _sprintf_integer(op, width, flags | HEXPREFIX, 0,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 un, 16, prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 case 's':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 if (!(t = va_arg(argp, char *)))
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 t = "(null)";
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 if (prec >= 0) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 * can't use strlen; can only look for the
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 * NUL in the first `prec' characters, and
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 * strlen() will go further.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 char *p, *memchr();
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 if (p = memchr(t, 0, prec)) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 n = p - t;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 if (n > prec)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 n = prec;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 } else
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 n = prec;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255 } else
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 n = strlen(t);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 op = _sprintf_field(op, width, flags, 0, t, n, 0, 0);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 case 'U':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 flags |= LONGINT;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261 /*FALLTHROUGH*/
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 case 'u':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 un = va_arg(argp, unsigned);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 op = _sprintf_integer(op, width, flags, 0, un, 10,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267 case 'X':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 flags |= UPPERCASE;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 /* FALLTHROUGH */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270 case 'x':
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 un = va_arg(argp, unsigned);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 /* leading 0x/X only if non-zero */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 if (flags & ALT && un != 0)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274 flags |= HEXPREFIX;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 op = _sprintf_integer(op, width, flags, 0, un, 16,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 prec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 break;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 case '\0': /* "%?" prints ?, unless ? is NULL */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 goto out;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 default:
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 *op++ = *fmt;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 /* NOTREACHED */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285 }