-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprintf.c
More file actions
232 lines (182 loc) · 4.06 KB
/
printf.c
File metadata and controls
232 lines (182 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
printf implementation for printf-debugging. Not meant to be used
in any other way. Non-debug code should use fmt* functions instead.
Only stdout (printf) or stderr (tracef) output, with very limited
format support:
%Ni %0NX %s %p %m
Max output length in a single call is defined by PRINTFBUF below.
Anything above that will be silently dropped.
*/
#include <stdarg.h>
#include <string.h>
#include <format.h>
#include <printf.h>
#include <util.h>
#define F0 (1<<0) /* leading 0 */
#define Fm (1<<1) /* - (minus) */
#define Fl (1<<2) /* l (long) */
#define Fd (1<<3) /* . (dot) */
#define PRINTFBUF 512
struct spec {
char c;
short flags;
short width;
short prec;
};
static char* skip_to_fmt(char* s)
{
while(*s && *s != '%')
s++;
return s;
}
static char* fmt_s(char* p, char* e, struct spec* sp, va_list ap)
{
char* str = va_arg(ap, char*);
if(!str)
str = "(null)";
if(sp->flags & Fd)
return fmtstrn(p, e, str, sp->prec);
else
return fmtstr(p, e, str);
}
static char* fmt_c(char* p, char* e, struct spec* sp, va_list ap)
{
(void)sp;
return fmtchar(p, e, va_arg(ap, unsigned));
}
static char* fmt_i(char* p, char* e, struct spec* sp, va_list ap)
{
if(sp->flags & Fl)
return fmtlong(p, e, va_arg(ap, long));
else
return fmtint(p, e, va_arg(ap, int));
}
static char* fmt_u(char* p, char* e, struct spec* sp, va_list ap)
{
if(sp->flags & Fl)
return fmtulong(p, e, va_arg(ap, unsigned long));
else
return fmtuint(p, e, va_arg(ap, unsigned));
}
static char* fmt_x(char* p, char* e, struct spec* sp, va_list ap)
{
if(sp->flags & Fl)
return fmtxlong(p, e, va_arg(ap, unsigned long));
else
return fmtxlong(p, e, va_arg(ap, unsigned));
}
static char* fmt_p(char* p, char* e, struct spec* sp, va_list ap)
{
(void)sp;
p = fmtstr(p, e, "0x");
p = fmtxlong(p, e, (long)va_arg(ap, void*));
return p;
}
static char* dispatch(char* p, char* e, struct spec* sp, va_list ap)
{
switch(sp->c) {
case 's': return fmt_s(p, e, sp, ap);
case 'c': return fmt_c(p, e, sp, ap);
case 'i': return fmt_i(p, e, sp, ap);
case 'u': return fmt_u(p, e, sp, ap);
case 'X':
case 'x': return fmt_x(p, e, sp, ap);
case 'p': return fmt_p(p, e, sp, ap);
default: return NULL;
}
}
static char* format(char* p, char* e, struct spec* sp, va_list ap)
{
char* q;
if(!(q = dispatch(p, e, sp, ap)))
return q;
int width = sp->width;
int flags = sp->flags;
if(q - p >= width)
return q;
if(flags & F0)
return fmtpad0(p, e, width, q);
if(flags & Fm)
return fmtpadr(p, e, width, q);
else
return fmtpad(p, e, width, q);
return q;
}
static char* intpart(char* q, short* dst, va_list ap)
{
if(*q >= '0' && *q <= '9') {
int num = 0;
while(*q >= '0' && *q <= '9')
num = num*10 + (*q++ - '0');
*dst = num;
} else if(*q == '*') {
q++;
*dst = va_arg(ap, int);
} else {
*dst = 0;
}
return q;
}
static char* parse(char* q, struct spec* sp, va_list ap)
{
short flags = 0;
q++; /* skip % */
if(*q == '-') { flags |= Fm; q++; }
if(*q == '0') { flags |= F0; q++; }
q = intpart(q, &sp->width, ap);
if(*q == '.') { flags |= Fd; q++; }
q = intpart(q, &sp->prec, ap);
if(*q == 'l') { flags |= Fl; q++; }
sp->c = *q++;
sp->flags = flags;
return q;
}
int vfdprintf(int fd, const char* fmt, va_list ap)
{
char buf[PRINTFBUF];
int bufsize = sizeof(buf);
char* p = buf;
char* e = buf + bufsize;
char* f = (char*)fmt;
struct spec sp;
while(*f) {
if(*f == '%') {
if(!(f = parse(f, &sp, ap)))
break;
if(!(p = format(p, e, &sp, ap)))
break;
} else {
char* t = f;
f = skip_to_fmt(f);
p = fmtraw(p, e, t, f - t);
}
}
return writeall(fd, buf, p - buf);
}
int printf(const char* fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfdprintf(STDOUT, fmt, ap);
va_end(ap);
return ret;
}
int putchar(int c)
{
char cc = c;
return writeall(STDOUT, &cc, 1);
}
int puts(const char* str)
{
return writeall(STDOUT, (char*)str, strlen(str));
}
int tracef(const char* fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfdprintf(STDERR, fmt, ap);
va_end(ap);
return ret;
}