2 * Helper function for the printf family.
16 /* Use static variables for locals */
17 #pragma staticlocals (1);
21 int _printf (struct outdesc* d, char* f, va_list ap)
23 outfunc fout; /* Output function */
24 unsigned char type; /* variable argument type */
25 char str [20]; /* string buffer */
26 char c; /* Current format char */
27 char leftjust; /* left justify string */
28 char addsign; /* always add + or - */
29 char addblank; /* add blank instead of + */
30 char altform; /* alternate form? */
31 char padchar; /* pad with space or zeroes? */
32 char islong; /* l modifier found */
33 unsigned arglen; /* length of argument string */
34 unsigned prec; /* Precision */
35 unsigned width; /* Width of output field */
36 int i; /* Integer value */
37 long l; /* Long value */
38 char* sptr; /* pointer to argument string */
39 register char* s; /* work pointer to argument string */
41 /* Remember the format string in a register variable for shorter code */
42 register char* format = f;
44 /* Remember the output function in a local variable for speed and size */
49 while (c = *format++) {
63 /* format is: %[flags][width][.precision][mod]type */
66 leftjust = addsign = addblank = altform = 0;
68 switch (c = *format) {
100 if (*format == '*') {
101 width = va_arg (ap, int);
105 while (isdigit (c = *format)) {
106 width = width * 10 + (c - '0');
113 if (*format == '.') {
115 if (*format == '*') {
116 prec = va_arg (ap, int);
119 while (isdigit (c = *format)) {
120 prec = prec * 10 + (c - '0');
128 while (strchr ("FNhlL", c = *format)) {
139 /* Check the format specifier */
145 str [0] = va_arg (ap, char);
153 } else if (addblank) {
157 ltoa (va_arg (ap, long), s, 10);
159 itoa (va_arg (ap, int), s, 10);
164 *va_arg (ap, int*) = d->ccount;
169 l = va_arg (ap, unsigned long);
170 if (altform && (l || prec)) {
175 i = va_arg (ap, unsigned);
176 if (altform && (i || prec)) {
184 sptr = va_arg (ap, char*);
189 ultoa (va_arg (ap, unsigned long), str, 10);
191 utoa (va_arg (ap, unsigned), str, 10);
202 ultoa (va_arg (ap, unsigned long), s, 16);
204 utoa (va_arg (ap, unsigned), s, 16);
212 /* Unknown type char - skip it */
217 /* Do argument string formatting */
218 arglen = strlen (sptr);
219 if (prec && prec < arglen) {
222 if (width > arglen) {
223 width -= arglen; /* padcount */
228 /* Do padding on the left side if needed */
230 /* argument right justified */
232 fout (d, &padchar, 1);
237 /* Output the argument string */
238 fout (d, sptr, arglen);
240 /* Output right padding bytes if needed */
242 /* argument left justified */
244 fout (d, &padchar, 1);