libc: Make perror/putchar/getchar/puts work without CONFIG_FILE_STREAM

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2021-11-15 03:25:15 +08:00
committed by David Sidrane
parent 6dd4d5de15
commit bd2327cc2d
5 changed files with 43 additions and 4 deletions
+5 -4
View File
@@ -24,6 +24,7 @@
CSRCS += lib_fileno.c lib_printf.c lib_sprintf.c lib_asprintf.c
CSRCS += lib_snprintf.c lib_libsprintf.c lib_vsprintf.c lib_vasprintf.c
CSRCS += lib_vsnprintf.c lib_dprintf.c lib_vdprintf.c
CSRCS += lib_perror.c lib_putchar.c lib_getchar.c lib_puts.c
CSRCS += lib_meminstream.c lib_memoutstream.c lib_memsistream.c
CSRCS += lib_memsostream.c lib_lowoutstream.c
CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c
@@ -39,13 +40,13 @@ CSRCS += lib_tempnam.c lib_tmpnam.c
ifeq ($(CONFIG_FILE_STREAM),y)
CSRCS += lib_fopen.c lib_freopen.c lib_fclose.c lib_fread.c lib_libfread.c
CSRCS += lib_fseek.c lib_fseeko.c lib_ftell.c lib_ftello.c lib_fsetpos.c
CSRCS += lib_getdelim.c lib_fgetpos.c lib_getc.c lib_getchar.c lib_fgetc.c
CSRCS += lib_getdelim.c lib_fgetpos.c lib_getc.c lib_fgetc.c
CSRCS += lib_fgets.c lib_gets_s.c lib_gets.c lib_libfgets.c lib_fwrite.c
CSRCS += lib_libfwrite.c lib_fflush.c lib_libflushall.c lib_libfflush.c
CSRCS += lib_rdflush.c lib_wrflush.c lib_putc.c lib_putchar.c lib_fputc.c
CSRCS += lib_puts.c lib_fputs.c lib_ungetc.c lib_vprintf.c lib_fprintf.c
CSRCS += lib_rdflush.c lib_wrflush.c lib_putc.c lib_fputc.c
CSRCS += lib_fputs.c lib_ungetc.c lib_vprintf.c lib_fprintf.c
CSRCS += lib_vfprintf.c lib_stdinstream.c lib_stdoutstream.c lib_stdsistream.c
CSRCS += lib_stdsostream.c lib_perror.c lib_feof.c lib_ferror.c
CSRCS += lib_stdsostream.c lib_feof.c lib_ferror.c
CSRCS += lib_rawinstream.c lib_rawoutstream.c lib_rawsistream.c
CSRCS += lib_rawsostream.c lib_remove.c lib_rewind.c lib_clearerr.c
CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c
+6
View File
@@ -23,6 +23,7 @@
****************************************************************************/
#include <stdio.h>
#include <unistd.h>
/****************************************************************************
* Public Functions
@@ -30,5 +31,10 @@
int getchar(void)
{
#ifdef CONFIG_FILE_STREAM
return fgetc(stdin);
#else
unsigned char c;
return read(STDIN_FILENO, &c, 1) == 1 ? c : EOF;
#endif
}
+11
View File
@@ -27,6 +27,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
/****************************************************************************
* Pre-processor Definitions
@@ -39,8 +40,10 @@
#ifdef CONFIG_LIBC_PERROR_STDOUT
# define PERROR_STREAM stdout
# define PERROR_FILENO STDOUT_FILENO
#else
# define PERROR_STREAM stderr
# define PERROR_FILENO STDERR_FILENO
#endif
/****************************************************************************
@@ -56,8 +59,16 @@ void perror(FAR const char *s)
/* If strerror() is not enabled, then just print the error number */
#ifdef CONFIG_LIBC_STRERROR
# ifdef CONFIG_FILE_STREAM
fprintf(PERROR_STREAM, "%s: %s\n", s, strerror(get_errno()));
# else
dprintf(PERROR_FILENO, "%s: %s\n", s, strerror(get_errno()));
# endif
#else
# ifdef CONFIG_FILE_STREAM
fprintf(PERROR_STREAM, "%s: Error %d\n", s, get_errno());
# else
dprintf(PERROR_FILENO, "%s: Error %d\n", s, get_errno());
# endif
#endif
}
+6
View File
@@ -23,6 +23,7 @@
****************************************************************************/
#include <stdio.h>
#include <unistd.h>
/****************************************************************************
* Public Functions
@@ -30,5 +31,10 @@
int putchar(int c)
{
#ifdef CONFIG_FILE_STREAM
return fputc(c, stdout);
#else
unsigned char tmp = c;
return write(STDOUT_FILENO, &tmp, 1) == 1 ? c : EOF;
#endif
}
+15
View File
@@ -23,6 +23,9 @@
****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/uio.h>
#include "libc.h"
/****************************************************************************
@@ -39,6 +42,7 @@
int puts(FAR const IPTR char *s)
{
#ifdef CONFIG_FILE_STREAM
FILE *stream = stdout;
int nwritten;
int nput = EOF;
@@ -78,4 +82,15 @@ int puts(FAR const IPTR char *s)
lib_give_semaphore(stdout);
return nput;
#else
size_t len = strlen(s);
struct iovec iov[2];
iov[0].iov_base = (FAR void *)s;
iov[0].iov_len = len;
iov[1].iov_base = "\n";
iov[1].iov_len = 1;
return writev(STDOUT_FILENO, iov, 2) == ++len ? len : EOF;
#endif
}