8 /* vprintf() uses kputc() to print characters. */
12 #define count_kputc(c) do { charcount++; putf((c), farg); } while(0)
14 int __fvprintf(void (*putf
)(int, void *), const char *fmt
, va_list argp
, void *farg
)
15 #else /* !NBSD_LIBC */
16 #define count_kputc(c) do { charcount++; kputc(c); } while(0)
18 int vprintf(const char *fmt
, va_list argp
)
19 #endif /* NBSD_LIBC */
22 enum { LEFT
, RIGHT
} adjust
;
23 enum { LLONG
, LONG
, INT
} intsize
;
25 int width
, max
, len
, base
;
26 static char X2C_tab
[]= "0123456789ABCDEF";
27 static char x2c_tab
[]= "0123456789abcdef";
32 char temp
[8 * sizeof(long long) / 3 + 2];
34 while ((c
= *fmt
++) != 0) {
36 /* Ordinary character. */
41 /* Format specifier of the form:
42 * %[adjust][fill][width][.max]keys
60 /* Width is specified as an argument, e.g. %*d. */
61 width
= va_arg(argp
, int);
65 /* A number tells the width, e.g. %10d. */
67 width
= width
* 10 + (c
- '0');
68 } while (isdigit(c
= *fmt
++));
73 /* Max field length coming up. */
74 if ((c
= *fmt
++) == '*') {
75 max
= va_arg(argp
, int);
81 max
= max
* 10 + (c
- '0');
82 } while (isdigit(c
= *fmt
++));
86 /* Set a few flags to the default. */
91 if (c
== 'l' || c
== 'L') {
92 /* "Long" key, e.g. %ld. */
96 if (c
== 'l' || c
== 'L') {
97 /* "Long long" key, e.g. %lld. */
107 case LLONG
: i
= va_arg(argp
, long long); break;
108 case LONG
: i
= va_arg(argp
, long); break;
109 case INT
: i
= va_arg(argp
, int); break;
119 /* Pointer, interpret as %X or %lX. */
121 if (sizeof(char *) > sizeof(long)) intsize
= LLONG
;
122 else if (sizeof(char *) > sizeof(int)) intsize
= LONG
;
124 /* Hexadecimal. %X prints upper case A-F, not %lx. */
131 /* Unsigned decimal. */
135 case LLONG
: u
= va_arg(argp
, unsigned long long); break;
136 case LONG
: u
= va_arg(argp
, unsigned long); break;
137 case INT
: u
= va_arg(argp
, unsigned int); break;
140 p
= temp
+ sizeof(temp
)-1;
143 *--p
= x2c
[(ptrdiff_t) (u
% base
)];
144 } while ((u
/= base
) > 0);
150 *p
= va_arg(argp
, int);
154 /* Simply a percent. */
161 /* A string. The other cases will join in here. */
163 p
= va_arg(argp
, char *);
164 if (!p
) p
= "(null)";
167 for (len
= 0; p
[len
] != 0 && len
< max
; len
++) {}
172 if (fill
== '0' && i
< 0) count_kputc('-');
173 if (adjust
== RIGHT
) {
174 while (width
> 0) { count_kputc(fill
); width
--; }
176 if (fill
== ' ' && i
< 0) count_kputc('-');
177 while (len
> 0) { count_kputc((unsigned char) *p
++); len
--; }
178 while (width
> 0) { count_kputc(fill
); width
--; }
181 /* Unrecognized format key, echo it back. */
188 /* Mark the end with a null (should be something else, like -1). */
198 #include <sys/cdefs.h>
203 __weak_alias(vprintf
, _vprintf
)
204 __weak_alias(vfprintf
, _vfprintf
)
205 __strong_alias(__vfprintf_unlocked
, _vfprintf
)
208 __xfputc(int c
, void *arg
)
210 FILE *fp
= (FILE *)arg
;
211 if (fp
->_flags
& __SSTR
) {
212 /* Write to a string. */
215 memset(fp
->_p
++, c
, 1);
220 /* Not a string. Print it. */
224 int _vprintf(const char *fmt
, va_list argp
)
226 return __fvprintf(__xfputc
, fmt
, argp
, stdout
);
229 int _vfprintf(FILE *fp
, const char *fmt
, va_list argp
)
231 return __fvprintf(__xfputc
, fmt
, argp
, fp
);
236 * $PchId: kprintf.c,v 1.5 1996/04/11 06:59:05 philip Exp $