2022-02-07 17:23:30 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
|
|
|
/*
|
|
|
|
* minimal stdio function definitions for NOLIBC
|
|
|
|
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NOLIBC_STDIO_H
|
|
|
|
#define _NOLIBC_STDIO_H
|
|
|
|
|
|
|
|
#include "std.h"
|
|
|
|
#include "arch.h"
|
2022-02-07 17:23:48 +01:00
|
|
|
#include "errno.h"
|
2022-02-07 17:23:30 +01:00
|
|
|
#include "types.h"
|
|
|
|
#include "sys.h"
|
2023-08-30 17:07:12 +02:00
|
|
|
#include "stdarg.h"
|
2022-02-07 17:23:30 +01:00
|
|
|
#include "stdlib.h"
|
|
|
|
#include "string.h"
|
2024-09-30 07:35:19 +02:00
|
|
|
#include "compiler.h"
|
2022-02-07 17:23:30 +01:00
|
|
|
|
|
|
|
#ifndef EOF
|
|
|
|
#define EOF (-1)
|
|
|
|
#endif
|
|
|
|
|
tools/nolibc/stdio: add setvbuf() to set buffering mode
Add a minimal implementation of setvbuf(), which error checks the mode
argument (as required by spec) and returns. Since nolibc never buffers
output, nothing needs to be done.
The kselftest framework recently added a call to setvbuf(). As a result,
any tests that use the kselftest framework and nolibc cause a compiler
error due to missing function. This provides an urgent fix for the
problem which is preventing arm64 testing on linux-next.
Example:
clang --target=aarch64-linux-gnu -fintegrated-as
-Werror=unknown-warning-option -Werror=ignored-optimization-argument
-Werror=option-ignored -Werror=unused-command-line-argument
--target=aarch64-linux-gnu -fintegrated-as
-fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
-include ../../../../include/nolibc/nolibc.h -I../..\
-static -ffreestanding -Wall za-fork.c
build/kselftest/arm64/fp/za-fork-asm.o
-o build/kselftest/arm64/fp/za-fork
In file included from <built-in>:1:
In file included from ./../../../../include/nolibc/nolibc.h:97:
In file included from ./../../../../include/nolibc/arch.h:25:
./../../../../include/nolibc/arch-aarch64.h:178:35: warning: unknown
attribute 'optimize' ignored [-Wunknown-attributes]
void __attribute__((weak,noreturn,optimize("omit-frame-pointer")))
__no_stack_protector _start(void)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from za-fork.c:12:
../../kselftest.h:123:2: error: call to undeclared function 'setvbuf';
ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
setvbuf(stdout, NULL, _IOLBF, 0);
^
../../kselftest.h:123:24: error: use of undeclared identifier '_IOLBF'
setvbuf(stdout, NULL, _IOLBF, 0);
^
1 warning and 2 errors generated.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Link: https://lore.kernel.org/linux-kselftest/CA+G9fYus3Z8r2cg3zLv8uH8MRrzLFVWdnor02SNr=rCz+_WGVg@mail.gmail.com/
Reviewed-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
2023-07-26 08:06:55 +01:00
|
|
|
/* Buffering mode used by setvbuf. */
|
|
|
|
#define _IOFBF 0 /* Fully buffered. */
|
|
|
|
#define _IOLBF 1 /* Line buffered. */
|
|
|
|
#define _IONBF 2 /* No buffering. */
|
|
|
|
|
2023-04-02 20:48:05 +02:00
|
|
|
/* just define FILE as a non-empty type. The value of the pointer gives
|
|
|
|
* the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE
|
|
|
|
* are immediately identified as abnormal entries (i.e. possible copies
|
|
|
|
* of valid pointers to something else).
|
|
|
|
*/
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
typedef struct FILE {
|
|
|
|
char dummy[1];
|
|
|
|
} FILE;
|
|
|
|
|
2023-04-02 20:48:05 +02:00
|
|
|
static __attribute__((unused)) FILE* const stdin = (FILE*)(intptr_t)~STDIN_FILENO;
|
|
|
|
static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO;
|
|
|
|
static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_FILENO;
|
|
|
|
|
|
|
|
/* provides a FILE* equivalent of fd. The mode is ignored. */
|
|
|
|
static __attribute__((unused))
|
|
|
|
FILE *fdopen(int fd, const char *mode __attribute__((unused)))
|
|
|
|
{
|
|
|
|
if (fd < 0) {
|
|
|
|
SET_ERRNO(EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (FILE*)(intptr_t)~fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* provides the fd of stream. */
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fileno(FILE *stream)
|
|
|
|
{
|
|
|
|
intptr_t i = (intptr_t)stream;
|
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
SET_ERRNO(EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ~i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush a stream. */
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fflush(FILE *stream)
|
|
|
|
{
|
|
|
|
intptr_t i = (intptr_t)stream;
|
|
|
|
|
|
|
|
/* NULL is valid here. */
|
|
|
|
if (i > 0) {
|
|
|
|
SET_ERRNO(EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't do anything, nolibc does not support buffering. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush a stream. */
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fclose(FILE *stream)
|
|
|
|
{
|
|
|
|
intptr_t i = (intptr_t)stream;
|
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
SET_ERRNO(EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(~i))
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
|
|
|
|
/* getc(), fgetc(), getchar() */
|
|
|
|
|
|
|
|
#define getc(stream) fgetc(stream)
|
|
|
|
|
2022-02-07 17:23:30 +01:00
|
|
|
static __attribute__((unused))
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
int fgetc(FILE* stream)
|
2022-02-07 17:23:30 +01:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
|
2023-04-02 20:48:05 +02:00
|
|
|
if (read(fileno(stream), &ch, 1) <= 0)
|
2022-02-07 17:23:30 +01:00
|
|
|
return EOF;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
int getchar(void)
|
|
|
|
{
|
|
|
|
return fgetc(stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* putc(), fputc(), putchar() */
|
|
|
|
|
|
|
|
#define putc(c, stream) fputc(c, stream)
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fputc(int c, FILE* stream)
|
2022-02-07 17:23:30 +01:00
|
|
|
{
|
|
|
|
unsigned char ch = c;
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
|
2023-04-02 20:48:05 +02:00
|
|
|
if (write(fileno(stream), &ch, 1) <= 0)
|
2022-02-07 17:23:30 +01:00
|
|
|
return EOF;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
int putchar(int c)
|
|
|
|
{
|
|
|
|
return fputc(c, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-07 17:23:32 +01:00
|
|
|
/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
|
2022-02-07 17:23:32 +01:00
|
|
|
/* internal fwrite()-like function which only takes a size and returns 0 on
|
|
|
|
* success or EOF on error. It automatically retries on short writes.
|
|
|
|
*/
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
static __attribute__((unused))
|
2022-02-07 17:23:32 +01:00
|
|
|
int _fwrite(const void *buf, size_t size, FILE *stream)
|
2022-02-07 17:23:30 +01:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
2023-04-02 20:48:05 +02:00
|
|
|
int fd = fileno(stream);
|
2022-02-07 17:23:30 +01:00
|
|
|
|
2022-02-07 17:23:32 +01:00
|
|
|
while (size) {
|
|
|
|
ret = write(fd, buf, size);
|
2022-02-07 17:23:30 +01:00
|
|
|
if (ret <= 0)
|
|
|
|
return EOF;
|
2022-02-07 17:23:32 +01:00
|
|
|
size -= ret;
|
|
|
|
buf += ret;
|
2022-02-07 17:23:30 +01:00
|
|
|
}
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:23:32 +01:00
|
|
|
static __attribute__((unused))
|
|
|
|
size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
|
|
|
|
{
|
|
|
|
size_t written;
|
|
|
|
|
|
|
|
for (written = 0; written < nmemb; written++) {
|
|
|
|
if (_fwrite(s, size, stream) != 0)
|
|
|
|
break;
|
|
|
|
s += size;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fputs(const char *s, FILE *stream)
|
|
|
|
{
|
|
|
|
return _fwrite(s, strlen(s), stream);
|
|
|
|
}
|
|
|
|
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
static __attribute__((unused))
|
|
|
|
int puts(const char *s)
|
|
|
|
{
|
|
|
|
if (fputs(s, stdout) == EOF)
|
|
|
|
return EOF;
|
2022-02-07 17:23:30 +01:00
|
|
|
return putchar('\n');
|
|
|
|
}
|
|
|
|
|
tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.
This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.
>From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.
There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-02-07 17:23:31 +01:00
|
|
|
|
|
|
|
/* fgets() */
|
|
|
|
static __attribute__((unused))
|
|
|
|
char *fgets(char *s, int size, FILE *stream)
|
|
|
|
{
|
|
|
|
int ofs;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (ofs = 0; ofs + 1 < size;) {
|
|
|
|
c = fgetc(stream);
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
s[ofs++] = c;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ofs < size)
|
|
|
|
s[ofs] = 0;
|
|
|
|
return ofs ? s : NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:23:33 +01:00
|
|
|
|
|
|
|
/* minimal vfprintf(). It supports the following formats:
|
2022-03-21 18:33:09 +01:00
|
|
|
* - %[l*]{d,u,c,x,p}
|
2022-02-07 17:23:33 +01:00
|
|
|
* - %s
|
|
|
|
* - unknown modifiers are ignored.
|
|
|
|
*/
|
2023-11-23 22:53:13 +01:00
|
|
|
static __attribute__((unused, format(printf, 2, 0)))
|
2022-02-07 17:23:33 +01:00
|
|
|
int vfprintf(FILE *stream, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
char escape, lpref, c;
|
|
|
|
unsigned long long v;
|
|
|
|
unsigned int written;
|
|
|
|
size_t len, ofs;
|
|
|
|
char tmpbuf[21];
|
|
|
|
const char *outstr;
|
|
|
|
|
|
|
|
written = ofs = escape = lpref = 0;
|
|
|
|
while (1) {
|
|
|
|
c = fmt[ofs++];
|
|
|
|
|
|
|
|
if (escape) {
|
|
|
|
/* we're in an escape sequence, ofs == 1 */
|
|
|
|
escape = 0;
|
2022-03-21 18:33:09 +01:00
|
|
|
if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') {
|
|
|
|
char *out = tmpbuf;
|
|
|
|
|
|
|
|
if (c == 'p')
|
|
|
|
v = va_arg(args, unsigned long);
|
|
|
|
else if (lpref) {
|
2022-02-07 17:23:33 +01:00
|
|
|
if (lpref > 1)
|
|
|
|
v = va_arg(args, unsigned long long);
|
|
|
|
else
|
|
|
|
v = va_arg(args, unsigned long);
|
|
|
|
} else
|
|
|
|
v = va_arg(args, unsigned int);
|
|
|
|
|
|
|
|
if (c == 'd') {
|
|
|
|
/* sign-extend the value */
|
|
|
|
if (lpref == 0)
|
|
|
|
v = (long long)(int)v;
|
|
|
|
else if (lpref == 1)
|
|
|
|
v = (long long)(long)v;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
2022-03-21 18:33:09 +01:00
|
|
|
case 'c':
|
|
|
|
out[0] = v;
|
|
|
|
out[1] = 0;
|
|
|
|
break;
|
2022-02-07 17:23:33 +01:00
|
|
|
case 'd':
|
2022-03-21 18:33:09 +01:00
|
|
|
i64toa_r(v, out);
|
2022-02-07 17:23:33 +01:00
|
|
|
break;
|
|
|
|
case 'u':
|
2022-03-21 18:33:09 +01:00
|
|
|
u64toa_r(v, out);
|
2022-02-07 17:23:33 +01:00
|
|
|
break;
|
2022-03-21 18:33:09 +01:00
|
|
|
case 'p':
|
|
|
|
*(out++) = '0';
|
|
|
|
*(out++) = 'x';
|
2024-09-30 07:35:19 +02:00
|
|
|
__nolibc_fallthrough;
|
2022-03-21 18:33:09 +01:00
|
|
|
default: /* 'x' and 'p' above */
|
|
|
|
u64toh_r(v, out);
|
2022-02-07 17:23:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
outstr = tmpbuf;
|
|
|
|
}
|
|
|
|
else if (c == 's') {
|
|
|
|
outstr = va_arg(args, char *);
|
2022-03-21 18:33:07 +01:00
|
|
|
if (!outstr)
|
|
|
|
outstr="(null)";
|
2022-02-07 17:23:33 +01:00
|
|
|
}
|
|
|
|
else if (c == '%') {
|
|
|
|
/* queue it verbatim */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* modifiers or final 0 */
|
|
|
|
if (c == 'l') {
|
|
|
|
/* long format prefix, maintain the escape */
|
|
|
|
lpref++;
|
|
|
|
}
|
|
|
|
escape = 1;
|
|
|
|
goto do_escape;
|
|
|
|
}
|
|
|
|
len = strlen(outstr);
|
|
|
|
goto flush_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not an escape sequence */
|
|
|
|
if (c == 0 || c == '%') {
|
|
|
|
/* flush pending data on escape or end */
|
|
|
|
escape = 1;
|
|
|
|
lpref = 0;
|
|
|
|
outstr = fmt;
|
|
|
|
len = ofs - 1;
|
|
|
|
flush_str:
|
|
|
|
if (_fwrite(outstr, len, stream) != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
written += len;
|
|
|
|
do_escape:
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
fmt += ofs;
|
|
|
|
ofs = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* literal char, just queue it */
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:53:13 +01:00
|
|
|
static __attribute__((unused, format(printf, 1, 0)))
|
2023-04-06 17:19:10 +01:00
|
|
|
int vprintf(const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
return vfprintf(stdout, fmt, args);
|
|
|
|
}
|
|
|
|
|
2022-05-20 00:21:16 +07:00
|
|
|
static __attribute__((unused, format(printf, 2, 3)))
|
2022-02-07 17:23:33 +01:00
|
|
|
int fprintf(FILE *stream, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = vfprintf(stream, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-20 00:21:16 +07:00
|
|
|
static __attribute__((unused, format(printf, 1, 2)))
|
2022-02-07 17:23:33 +01:00
|
|
|
int printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = vfprintf(stdout, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:23:35 +01:00
|
|
|
static __attribute__((unused))
|
|
|
|
void perror(const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
|
|
|
|
}
|
|
|
|
|
tools/nolibc/stdio: add setvbuf() to set buffering mode
Add a minimal implementation of setvbuf(), which error checks the mode
argument (as required by spec) and returns. Since nolibc never buffers
output, nothing needs to be done.
The kselftest framework recently added a call to setvbuf(). As a result,
any tests that use the kselftest framework and nolibc cause a compiler
error due to missing function. This provides an urgent fix for the
problem which is preventing arm64 testing on linux-next.
Example:
clang --target=aarch64-linux-gnu -fintegrated-as
-Werror=unknown-warning-option -Werror=ignored-optimization-argument
-Werror=option-ignored -Werror=unused-command-line-argument
--target=aarch64-linux-gnu -fintegrated-as
-fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
-include ../../../../include/nolibc/nolibc.h -I../..\
-static -ffreestanding -Wall za-fork.c
build/kselftest/arm64/fp/za-fork-asm.o
-o build/kselftest/arm64/fp/za-fork
In file included from <built-in>:1:
In file included from ./../../../../include/nolibc/nolibc.h:97:
In file included from ./../../../../include/nolibc/arch.h:25:
./../../../../include/nolibc/arch-aarch64.h:178:35: warning: unknown
attribute 'optimize' ignored [-Wunknown-attributes]
void __attribute__((weak,noreturn,optimize("omit-frame-pointer")))
__no_stack_protector _start(void)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from za-fork.c:12:
../../kselftest.h:123:2: error: call to undeclared function 'setvbuf';
ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
setvbuf(stdout, NULL, _IOLBF, 0);
^
../../kselftest.h:123:24: error: use of undeclared identifier '_IOLBF'
setvbuf(stdout, NULL, _IOLBF, 0);
^
1 warning and 2 errors generated.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Link: https://lore.kernel.org/linux-kselftest/CA+G9fYus3Z8r2cg3zLv8uH8MRrzLFVWdnor02SNr=rCz+_WGVg@mail.gmail.com/
Reviewed-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
2023-07-26 08:06:55 +01:00
|
|
|
static __attribute__((unused))
|
2023-08-03 09:28:47 +02:00
|
|
|
int setvbuf(FILE *stream __attribute__((unused)),
|
|
|
|
char *buf __attribute__((unused)),
|
|
|
|
int mode,
|
|
|
|
size_t size __attribute__((unused)))
|
tools/nolibc/stdio: add setvbuf() to set buffering mode
Add a minimal implementation of setvbuf(), which error checks the mode
argument (as required by spec) and returns. Since nolibc never buffers
output, nothing needs to be done.
The kselftest framework recently added a call to setvbuf(). As a result,
any tests that use the kselftest framework and nolibc cause a compiler
error due to missing function. This provides an urgent fix for the
problem which is preventing arm64 testing on linux-next.
Example:
clang --target=aarch64-linux-gnu -fintegrated-as
-Werror=unknown-warning-option -Werror=ignored-optimization-argument
-Werror=option-ignored -Werror=unused-command-line-argument
--target=aarch64-linux-gnu -fintegrated-as
-fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
-include ../../../../include/nolibc/nolibc.h -I../..\
-static -ffreestanding -Wall za-fork.c
build/kselftest/arm64/fp/za-fork-asm.o
-o build/kselftest/arm64/fp/za-fork
In file included from <built-in>:1:
In file included from ./../../../../include/nolibc/nolibc.h:97:
In file included from ./../../../../include/nolibc/arch.h:25:
./../../../../include/nolibc/arch-aarch64.h:178:35: warning: unknown
attribute 'optimize' ignored [-Wunknown-attributes]
void __attribute__((weak,noreturn,optimize("omit-frame-pointer")))
__no_stack_protector _start(void)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from za-fork.c:12:
../../kselftest.h:123:2: error: call to undeclared function 'setvbuf';
ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
setvbuf(stdout, NULL, _IOLBF, 0);
^
../../kselftest.h:123:24: error: use of undeclared identifier '_IOLBF'
setvbuf(stdout, NULL, _IOLBF, 0);
^
1 warning and 2 errors generated.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Link: https://lore.kernel.org/linux-kselftest/CA+G9fYus3Z8r2cg3zLv8uH8MRrzLFVWdnor02SNr=rCz+_WGVg@mail.gmail.com/
Reviewed-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
2023-07-26 08:06:55 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* nolibc does not support buffering so this is a nop. Just check mode
|
|
|
|
* is valid as required by the spec.
|
|
|
|
*/
|
|
|
|
switch (mode) {
|
|
|
|
case _IOFBF:
|
|
|
|
case _IOLBF:
|
|
|
|
case _IONBF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-26 13:08:57 +02:00
|
|
|
static __attribute__((unused))
|
|
|
|
const char *strerror(int errno)
|
|
|
|
{
|
|
|
|
static char buf[18] = "errno=";
|
|
|
|
|
|
|
|
i64toa_r(errno, &buf[6]);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-01-09 08:54:40 +01:00
|
|
|
/* make sure to include all global symbols */
|
|
|
|
#include "nolibc.h"
|
|
|
|
|
2022-02-07 17:23:30 +01:00
|
|
|
#endif /* _NOLIBC_STDIO_H */
|