xref: /linux/tools/include/nolibc/stdio.h (revision b5f3f59cf4384a8c9e60fa4bb1a8f4ad71126a90)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * minimal stdio function definitions for NOLIBC
4  * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5  */
6 
7 /* make sure to include all global symbols */
8 #include "nolibc.h"
9 
10 #ifndef _NOLIBC_STDIO_H
11 #define _NOLIBC_STDIO_H
12 
13 #include "std.h"
14 #include "arch.h"
15 #include "errno.h"
16 #include "fcntl.h"
17 #include "types.h"
18 #include "sys.h"
19 #include "stdarg.h"
20 #include "stdlib.h"
21 #include "string.h"
22 #include "compiler.h"
23 
24 static const char *strerror(int errnum);
25 
26 #ifndef EOF
27 #define EOF (-1)
28 #endif
29 
30 /* Buffering mode used by setvbuf.  */
31 #define _IOFBF 0	/* Fully buffered. */
32 #define _IOLBF 1	/* Line buffered. */
33 #define _IONBF 2	/* No buffering. */
34 
35 /* just define FILE as a non-empty type. The value of the pointer gives
36  * the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE
37  * are immediately identified as abnormal entries (i.e. possible copies
38  * of valid pointers to something else).
39  */
40 typedef struct FILE {
41 	char dummy[1];
42 } FILE;
43 
44 static __attribute__((unused)) FILE* const stdin  = (FILE*)(intptr_t)~STDIN_FILENO;
45 static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO;
46 static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_FILENO;
47 
48 /* provides a FILE* equivalent of fd. The mode is ignored. */
49 static __attribute__((unused))
50 FILE *fdopen(int fd, const char *mode __attribute__((unused)))
51 {
52 	if (fd < 0) {
53 		SET_ERRNO(EBADF);
54 		return NULL;
55 	}
56 	return (FILE*)(intptr_t)~fd;
57 }
58 
59 static __attribute__((unused))
60 FILE *fopen(const char *pathname, const char *mode)
61 {
62 	int flags, fd;
63 
64 	switch (*mode) {
65 	case 'r':
66 		flags = O_RDONLY;
67 		break;
68 	case 'w':
69 		flags = O_WRONLY | O_CREAT | O_TRUNC;
70 		break;
71 	case 'a':
72 		flags = O_WRONLY | O_CREAT | O_APPEND;
73 		break;
74 	default:
75 		SET_ERRNO(EINVAL); return NULL;
76 	}
77 
78 	if (mode[1] == '+')
79 		flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
80 
81 	fd = open(pathname, flags, 0666);
82 	return fdopen(fd, mode);
83 }
84 
85 /* provides the fd of stream. */
86 static __attribute__((unused))
87 int fileno(FILE *stream)
88 {
89 	intptr_t i = (intptr_t)stream;
90 
91 	if (i >= 0) {
92 		SET_ERRNO(EBADF);
93 		return -1;
94 	}
95 	return ~i;
96 }
97 
98 /* flush a stream. */
99 static __attribute__((unused))
100 int fflush(FILE *stream)
101 {
102 	intptr_t i = (intptr_t)stream;
103 
104 	/* NULL is valid here. */
105 	if (i > 0) {
106 		SET_ERRNO(EBADF);
107 		return -1;
108 	}
109 
110 	/* Don't do anything, nolibc does not support buffering. */
111 	return 0;
112 }
113 
114 /* flush a stream. */
115 static __attribute__((unused))
116 int fclose(FILE *stream)
117 {
118 	intptr_t i = (intptr_t)stream;
119 
120 	if (i >= 0) {
121 		SET_ERRNO(EBADF);
122 		return -1;
123 	}
124 
125 	if (close(~i))
126 		return EOF;
127 
128 	return 0;
129 }
130 
131 /* getc(), fgetc(), getchar() */
132 
133 #define getc(stream) fgetc(stream)
134 
135 static __attribute__((unused))
136 int fgetc(FILE* stream)
137 {
138 	unsigned char ch;
139 
140 	if (read(fileno(stream), &ch, 1) <= 0)
141 		return EOF;
142 	return ch;
143 }
144 
145 static __attribute__((unused))
146 int getchar(void)
147 {
148 	return fgetc(stdin);
149 }
150 
151 
152 /* putc(), fputc(), putchar() */
153 
154 #define putc(c, stream) fputc(c, stream)
155 
156 static __attribute__((unused))
157 int fputc(int c, FILE* stream)
158 {
159 	unsigned char ch = c;
160 
161 	if (write(fileno(stream), &ch, 1) <= 0)
162 		return EOF;
163 	return ch;
164 }
165 
166 static __attribute__((unused))
167 int putchar(int c)
168 {
169 	return fputc(c, stdout);
170 }
171 
172 
173 /* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
174 
175 /* internal fwrite()-like function which only takes a size and returns 0 on
176  * success or EOF on error. It automatically retries on short writes.
177  */
178 static __attribute__((unused))
179 int _fwrite(const void *buf, size_t size, FILE *stream)
180 {
181 	ssize_t ret;
182 	int fd = fileno(stream);
183 
184 	while (size) {
185 		ret = write(fd, buf, size);
186 		if (ret <= 0)
187 			return EOF;
188 		size -= ret;
189 		buf += ret;
190 	}
191 	return 0;
192 }
193 
194 static __attribute__((unused))
195 size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
196 {
197 	size_t written;
198 
199 	for (written = 0; written < nmemb; written++) {
200 		if (_fwrite(s, size, stream) != 0)
201 			break;
202 		s += size;
203 	}
204 	return written;
205 }
206 
207 /* internal fread()-like function which only takes a size and returns 0 on
208  * success or EOF on error. It automatically retries on short reads.
209  */
210 static __attribute__((unused))
211 int _fread(void *buf, size_t size, FILE *stream)
212 {
213 	int fd = fileno(stream);
214 	ssize_t ret;
215 
216 	while (size) {
217 		ret = read(fd, buf, size);
218 		if (ret <= 0)
219 			return EOF;
220 		size -= ret;
221 		buf += ret;
222 	}
223 	return 0;
224 }
225 
226 static __attribute__((unused))
227 size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
228 {
229 	size_t nread;
230 
231 	for (nread = 0; nread < nmemb; nread++) {
232 		if (_fread(s, size, stream) != 0)
233 			break;
234 		s += size;
235 	}
236 	return nread;
237 }
238 
239 static __attribute__((unused))
240 int fputs(const char *s, FILE *stream)
241 {
242 	return _fwrite(s, strlen(s), stream);
243 }
244 
245 static __attribute__((unused))
246 int puts(const char *s)
247 {
248 	if (fputs(s, stdout) == EOF)
249 		return EOF;
250 	return putchar('\n');
251 }
252 
253 
254 /* fgets() */
255 static __attribute__((unused))
256 char *fgets(char *s, int size, FILE *stream)
257 {
258 	int ofs;
259 	int c;
260 
261 	for (ofs = 0; ofs + 1 < size;) {
262 		c = fgetc(stream);
263 		if (c == EOF)
264 			break;
265 		s[ofs++] = c;
266 		if (c == '\n')
267 			break;
268 	}
269 	if (ofs < size)
270 		s[ofs] = 0;
271 	return ofs ? s : NULL;
272 }
273 
274 
275 /* fseek */
276 static __attribute__((unused))
277 int fseek(FILE *stream, long offset, int whence)
278 {
279 	int fd = fileno(stream);
280 	off_t ret;
281 
282 	ret = lseek(fd, offset, whence);
283 
284 	/* lseek() and fseek() differ in that lseek returns the new
285 	 * position or -1, fseek() returns either 0 or -1.
286 	 */
287 	if (ret >= 0)
288 		return 0;
289 
290 	return -1;
291 }
292 
293 
294 /* printf(). Supports most of the normal integer and string formats.
295  *  - %[#-+ 0][width][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%}
296  *  - %% generates a single %
297  *  - %m outputs strerror(errno).
298  *  - %X outputs a..f the same as %x.
299  *  - The modifiers [-0] are currently ignored.
300  *  - No support for precision or variable widths.
301  *  - No support for floating point or wide characters.
302  *  - Invalid formats are copied to the output buffer.
303  *
304  * Called by vfprintf() and snprintf() to do the actual formatting.
305  * The callers provide a callback function to save the formatted data.
306  * The callback function is called multiple times:
307  *  - for each group of literal characters in the format string.
308  *  - for field padding.
309  *  - for each conversion specifier.
310  *  - with (NULL, 0) at the end of the __nolibc_printf.
311  * If the callback returns non-zero __nolibc_printf() immediately returns -1.
312  */
313 
314 typedef int (*__nolibc_printf_cb)(void *state, const char *buf, size_t size);
315 
316 /* This code uses 'flag' variables that are indexed by the low 6 bits
317  * of characters to optimise checks for multiple characters.
318  *
319  * _NOLIBC_PF_FLAGS_CONTAIN(flags, 'a', 'b'. ...)
320  * returns non-zero if the bit for any of the specified characters is set.
321  *
322  * _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'a', 'b'. ...)
323  * returns the flag bit for ch if it is one of the specified characters.
324  * All the characters must be in the same 32 character block (non-alphabetic,
325  * upper case, or lower case) of the ASCII character set.
326  */
327 #define _NOLIBC_PF_FLAG(ch) (1u << ((ch) & 0x1f))
328 #define _NOLIBC_PF_FLAG_NZ(ch) ((ch) ? _NOLIBC_PF_FLAG(ch) : 0)
329 #define _NOLIBC_PF_FLAG8(cmp_1, cmp_2, cmp_3, cmp_4, cmp_5, cmp_6, cmp_7, cmp_8, ...) \
330 	(_NOLIBC_PF_FLAG_NZ(cmp_1) | _NOLIBC_PF_FLAG_NZ(cmp_2) | \
331 	 _NOLIBC_PF_FLAG_NZ(cmp_3) | _NOLIBC_PF_FLAG_NZ(cmp_4) | \
332 	 _NOLIBC_PF_FLAG_NZ(cmp_5) | _NOLIBC_PF_FLAG_NZ(cmp_6) | \
333 	 _NOLIBC_PF_FLAG_NZ(cmp_7) | _NOLIBC_PF_FLAG_NZ(cmp_8))
334 #define _NOLIBC_PF_FLAGS_CONTAIN(flags, ...) \
335 	((flags) & _NOLIBC_PF_FLAG8(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0))
336 #define _NOLIBC_PF_CHAR_IS_ONE_OF(ch, cmp_1, ...) \
337 	((unsigned int)(ch) - (cmp_1 & 0xe0) > 0x1f ? 0 : \
338 		_NOLIBC_PF_FLAGS_CONTAIN(_NOLIBC_PF_FLAG(ch), cmp_1, __VA_ARGS__))
339 
340 static __attribute__((unused, format(printf, 3, 0)))
341 int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list args)
342 {
343 	char ch;
344 	unsigned long long v;
345 	long long signed_v;
346 	int written, width, len;
347 	unsigned int flags, ch_flag;
348 	char outbuf[2 + 22 + 1];
349 	char *out;
350 	const char *outstr;
351 	unsigned int sign_prefix;
352 
353 	written = 0;
354 	while (1) {
355 		outstr = fmt;
356 		ch = *fmt++;
357 		if (!ch)
358 			break;
359 
360 		width = 0;
361 		flags = 0;
362 		if (ch != '%') {
363 			while (*fmt && *fmt != '%')
364 				fmt++;
365 			/* Output characters from the format string. */
366 			len = fmt - outstr;
367 			goto do_output;
368 		}
369 
370 		/* we're in a format sequence */
371 
372 		/* Conversion flag characters */
373 		while (1) {
374 			ch = *fmt++;
375 			ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, ' ', '#', '+', '-', '0');
376 			if (!ch_flag)
377 				break;
378 			flags |= ch_flag;
379 		}
380 
381 		/* width */
382 		while (ch >= '0' && ch <= '9') {
383 			width *= 10;
384 			width += ch - '0';
385 
386 			ch = *fmt++;
387 		}
388 
389 		/* Length modifier.
390 		 * They miss the conversion flags characters " #+-0" so can go into flags.
391 		 * Change both L and ll to j (all always 64bit).
392 		 */
393 		if (ch == 'L')
394 			ch = 'j';
395 		ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 't', 'z', 'j', 'q');
396 		if (ch_flag != 0) {
397 			if (ch == 'l' && fmt[0] == 'l') {
398 				fmt++;
399 				ch_flag = _NOLIBC_PF_FLAG('j');
400 			}
401 			flags |= ch_flag;
402 			ch = *fmt++;
403 		}
404 
405 		/* Conversion specifiers. */
406 
407 		/* Numeric and pointer conversion specifiers.
408 		 *
409 		 * Use an explicit bound check (rather than _NOLIBC_PF_CHAR_IS_ONE_OF())
410 		 * so that 'X' can be allowed through.
411 		 * 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same
412 		 * value for both.
413 		 *
414 		 * We need to check for "%p" or "%#x" later, merging here gives better code.
415 		 * But '#' collides with 'c' so shift right.
416 		 */
417 		ch_flag = _NOLIBC_PF_FLAG(ch) | (flags & _NOLIBC_PF_FLAG('#')) >> 1;
418 		if (((ch >= 'a' && ch <= 'z') || ch == 'X') &&
419 		    _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'i', 'u', 'x', 'p', 's')) {
420 			/* 'long' is needed for pointer/string conversions and ltz lengths.
421 			 * A single test can be used provided 'p' (the same bit as '0')
422 			 * is masked from flags.
423 			 */
424 			if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag | (flags & ~_NOLIBC_PF_FLAG('p')),
425 						     'p', 's', 'l', 't', 'z')) {
426 				v = va_arg(args, unsigned long);
427 				signed_v = (long)v;
428 			} else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j', 'q')) {
429 				v = va_arg(args, unsigned long long);
430 				signed_v = v;
431 			} else {
432 				v = va_arg(args, unsigned int);
433 				signed_v = (int)v;
434 			}
435 
436 			if (ch == 'c') {
437 				/* "%c" - single character. */
438 				outbuf[0] = v;
439 				len = 1;
440 				outstr = outbuf;
441 				goto do_output;
442 			}
443 
444 			if (ch == 's') {
445 				/* "%s" - character string. */
446 				outstr = (const char  *)(uintptr_t)v;
447 				if (!outstr)
448 					outstr = "(null)";
449 				goto do_strlen_output;
450 			}
451 
452 			/* The 'sign_prefix' can be zero, one or two ("0x") characters.
453 			 * Prepended least significant byte first stopping on a zero byte.
454 			 */
455 			sign_prefix = 0;
456 
457 			if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i')) {
458 				/* "%d" and "%i" - signed decimal numbers. */
459 				if (signed_v < 0) {
460 					sign_prefix = '-';
461 					v = -(signed_v + 1);
462 					v++;
463 				} else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, '+')) {
464 					sign_prefix = '+';
465 				} else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, ' ')) {
466 					sign_prefix = ' ';
467 				}
468 			}
469 
470 			/* The value is converted offset into the buffer so that
471 			 * the sign/prefix can be added in front.
472 			 * The longest digit string is 22 + 1 for octal conversions, the
473 			 * space is reserved even though octal isn't currently supported.
474 			 */
475 			out = outbuf + 2;
476 
477 			if (v == 0) {
478 				/* There are special rules for zero. */
479 				if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'p')) {
480 					/* "%p" match glibc, precision is ignored */
481 					outstr = "(nil)";
482 					len = 5;
483 					goto do_output;
484 				}
485 				/* All other formats (including "%#x") just output "0". */
486 				out[0] = '0';
487 				len = 1;
488 			} else {
489 				/* Convert the number to ascii in the required base. */
490 				if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i', 'u')) {
491 					/* Base 10 */
492 					len = u64toa_r(v, out);
493 				} else {
494 					/* Base 16 */
495 					if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'p', '#' - 1)) {
496 						/* "%p" and "%#x" need "0x" prepending. */
497 						sign_prefix = '0' << 8 | 'x';
498 					}
499 					len = u64toh_r(v, out);
500 				}
501 			}
502 
503 			/* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */
504 			for (; sign_prefix; sign_prefix >>= 8) {
505 				/* Force gcc to increment len inside the loop. */
506 				_NOLIBC_OPTIMIZER_HIDE_VAR(len);
507 				len++;
508 				*--out = sign_prefix;
509 			}
510 			outstr = out;
511 			goto do_output;
512 		}
513 
514 		if (ch == 'm') {
515 #ifdef NOLIBC_IGNORE_ERRNO
516 			outstr = "unknown error";
517 #else
518 			outstr = strerror(errno);
519 #endif /* NOLIBC_IGNORE_ERRNO */
520 			goto do_strlen_output;
521 		}
522 
523 		if (ch != '%') {
524 			/* Invalid format: back up to output the format characters */
525 			fmt = outstr + 1;
526 			/* and output a '%' now. */
527 		}
528 		/* %% is documented as a 'conversion specifier'.
529 		 * Any flags, precision or length modifier are ignored.
530 		 */
531 		len = 1;
532 		width = 0;
533 		outstr = fmt - 1;
534 		goto do_output;
535 
536 do_strlen_output:
537 		/* Open coded strlen() (slightly smaller). */
538 		for (len = 0;; len++)
539 			if (!outstr[len])
540 				break;
541 
542 do_output:
543 		written += len;
544 
545 		/* Stop gcc back-merging this code into one of the conditionals above. */
546 		_NOLIBC_OPTIMIZER_HIDE_VAR(len);
547 
548 		/* Output the characters on the required side of any padding. */
549 		width -= len;
550 		flags = _NOLIBC_PF_FLAGS_CONTAIN(flags, '-');
551 		if (flags && cb(state, outstr, len) != 0)
552 			return -1;
553 		while (width > 0) {
554 			/* Output pad in 16 byte blocks with the small block first. */
555 			int pad_len = ((width - 1) & 15) + 1;
556 			width -= pad_len;
557 			written += pad_len;
558 			if (cb(state, "                ", pad_len) != 0)
559 				return -1;
560 		}
561 		if (!flags && cb(state, outstr, len) != 0)
562 			return -1;
563 	}
564 
565 	/* Request a final '\0' be added to the snprintf() output.
566 	 * This may be the only call of the cb() function.
567 	 */
568 	if (cb(state, NULL, 0) != 0)
569 		return -1;
570 
571 	return written;
572 }
573 
574 static int __nolibc_fprintf_cb(void *stream, const char *buf, size_t size)
575 {
576 	return _fwrite(buf, size, stream);
577 }
578 
579 static __attribute__((unused, format(printf, 2, 0)))
580 int vfprintf(FILE *stream, const char *fmt, va_list args)
581 {
582 	return __nolibc_printf(__nolibc_fprintf_cb, stream, fmt, args);
583 }
584 
585 static __attribute__((unused, format(printf, 1, 0)))
586 int vprintf(const char *fmt, va_list args)
587 {
588 	return vfprintf(stdout, fmt, args);
589 }
590 
591 static __attribute__((unused, format(printf, 2, 3)))
592 int fprintf(FILE *stream, const char *fmt, ...)
593 {
594 	va_list args;
595 	int ret;
596 
597 	va_start(args, fmt);
598 	ret = vfprintf(stream, fmt, args);
599 	va_end(args);
600 	return ret;
601 }
602 
603 static __attribute__((unused, format(printf, 1, 2)))
604 int printf(const char *fmt, ...)
605 {
606 	va_list args;
607 	int ret;
608 
609 	va_start(args, fmt);
610 	ret = vfprintf(stdout, fmt, args);
611 	va_end(args);
612 	return ret;
613 }
614 
615 static __attribute__((unused, format(printf, 2, 0)))
616 int vdprintf(int fd, const char *fmt, va_list args)
617 {
618 	FILE *stream;
619 
620 	stream = fdopen(fd, NULL);
621 	if (!stream)
622 		return -1;
623 	/* Technically 'stream' is leaked, but as it's only a wrapper around 'fd' that is fine */
624 	return vfprintf(stream, fmt, args);
625 }
626 
627 static __attribute__((unused, format(printf, 2, 3)))
628 int dprintf(int fd, const char *fmt, ...)
629 {
630 	va_list args;
631 	int ret;
632 
633 	va_start(args, fmt);
634 	ret = vdprintf(fd, fmt, args);
635 	va_end(args);
636 
637 	return ret;
638 }
639 
640 struct __nolibc_sprintf_cb_state {
641 	char *buf;
642 	size_t space;
643 };
644 
645 static int __nolibc_sprintf_cb(void *v_state, const char *buf, size_t size)
646 {
647 	struct __nolibc_sprintf_cb_state *state = v_state;
648 	size_t space = state->space;
649 	char *tgt;
650 
651 	/* Truncate the request to fit in the output buffer space.
652 	 * The last byte is reserved for the terminating '\0'.
653 	 * state->space can only be zero for snprintf(NULL, 0, fmt, args)
654 	 * so this normally lets through calls with 'size == 0'.
655 	 */
656 	if (size >= space) {
657 		if (space <= 1)
658 			return 0;
659 		size = space - 1;
660 	}
661 	tgt = state->buf;
662 
663 	/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output
664 	 * buffer be '\0' terminated.
665 	 * That will be the only cb() call for, eg, snprintf(buf, sz, "").
666 	 * Zero lengths can occur at other times (eg "%s" for an empty string).
667 	 * Unconditionally write the '\0' byte to reduce code size, it is
668 	 * normally overwritten by the data being output.
669 	 * There is no point adding a '\0' after copied data - there is always
670 	 * another call.
671 	 */
672 	*tgt = '\0';
673 	if (size) {
674 		state->space = space - size;
675 		state->buf = tgt + size;
676 		memcpy(tgt, buf, size);
677 	}
678 
679 	return 0;
680 }
681 
682 static __attribute__((unused, format(printf, 3, 0)))
683 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
684 {
685 	struct __nolibc_sprintf_cb_state state = { .buf = buf, .space = size };
686 
687 	return __nolibc_printf(__nolibc_sprintf_cb, &state, fmt, args);
688 }
689 
690 static __attribute__((unused, format(printf, 3, 4)))
691 int snprintf(char *buf, size_t size, const char *fmt, ...)
692 {
693 	va_list args;
694 	int ret;
695 
696 	va_start(args, fmt);
697 	ret = vsnprintf(buf, size, fmt, args);
698 	va_end(args);
699 
700 	return ret;
701 }
702 
703 static __attribute__((unused, format(printf, 2, 0)))
704 int vsprintf(char *buf, const char *fmt, va_list args)
705 {
706 	return vsnprintf(buf, SIZE_MAX, fmt, args);
707 }
708 
709 static __attribute__((unused, format(printf, 2, 3)))
710 int sprintf(char *buf, const char *fmt, ...)
711 {
712 	va_list args;
713 	int ret;
714 
715 	va_start(args, fmt);
716 	ret = vsprintf(buf, fmt, args);
717 	va_end(args);
718 
719 	return ret;
720 }
721 
722 static __attribute__((unused))
723 int vsscanf(const char *str, const char *format, va_list args)
724 {
725 	uintmax_t uval;
726 	intmax_t ival;
727 	int base;
728 	char *endptr;
729 	int matches;
730 	int lpref;
731 
732 	matches = 0;
733 
734 	while (1) {
735 		if (*format == '%') {
736 			/* start of pattern */
737 			lpref = 0;
738 			format++;
739 
740 			if (*format == 'l') {
741 				/* same as in printf() */
742 				lpref = 1;
743 				format++;
744 				if (*format == 'l') {
745 					lpref = 2;
746 					format++;
747 				}
748 			}
749 
750 			if (*format == '%') {
751 				/* literal % */
752 				if ('%' != *str)
753 					goto done;
754 				str++;
755 				format++;
756 				continue;
757 			} else if (*format == 'd') {
758 				ival = strtoll(str, &endptr, 10);
759 				if (lpref == 0)
760 					*va_arg(args, int *) = ival;
761 				else if (lpref == 1)
762 					*va_arg(args, long *) = ival;
763 				else if (lpref == 2)
764 					*va_arg(args, long long *) = ival;
765 			} else if (*format == 'u' || *format == 'x' || *format == 'X') {
766 				base = *format == 'u' ? 10 : 16;
767 				uval = strtoull(str, &endptr, base);
768 				if (lpref == 0)
769 					*va_arg(args, unsigned int *) = uval;
770 				else if (lpref == 1)
771 					*va_arg(args, unsigned long *) = uval;
772 				else if (lpref == 2)
773 					*va_arg(args, unsigned long long *) = uval;
774 			} else if (*format == 'p') {
775 				*va_arg(args, void **) = (void *)strtoul(str, &endptr, 16);
776 			} else {
777 				SET_ERRNO(EILSEQ);
778 				goto done;
779 			}
780 
781 			format++;
782 			str = endptr;
783 			matches++;
784 
785 		} else if (*format == '\0') {
786 			goto done;
787 		} else if (isspace(*format)) {
788 			/* skip spaces in format and str */
789 			while (isspace(*format))
790 				format++;
791 			while (isspace(*str))
792 				str++;
793 		} else if (*format == *str) {
794 			/* literal match */
795 			format++;
796 			str++;
797 		} else {
798 			if (!matches)
799 				matches = EOF;
800 			goto done;
801 		}
802 	}
803 
804 done:
805 	return matches;
806 }
807 
808 static __attribute__((unused, format(scanf, 2, 3)))
809 int sscanf(const char *str, const char *format, ...)
810 {
811 	va_list args;
812 	int ret;
813 
814 	va_start(args, format);
815 	ret = vsscanf(str, format, args);
816 	va_end(args);
817 	return ret;
818 }
819 
820 static __attribute__((unused))
821 void perror(const char *msg)
822 {
823 #ifdef NOLIBC_IGNORE_ERRNO
824 	fprintf(stderr, "%s%sunknown error\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "");
825 #else
826 	fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
827 #endif
828 }
829 
830 static __attribute__((unused))
831 int setvbuf(FILE *stream __attribute__((unused)),
832 	    char *buf __attribute__((unused)),
833 	    int mode,
834 	    size_t size __attribute__((unused)))
835 {
836 	/*
837 	 * nolibc does not support buffering so this is a nop. Just check mode
838 	 * is valid as required by the spec.
839 	 */
840 	switch (mode) {
841 	case _IOFBF:
842 	case _IOLBF:
843 	case _IONBF:
844 		break;
845 	default:
846 		return EOF;
847 	}
848 
849 	return 0;
850 }
851 
852 static __attribute__((unused))
853 int strerror_r(int errnum, char *buf, size_t buflen)
854 {
855 	if (buflen < 18)
856 		return ERANGE;
857 
858 	__builtin_memcpy(buf, "errno=", 6);
859 	i64toa_r(errnum, buf + 6);
860 	return 0;
861 }
862 
863 static __attribute__((unused))
864 const char *strerror(int errnum)
865 {
866 	static char buf[18];
867 	char *b = buf;
868 
869 	/* Force gcc to use 'register offset' to access buf[]. */
870 	_NOLIBC_OPTIMIZER_HIDE_VAR(b);
871 
872 	/* Use strerror_r() to avoid having the only .data in small programs. */
873 	strerror_r(errnum, b, sizeof(buf));
874 
875 	return b;
876 }
877 
878 #endif /* _NOLIBC_STDIO_H */
879