summaryrefslogtreecommitdiff
path: root/src/stdio/__printf.c
blob: 15b172f2b060459580a554f891a53fe66cc982cd (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <limits.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <wchar.h>

#ifdef _POSIX_SOURCE
#include <sys/types.h>
#include <unistd.h>
#endif

#include "_forced/strtoumax.h"
#include "_forced/strdup.h"

#include "_stdio.h"

#define NUMBUFLEN 64

static void __output(struct io_options *opt, struct io_conversion *conv, const char *str, size_t len)
{
	static char pad[BUFSIZ] = "";
	size_t olen = strlen(str);
	if (len == 0) {
		len = olen;
	}

	if (pad[0] == '\0') {
		memset(pad, ' ', sizeof(pad));
	}

	if (conv && (conv->flags & F_WIDTH)) {
		if (conv->width < olen) {
			conv->width = 0;
		} else {
			conv->width -= olen;
		}
	}

	if (conv && (conv->flags & F_WIDTH) && !(conv->flags & F_LEFT)) {
		while (conv->width > 0) {
			__output(opt, NULL, pad, conv->width % BUFSIZ);
			conv->width -= conv->width % BUFSIZ;
		}
	}

	if (len > INT_MAX || opt->ret > INT_MAX - (int)len) {
		UNDEFINED("In call to %s(): Attempting to output more than INT_MAX characters", opt->fnname);
	}
	
	/* TODO: check for errors */
	if (opt->stream) {
		opt->ret += fwrite(str, 1, len, opt->stream);
	} else if (opt->string) {
		memcpy(opt->string + opt->ret, str, len);
		opt->ret += len;
	} else {
		/* opt->ret += write(opt->fd, str, len); */
	}

	if (conv && (conv->flags & F_WIDTH) && (conv->flags & F_LEFT)) {
		while (conv->width > 0) {
			__output(opt, NULL, pad, conv->width % BUFSIZ);
			conv->width -= conv->width % BUFSIZ;
		}
	}
}

static void __utos(char *s, uintmax_t n, enum conversion_flags flags, int precision, int base)
{
	char lower[] = "0123456789abcdef";
	char upper[] = "0123456789ABCDEF";
	char *digits = (flags & F_UPPER ? upper : lower);
	char sign = '+';
	char buf[NUMBUFLEN + 1]; 
	char *out = buf + NUMBUFLEN;

	*out = '\0';
	out--;
	if (n == 0) {
		*out = '0';
		out--;
	}

	while (n > 0) {
		precision--;
		*out = digits[n % base];
		n /= base;
		out--;
	}

	/* TODO: this has a risk of overflowing */
	while (precision > 0 && (flags & (F_ZERO | F_PRECISION))) {
		*out = '0';
		precision--;
		out--;
	}

	if (flags & F_SIGN) {
		*out = sign;
		out--;
	}

	out++;
	while ((*s++ = *out++) != 0) {
		continue;
	}
}

static void __itos(char *s, intmax_t n, enum conversion_flags flags, int precision, int base)
{
	uintmax_t absn = (uintmax_t)imaxabs(n);
	__utos(s, absn, flags | ((n < 0) ? F_SIGN : 0), precision, base);

	if (n < 0) {
		char *sign = strrchr(s, '+');
		*sign = '-';
	}
}

int __printf(struct io_options *opt, const char * format, va_list arg)
{
	int nout = 0;

	intmax_t argint = 0;
	uintmax_t arguns = 0;
	void *argptr = NULL;
	char numbuf[NUMBUFLEN];

	size_t i;
	//char *s = NULL;

	if (opt->stream) {
		flockfile(opt->stream);
	}

	for (i = 0; format[i] != 0; i++) {
		struct io_conversion conv = {
			.func = opt->fnname,
			.dir = IO_OUT,
		};
		int base = 10;

		if (format[i] != '%') {
			size_t prev = i;
			while (format[i] != '\0' && format[i] != '%') {
				i++;
			}
			__output(opt, NULL, format + prev, i - prev);
			i--;
			continue;
		}

		i += __conv(format + i, &conv, arg);

		switch (conv.spec) {
		case 'o':	/* unsigned int */
		case 'u':
		case 'x':
		case 'X':
			switch (conv.length) {
			case L_hh:	arguns = (unsigned char)va_arg(arg, unsigned int); break;
			case L_h:	arguns = (unsigned short int)va_arg(arg, unsigned int); break;
			case L_l:	arguns = va_arg(arg, unsigned long int); break;

			#if defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__
			case L_ll:	arguns = va_arg(arg, unsigned long long int); break;
			#endif

			case L_j:	arguns = va_arg(arg, uintmax_t); break;
			case L_z:	arguns = va_arg(arg, size_t); break;
			case L_t:	arguns = va_arg(arg, ptrdiff_t); break;

			default:	arguns = va_arg(arg, unsigned int); break;
			}
			if (conv.spec == 'o') {
				base = 8;
			} else if (conv.spec == 'x' || conv.spec == 'X') {
				base = 16;
			}
			__utos(numbuf, arguns, conv.flags, conv.precision, base);
			__output(opt, &conv, numbuf, 0);
			break;

		case 'd':	/* int */
		case 'i':
			switch (conv.length) {
			case L_hh:	argint = (signed char)va_arg(arg, int); break;
			case L_h:	argint = (short int)va_arg(arg, int); break;
			case L_l:	argint = va_arg(arg, long int); break;

			#if defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__
			case L_ll:	argint = va_arg(arg, long long int); break;
			#endif

			case L_j:	argint = va_arg(arg, intmax_t); break;
			case L_z:	argint = va_arg(arg, size_t); break;
			case L_t:	argint = va_arg(arg, ptrdiff_t); break;

			default:	argint = va_arg(arg, int); break;
			}
			__itos(numbuf, argint, conv.flags, conv.precision, 10);
			__output(opt, &conv, numbuf, 0);
			break;


		case 'f':	/* double [-]ddd.ddd */
		case 'F':
			/* fcvt */
			break;

		case 'e':	/* double [-]d.ddde+/-dd */
		case 'E':
			/* ecvt */
			break;

		case 'g':	/* double f or e see docs */
		case 'G':
			/* gcvt */
			break;

		case 'a':	/* double as hex */
		case 'A':
			break;

		case 'c':	/* char */
			if (conv.length == L_default) {
				char c = va_arg(arg, int);
				__output(opt, &conv, &c, 1);
				nout++;
			} else if (conv.length == L_l) {
				#if defined __STDC_VERSION__
				wint_t wc = va_arg(arg, wint_t);
				char mb[MB_CUR_MAX + 1];
				mb[0] = 'W'; mb[1] = 'C'; mb[2] = '\0';
				wctomb(mb, wc);
				__output(opt, &conv, mb, 0);
				#endif
			}
			break;

		case 's':	/* string */
			if (conv.length == L_default) {
				char *string = va_arg(arg, char *);
				__output(opt, &conv, string, 0);
			} else if (conv.length == L_l) {
				#if defined __STDC_VERSION__
				wchar_t *ws = va_arg(arg, wchar_t *);
				char *mbs = malloc(wcslen(ws) * MB_CUR_MAX + 1);
				wcstombs(mbs, ws, wcslen(ws) * MB_CUR_MAX + 1);
				__output(opt, &conv, mbs, 0);
				free(mbs);
				#endif
			}

			break;

		case 'p':	/* pointer */
			argptr = va_arg(arg, void *);
			__itos(numbuf, (intptr_t)argptr, F_ALT | F_ZERO, sizeof(argptr) * 2, 16);
			__output(opt, &conv, numbuf, 0);
			ADD_PREV_STRING(numbuf, __stdio_h.formatted_pointers, __stdio_h.nformatted_pointers);
			break;

		case 'n':	/* write-back */
			switch (conv.length) {
			case L_hh:
				signed char *sc = va_arg(arg, signed char *);
				*sc = nout;
				break;
			case L_h:
				short int *si = va_arg(arg, short int *);
				*si = nout;
				break;
			case L_l:
				long int *li = va_arg(arg, long int *);
				*li = nout;
				break;
			case L_ll:
				long long int *lli = va_arg(arg, long long int *);
				*lli = nout;
				break;
			case L_j:
				intmax_t *im = va_arg(arg, intmax_t *);
				*im = nout;
				break;
			case L_z:
				size_t *sz = va_arg(arg, size_t *);
				*sz = nout;
				break;
			case L_t:
				ptrdiff_t *pd = va_arg(arg, ptrdiff_t *);
				*pd = nout;
				break;
			default:
				int *ip = va_arg(arg, int *);
				*ip = nout;
				break;
			}
			break;

		case '%':	/* literal '%' */
			__output(opt, &conv, "%", 1);
			break;

		default:	/* undefined */
			UNDEFINED("In call to %s(): Unknown conversion specifier %%%c", opt->fnname, format[i]);
		}
	}

	if (opt->stream) {
		funlockfile(opt->stream);
	}

	return opt->ret;
}

/*
STDC(0)
SIGNAL_SAFE(0)
*/