2022-02-08 00:23:30 +08: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"
|
|
|
|
#include "types.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#ifndef EOF
|
|
|
|
#define EOF (-1)
|
|
|
|
#endif
|
|
|
|
|
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-08 00:23:31 +08:00
|
|
|
/* just define FILE as a non-empty type */
|
|
|
|
typedef struct FILE {
|
|
|
|
char dummy[1];
|
|
|
|
} FILE;
|
|
|
|
|
|
|
|
/* We define the 3 common stdio files as constant invalid pointers that
|
|
|
|
* are easily recognized.
|
|
|
|
*/
|
|
|
|
static __attribute__((unused)) FILE* const stdin = (FILE*)-3;
|
|
|
|
static __attribute__((unused)) FILE* const stdout = (FILE*)-2;
|
|
|
|
static __attribute__((unused)) FILE* const stderr = (FILE*)-1;
|
|
|
|
|
|
|
|
/* getc(), fgetc(), getchar() */
|
|
|
|
|
|
|
|
#define getc(stream) fgetc(stream)
|
|
|
|
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08:00
|
|
|
int fgetc(FILE* stream)
|
2022-02-08 00:23:30 +08:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
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-08 00:23:31 +08:00
|
|
|
int fd;
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08:00
|
|
|
if (stream < stdin || stream > stderr)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
fd = 3 + (long)stream;
|
|
|
|
|
|
|
|
if (read(fd, &ch, 1) <= 0)
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08: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-08 00:23:30 +08: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-08 00:23:31 +08:00
|
|
|
int fd;
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08:00
|
|
|
if (stream < stdin || stream > stderr)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
fd = 3 + (long)stream;
|
|
|
|
|
|
|
|
if (write(fd, &ch, 1) <= 0)
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08:00
|
|
|
int putchar(int c)
|
|
|
|
{
|
|
|
|
return fputc(c, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
int fputs(const char *s, FILE *stream)
|
2022-02-08 00:23:30 +08:00
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
ssize_t ret;
|
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-08 00:23:31 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (stream < stdin || stream > stderr)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
fd = 3 + (long)stream;
|
2022-02-08 00:23:30 +08:00
|
|
|
|
|
|
|
while (len > 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-08 00:23:31 +08:00
|
|
|
ret = write(fd, s, len);
|
2022-02-08 00:23:30 +08:00
|
|
|
if (ret <= 0)
|
|
|
|
return EOF;
|
|
|
|
s += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
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-08 00:23:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
int puts(const char *s)
|
|
|
|
{
|
|
|
|
if (fputs(s, stdout) == EOF)
|
|
|
|
return EOF;
|
2022-02-08 00:23:30 +08: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-08 00:23:31 +08: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-08 00:23:30 +08:00
|
|
|
#endif /* _NOLIBC_STDIO_H */
|