This is an automated email from the ASF dual-hosted git repository.

davids5 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new bd2327c  libc: Make perror/putchar/getchar/puts work without 
CONFIG_FILE_STREAM
bd2327c is described below

commit bd2327cc2d58692b025bb48573a285d60915ccae
Author: Xiang Xiao <xiaoxi...@xiaomi.com>
AuthorDate: Mon Nov 15 03:25:15 2021 +0800

    libc: Make perror/putchar/getchar/puts work without CONFIG_FILE_STREAM
    
    Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com>
---
 libs/libc/stdio/Make.defs     |  9 +++++----
 libs/libc/stdio/lib_getchar.c |  6 ++++++
 libs/libc/stdio/lib_perror.c  | 11 +++++++++++
 libs/libc/stdio/lib_putchar.c |  6 ++++++
 libs/libc/stdio/lib_puts.c    | 15 +++++++++++++++
 5 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs
index a1ba9ec..5456672 100644
--- a/libs/libc/stdio/Make.defs
+++ b/libs/libc/stdio/Make.defs
@@ -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
diff --git a/libs/libc/stdio/lib_getchar.c b/libs/libc/stdio/lib_getchar.c
index 91373f1..a30f0e0 100644
--- a/libs/libc/stdio/lib_getchar.c
+++ b/libs/libc/stdio/lib_getchar.c
@@ -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
 }
diff --git a/libs/libc/stdio/lib_perror.c b/libs/libc/stdio/lib_perror.c
index b177a8f..6e4b9ce 100644
--- a/libs/libc/stdio/lib_perror.c
+++ b/libs/libc/stdio/lib_perror.c
@@ -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
 }
diff --git a/libs/libc/stdio/lib_putchar.c b/libs/libc/stdio/lib_putchar.c
index 202ab63..6f3f0ad 100644
--- a/libs/libc/stdio/lib_putchar.c
+++ b/libs/libc/stdio/lib_putchar.c
@@ -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
 }
diff --git a/libs/libc/stdio/lib_puts.c b/libs/libc/stdio/lib_puts.c
index 0b4e1ee..859149b 100644
--- a/libs/libc/stdio/lib_puts.c
+++ b/libs/libc/stdio/lib_puts.c
@@ -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
 }

Reply via email to