2 #include "if_ether.h" /* for ETH_ALEN */
3 #include "limits.h" /* for CHAR_BIT */
8 #define LONG_SHIFT ((int)((sizeof(unsigned long)*CHAR_BIT) - 4))
9 #define INT_SHIFT ((int)((sizeof(unsigned int)*CHAR_BIT) - 4))
10 #define SHRT_SHIFT ((int)((sizeof(unsigned short)*CHAR_BIT) - 4))
11 #define CHAR_SHIFT ((int)((sizeof(unsigned char)*CHAR_BIT) - 4))
16 * Write a formatted string to a buffer.
18 * @v buf Buffer into which to write the string, or NULL
19 * @v fmt Format string
20 * @v args Arguments corresponding to the format string
21 * @ret len Length of string written to buffer (if buf != NULL)
22 * @ret 0 (if buf == NULL)
25 * If #buf==NULL, then the string will be written to the console
26 * directly using putchar().
29 static int vsprintf(char *buf, const char *fmt, va_list args)
34 for ( ; *fmt != '\0'; ++fmt) {
36 buf ? *s++ = *fmt : putchar(*fmt);
39 /* skip width specs */
41 while (*fmt >= '0' && *fmt <= '9')
45 while (*fmt >= '0' && *fmt <= '9')
48 for(p = va_arg(args, char *); *p != '\0'; p++)
49 buf ? *s++ = *p : putchar(*p);
50 } else if (*fmt == 'm') {
51 for(p = strerror(errno); *p != '\0'; p++)
52 buf ? *s++ = *p : putchar(*p);
53 } else { /* Length of item is bounded */
54 char tmp[40], *q = tmp;
56 int shift = INT_SHIFT;
65 else if (*fmt == 'h') {
75 * Before each format q points to tmp buffer
76 * After each format q points past end of item
78 if ((*fmt | 0x20) == 'x') {
79 /* With x86 gcc, sizeof(long) == sizeof(int) */
82 if (shift > INT_SHIFT) {
83 h = va_arg(args, unsigned long);
85 h = va_arg(args, unsigned int);
87 ncase = (*fmt & 0x20);
92 for ( ; shift >= 0; shift -= 4)
93 *q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
95 else if (*fmt == 'd') {
98 if (shift > INT_SHIFT) {
99 i = va_arg(args, long);
101 i = va_arg(args, int);
107 t = q; /* save beginning of digits */
109 *q++ = '0' + (i % 10);
112 /* reverse digits, stop in middle */
113 r = q; /* don't alter q */
120 else if (*fmt == '@') {
126 u.l = va_arg(args, uint32_t);
127 for (r = &u.c[0]; r < &u.c[4]; ++r)
128 q += sprintf(q, "%d.", *r);
131 else if (*fmt == '!') {
133 p = va_arg(args, char *);
134 for (r = p + ETH_ALEN; p < r; ++p)
135 q += sprintf(q, "%hhX:", *p);
138 else if (*fmt == 'c')
139 *q++ = va_arg(args, int);
142 /* now output the saved string */
143 for (p = tmp; p < q; ++p)
144 buf ? *s++ = *p : putchar(*p);
153 * Write a formatted string to a buffer.
155 * @v buf Buffer into which to write the string, or NULL
156 * @v fmt Format string
157 * @v ... Arguments corresponding to the format string
158 * @ret len Length of string written to buffer (if buf != NULL)
159 * @ret 0 (if buf == NULL)
162 * If #buf==NULL, then the string will be written to the console
163 * directly using putchar().
166 int sprintf(char *buf, const char *fmt, ...)
171 i=vsprintf(buf, fmt, args);
177 * Write a formatted string to the console.
179 * @v fmt Format string
180 * @v ... Arguments corresponding to the format string
185 void printf(const char *fmt, ...)
190 i=vsprintf(0, fmt, args);