Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / include / nolibc / stdio.h
blob3892034198dd566d21a5cc0a9f67cf097d428393
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 */
7 #ifndef _NOLIBC_STDIO_H
8 #define _NOLIBC_STDIO_H
10 #include "std.h"
11 #include "arch.h"
12 #include "errno.h"
13 #include "types.h"
14 #include "sys.h"
15 #include "stdarg.h"
16 #include "stdlib.h"
17 #include "string.h"
18 #include "compiler.h"
20 #ifndef EOF
21 #define EOF (-1)
22 #endif
24 /* Buffering mode used by setvbuf. */
25 #define _IOFBF 0 /* Fully buffered. */
26 #define _IOLBF 1 /* Line buffered. */
27 #define _IONBF 2 /* No buffering. */
29 /* just define FILE as a non-empty type. The value of the pointer gives
30 * the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE
31 * are immediately identified as abnormal entries (i.e. possible copies
32 * of valid pointers to something else).
34 typedef struct FILE {
35 char dummy[1];
36 } FILE;
38 static __attribute__((unused)) FILE* const stdin = (FILE*)(intptr_t)~STDIN_FILENO;
39 static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO;
40 static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_FILENO;
42 /* provides a FILE* equivalent of fd. The mode is ignored. */
43 static __attribute__((unused))
44 FILE *fdopen(int fd, const char *mode __attribute__((unused)))
46 if (fd < 0) {
47 SET_ERRNO(EBADF);
48 return NULL;
50 return (FILE*)(intptr_t)~fd;
53 /* provides the fd of stream. */
54 static __attribute__((unused))
55 int fileno(FILE *stream)
57 intptr_t i = (intptr_t)stream;
59 if (i >= 0) {
60 SET_ERRNO(EBADF);
61 return -1;
63 return ~i;
66 /* flush a stream. */
67 static __attribute__((unused))
68 int fflush(FILE *stream)
70 intptr_t i = (intptr_t)stream;
72 /* NULL is valid here. */
73 if (i > 0) {
74 SET_ERRNO(EBADF);
75 return -1;
78 /* Don't do anything, nolibc does not support buffering. */
79 return 0;
82 /* flush a stream. */
83 static __attribute__((unused))
84 int fclose(FILE *stream)
86 intptr_t i = (intptr_t)stream;
88 if (i >= 0) {
89 SET_ERRNO(EBADF);
90 return -1;
93 if (close(~i))
94 return EOF;
96 return 0;
99 /* getc(), fgetc(), getchar() */
101 #define getc(stream) fgetc(stream)
103 static __attribute__((unused))
104 int fgetc(FILE* stream)
106 unsigned char ch;
108 if (read(fileno(stream), &ch, 1) <= 0)
109 return EOF;
110 return ch;
113 static __attribute__((unused))
114 int getchar(void)
116 return fgetc(stdin);
120 /* putc(), fputc(), putchar() */
122 #define putc(c, stream) fputc(c, stream)
124 static __attribute__((unused))
125 int fputc(int c, FILE* stream)
127 unsigned char ch = c;
129 if (write(fileno(stream), &ch, 1) <= 0)
130 return EOF;
131 return ch;
134 static __attribute__((unused))
135 int putchar(int c)
137 return fputc(c, stdout);
141 /* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
143 /* internal fwrite()-like function which only takes a size and returns 0 on
144 * success or EOF on error. It automatically retries on short writes.
146 static __attribute__((unused))
147 int _fwrite(const void *buf, size_t size, FILE *stream)
149 ssize_t ret;
150 int fd = fileno(stream);
152 while (size) {
153 ret = write(fd, buf, size);
154 if (ret <= 0)
155 return EOF;
156 size -= ret;
157 buf += ret;
159 return 0;
162 static __attribute__((unused))
163 size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
165 size_t written;
167 for (written = 0; written < nmemb; written++) {
168 if (_fwrite(s, size, stream) != 0)
169 break;
170 s += size;
172 return written;
175 static __attribute__((unused))
176 int fputs(const char *s, FILE *stream)
178 return _fwrite(s, strlen(s), stream);
181 static __attribute__((unused))
182 int puts(const char *s)
184 if (fputs(s, stdout) == EOF)
185 return EOF;
186 return putchar('\n');
190 /* fgets() */
191 static __attribute__((unused))
192 char *fgets(char *s, int size, FILE *stream)
194 int ofs;
195 int c;
197 for (ofs = 0; ofs + 1 < size;) {
198 c = fgetc(stream);
199 if (c == EOF)
200 break;
201 s[ofs++] = c;
202 if (c == '\n')
203 break;
205 if (ofs < size)
206 s[ofs] = 0;
207 return ofs ? s : NULL;
211 /* minimal vfprintf(). It supports the following formats:
212 * - %[l*]{d,u,c,x,p}
213 * - %s
214 * - unknown modifiers are ignored.
216 static __attribute__((unused, format(printf, 2, 0)))
217 int vfprintf(FILE *stream, const char *fmt, va_list args)
219 char escape, lpref, c;
220 unsigned long long v;
221 unsigned int written;
222 size_t len, ofs;
223 char tmpbuf[21];
224 const char *outstr;
226 written = ofs = escape = lpref = 0;
227 while (1) {
228 c = fmt[ofs++];
230 if (escape) {
231 /* we're in an escape sequence, ofs == 1 */
232 escape = 0;
233 if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') {
234 char *out = tmpbuf;
236 if (c == 'p')
237 v = va_arg(args, unsigned long);
238 else if (lpref) {
239 if (lpref > 1)
240 v = va_arg(args, unsigned long long);
241 else
242 v = va_arg(args, unsigned long);
243 } else
244 v = va_arg(args, unsigned int);
246 if (c == 'd') {
247 /* sign-extend the value */
248 if (lpref == 0)
249 v = (long long)(int)v;
250 else if (lpref == 1)
251 v = (long long)(long)v;
254 switch (c) {
255 case 'c':
256 out[0] = v;
257 out[1] = 0;
258 break;
259 case 'd':
260 i64toa_r(v, out);
261 break;
262 case 'u':
263 u64toa_r(v, out);
264 break;
265 case 'p':
266 *(out++) = '0';
267 *(out++) = 'x';
268 __nolibc_fallthrough;
269 default: /* 'x' and 'p' above */
270 u64toh_r(v, out);
271 break;
273 outstr = tmpbuf;
275 else if (c == 's') {
276 outstr = va_arg(args, char *);
277 if (!outstr)
278 outstr="(null)";
280 else if (c == '%') {
281 /* queue it verbatim */
282 continue;
284 else {
285 /* modifiers or final 0 */
286 if (c == 'l') {
287 /* long format prefix, maintain the escape */
288 lpref++;
290 escape = 1;
291 goto do_escape;
293 len = strlen(outstr);
294 goto flush_str;
297 /* not an escape sequence */
298 if (c == 0 || c == '%') {
299 /* flush pending data on escape or end */
300 escape = 1;
301 lpref = 0;
302 outstr = fmt;
303 len = ofs - 1;
304 flush_str:
305 if (_fwrite(outstr, len, stream) != 0)
306 break;
308 written += len;
309 do_escape:
310 if (c == 0)
311 break;
312 fmt += ofs;
313 ofs = 0;
314 continue;
317 /* literal char, just queue it */
319 return written;
322 static __attribute__((unused, format(printf, 1, 0)))
323 int vprintf(const char *fmt, va_list args)
325 return vfprintf(stdout, fmt, args);
328 static __attribute__((unused, format(printf, 2, 3)))
329 int fprintf(FILE *stream, const char *fmt, ...)
331 va_list args;
332 int ret;
334 va_start(args, fmt);
335 ret = vfprintf(stream, fmt, args);
336 va_end(args);
337 return ret;
340 static __attribute__((unused, format(printf, 1, 2)))
341 int printf(const char *fmt, ...)
343 va_list args;
344 int ret;
346 va_start(args, fmt);
347 ret = vfprintf(stdout, fmt, args);
348 va_end(args);
349 return ret;
352 static __attribute__((unused))
353 void perror(const char *msg)
355 fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
358 static __attribute__((unused))
359 int setvbuf(FILE *stream __attribute__((unused)),
360 char *buf __attribute__((unused)),
361 int mode,
362 size_t size __attribute__((unused)))
365 * nolibc does not support buffering so this is a nop. Just check mode
366 * is valid as required by the spec.
368 switch (mode) {
369 case _IOFBF:
370 case _IOLBF:
371 case _IONBF:
372 break;
373 default:
374 return EOF;
377 return 0;
380 static __attribute__((unused))
381 const char *strerror(int errno)
383 static char buf[18] = "errno=";
385 i64toa_r(errno, &buf[6]);
387 return buf;
390 /* make sure to include all global symbols */
391 #include "nolibc.h"
393 #endif /* _NOLIBC_STDIO_H */