1 /* printf() - system services printf() Author: Kees J. Bot
9 #define isdigit(c) ((unsigned) ((c) - '0') < (unsigned) 10)
12 /* Classic C stuff, ignore. */
14 int printf(fmt
) char *fmt
;
17 /* Printf() uses kputc() to print characters. */
20 #define count_kputc(c) do { charcount++; kputc(c); } while(0)
22 int printf(const char *fmt
, ...)
26 enum { LEFT
, RIGHT
} adjust
;
27 enum { LONG
, INT
} intsize
;
29 int width
, max
, len
, base
;
30 static char X2C_tab
[]= "0123456789ABCDEF";
31 static char x2c_tab
[]= "0123456789abcdef";
36 char temp
[8 * sizeof(long) / 3 + 2];
42 while ((c
= *fmt
++) != 0) {
44 /* Ordinary character. */
49 /* Format specifier of the form:
50 * %[adjust][fill][width][.max]keys
68 /* Width is specified as an argument, e.g. %*d. */
69 width
= va_arg(argp
, int);
73 /* A number tells the width, e.g. %10d. */
75 width
= width
* 10 + (c
- '0');
76 } while (isdigit(c
= *fmt
++));
81 /* Max field length coming up. */
82 if ((c
= *fmt
++) == '*') {
83 max
= va_arg(argp
, int);
89 max
= max
* 10 + (c
- '0');
90 } while (isdigit(c
= *fmt
++));
94 /* Set a few flags to the default. */
99 if (c
== 'l' || c
== 'L') {
100 /* "Long" key, e.g. %ld. */
109 i
= intsize
== LONG
? va_arg(argp
, long)
119 /* Pointer, interpret as %X or %lX. */
121 if (sizeof(char *) > sizeof(int)) intsize
= LONG
;
123 /* Hexadecimal. %X prints upper case A-F, not %lx. */
130 /* Unsigned decimal. */
133 u
= intsize
== LONG
? va_arg(argp
, unsigned long)
134 : va_arg(argp
, unsigned int);
136 p
= temp
+ sizeof(temp
)-1;
139 *--p
= x2c
[(ptrdiff_t) (u
% base
)];
140 } while ((u
/= base
) > 0);
146 *p
= va_arg(argp
, int);
150 /* Simply a percent. */
157 /* A string. The other cases will join in here. */
159 p
= va_arg(argp
, char *);
162 for (len
= 0; p
[len
] != 0 && len
< max
; len
++) {}
167 if (fill
== '0' && i
< 0) count_kputc('-');
168 if (adjust
== RIGHT
) {
169 while (width
> 0) { count_kputc(fill
); width
--; }
171 if (fill
== ' ' && i
< 0) count_kputc('-');
172 while (len
> 0) { count_kputc((unsigned char) *p
++); len
--; }
173 while (width
> 0) { count_kputc(fill
); width
--; }
176 /* Unrecognized format key, echo it back. */
183 /* Mark the end with a null (should be something else, like -1). */
190 * $PchId: kprintf.c,v 1.5 1996/04/11 06:59:05 philip Exp $