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
|
|
|
|
|
2022-02-08 00:23:33 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2022-02-08 00:23:30 +08:00
|
|
|
#include "std.h"
|
|
|
|
#include "arch.h"
|
2022-02-08 00:23:48 +08:00
|
|
|
#include "errno.h"
|
2022-02-08 00:23:30 +08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-08 00:23:32 +08: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-08 00:23:31 +08:00
|
|
|
|
2022-02-08 00:23:32 +08: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-08 00:23:31 +08:00
|
|
|
static __attribute__((unused))
|
2022-02-08 00:23:32 +08:00
|
|
|
int _fwrite(const void *buf, size_t size, FILE *stream)
|
2022-02-08 00:23:30 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-02-08 00:23:32 +08:00
|
|
|
while (size) {
|
|
|
|
ret = write(fd, buf, size);
|
2022-02-08 00:23:30 +08:00
|
|
|
if (ret <= 0)
|
|
|
|
return EOF;
|
2022-02-08 00:23:32 +08:00
|
|
|
size -= ret;
|
|
|
|
buf += ret;
|
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
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-08 00:23:32 +08: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-08 00:23:31 +08:00
|
|
|
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:33 +08:00
|
|
|
|
|
|
|
/* minimal vfprintf(). It supports the following formats:
|
2022-03-22 01:33:09 +08:00
|
|
|
* - %[l*]{d,u,c,x,p}
|
2022-02-08 00:23:33 +08:00
|
|
|
* - %s
|
|
|
|
* - unknown modifiers are ignored.
|
|
|
|
*/
|
|
|
|
static __attribute__((unused))
|
|
|
|
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-22 01:33:09 +08: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-08 00:23:33 +08: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-22 01:33:09 +08:00
|
|
|
case 'c':
|
|
|
|
out[0] = v;
|
|
|
|
out[1] = 0;
|
|
|
|
break;
|
2022-02-08 00:23:33 +08:00
|
|
|
case 'd':
|
2022-03-22 01:33:09 +08:00
|
|
|
i64toa_r(v, out);
|
2022-02-08 00:23:33 +08:00
|
|
|
break;
|
|
|
|
case 'u':
|
2022-03-22 01:33:09 +08:00
|
|
|
u64toa_r(v, out);
|
2022-02-08 00:23:33 +08:00
|
|
|
break;
|
2022-03-22 01:33:09 +08:00
|
|
|
case 'p':
|
|
|
|
*(out++) = '0';
|
|
|
|
*(out++) = 'x';
|
|
|
|
/* fall through */
|
|
|
|
default: /* 'x' and 'p' above */
|
|
|
|
u64toh_r(v, out);
|
2022-02-08 00:23:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
outstr = tmpbuf;
|
|
|
|
}
|
|
|
|
else if (c == 's') {
|
|
|
|
outstr = va_arg(args, char *);
|
2022-03-22 01:33:07 +08:00
|
|
|
if (!outstr)
|
|
|
|
outstr="(null)";
|
2022-02-08 00:23:33 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
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-08 00:23:35 +08:00
|
|
|
static __attribute__((unused))
|
|
|
|
void perror(const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
|
|
|
|
}
|
|
|
|
|
2022-02-08 00:23:30 +08:00
|
|
|
#endif /* _NOLIBC_STDIO_H */
|