6 extern void pcboot_putchar(int c);
8 static void bwrite(char *buf, size_t buf_sz, char *str, int sz);
9 static int intern_printf(char *buf, size_t sz, const char *fmt, va_list ap);
17 int puts(const char *s)
26 /* -- printf and friends -- */
28 static char *convc = "dioxXucsfeEgGpn%";
30 #define IS_CONV(c) strchr(convc, c)
32 int printf(const char *fmt, ...)
38 res = intern_printf(0, 0, fmt, ap);
43 int vprintf(const char *fmt, va_list ap)
45 return intern_printf(0, 0, fmt, ap);
48 int sprintf(char *buf, const char *fmt, ...)
54 res = intern_printf(buf, 0, fmt, ap);
59 int vsprintf(char *buf, const char *fmt, va_list ap)
61 return intern_printf(buf, 0, fmt, ap);
64 int snprintf(char *buf, size_t sz, const char *fmt, ...)
70 res = intern_printf(buf, sz, fmt, ap);
75 int vsnprintf(char *buf, size_t sz, const char *fmt, va_list ap)
77 return intern_printf(buf, sz, fmt, ap);
81 /* intern_printf provides all the functionality needed by all the printf
83 * - buf: optional buffer onto which the formatted results are written. If null
84 * then the output goes to the terminal through putchar calls. This is used
85 * by the (v)sprintf variants which write to an array of char.
86 * - sz: optional maximum size of the output, 0 means unlimited. This is used
87 * by the (v)snprintf variants to avoid buffer overflows.
88 * The rest are obvious, format string and variable argument list.
91 #define BUF(x) ((x) ? (x) + cnum : (x))
92 #define SZ(x) ((x) ? (x) - cnum : (x))
94 static int intern_printf(char *buf, size_t sz, const char *fmt, va_list ap)
99 const char *fstart = 0;
108 int left_align = 0; /* not implemented yet */
129 bwrite(BUF(buf), SZ(sz), "0x", 2);
140 bwrite(BUF(buf), SZ(sz), "0", 1);
147 utoa(va_arg(ap, unsigned int), conv_buf, base);
149 itoa(va_arg(ap, int), conv_buf, base);
152 for(i=0; conv_buf[i]; i++) {
153 conv_buf[i] = toupper(conv_buf[i]);
157 slen = strlen(conv_buf);
158 for(i=slen; i<fwidth; i++) {
159 bwrite(BUF(buf), SZ(sz), (char*)&padc, 1);
163 bwrite(BUF(buf), SZ(sz), conv_buf, strlen(conv_buf));
169 char c = va_arg(ap, int);
170 bwrite(BUF(buf), SZ(sz), &c, 1);
176 str = va_arg(ap, char*);
179 for(i=slen; i<fwidth; i++) {
180 bwrite(BUF(buf), SZ(sz), (char*)&padc, 1);
183 bwrite(BUF(buf), SZ(sz), str, slen);
188 *va_arg(ap, int*) = cnum;
195 /* restore default conversion state */
228 const char *fw = fmt;
229 while(*fmt && isdigit(*fmt)) fmt++;
238 bwrite(BUF(buf), SZ(sz), (char*)fmt++, 1);
247 /* bwrite is called by intern_printf to transparently handle writing into a
248 * buffer (if buf is non-null) or to the terminal (if buf is null).
250 static void bwrite(char *buf, size_t buf_sz, char *str, int sz)
253 if(buf_sz && buf_sz <= sz) sz = buf_sz - 1;
254 memcpy(buf, str, sz);
259 for(i=0; i<sz; i++) {