diff --git a/components/libc/posix/io/stdio/stdio.c b/components/libc/posix/io/stdio/stdio.c index 98816c1f2812..6bbd93de974c 100644 --- a/components/libc/posix/io/stdio/stdio.c +++ b/components/libc/posix/io/stdio/stdio.c @@ -20,7 +20,7 @@ #include #include "posix/stdio.h" -#define STDIO_DEVICE_NAME_MAX 32 +#define STDIO_DEVICE_NAME_MAX 32 int sys_dup2(int oldfd, int new); @@ -49,8 +49,8 @@ INIT_ENV_EXPORT(rt_posix_stdio_init); #define NEWLIB_VERSION_NUM (__NEWLIB__ * 10000U + __NEWLIB_MINOR__ * 100U + __NEWLIB_PATCHLEVEL__) -static FILE* std_console = NULL; -int rt_posix_stdio_set_console(const char* device_name, int mode) +static FILE *std_console = NULL; +int rt_posix_stdio_set_console(const char *device_name, int mode) { FILE *fp; char name[STDIO_DEVICE_NAME_MAX]; @@ -86,11 +86,11 @@ int rt_posix_stdio_set_console(const char* device_name, int mode) if (mode == O_RDWR) { - _GLOBAL_REENT->_stdin = std_console; + _GLOBAL_REENT->_stdin = std_console; } else { - _GLOBAL_REENT->_stdin = NULL; + _GLOBAL_REENT->_stdin = NULL; } if (mode == O_RDONLY) @@ -104,8 +104,12 @@ int rt_posix_stdio_set_console(const char* device_name, int mode) _GLOBAL_REENT->_stderr = std_console; } -#if (NEWLIB_VERSION_NUM < 30400U) || (NEWLIB_VERSION_NUM >= 40000U && NEWLIB_VERSION_NUM < 40300U) - _GLOBAL_REENT->__sdidinit = 1; /* __sdidinit is obselete */ +#if defined(_REENT_SMALL) || !defined(__sdidinit) + // Newlib >= 3.4.0 or small reent version: no __sdidinit field + // Do nothing +#else + // Older newlib versions (typically < 3.4.0) still define __sdidinit + _GLOBAL_REENT->__sdidinit = 1; #endif } @@ -125,9 +129,9 @@ int rt_posix_stdio_get_console(void) #elif defined(RT_USING_MUSLLIBC) -static FILE* std_console = NULL; +static FILE *std_console = NULL; -int rt_posix_stdio_set_console(const char* device_name, int mode) +int rt_posix_stdio_set_console(const char *device_name, int mode) { FILE *fp; char name[STDIO_DEVICE_NAME_MAX]; @@ -136,9 +140,12 @@ int rt_posix_stdio_set_console(const char* device_name, int mode) rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - if (mode == O_RDWR) file_mode = "r+"; - else if (mode == O_WRONLY) file_mode = "wb"; - else file_mode = "rb"; + if (mode == O_RDWR) + file_mode = "r+"; + else if (mode == O_WRONLY) + file_mode = "wb"; + else + file_mode = "rb"; fp = fopen(name, file_mode); if (fp) @@ -177,7 +184,7 @@ int rt_posix_stdio_get_console(void) #else static int std_fd = -1; -int rt_posix_stdio_set_console(const char* device_name, int mode) +int rt_posix_stdio_set_console(const char *device_name, int mode) { int fd; char name[STDIO_DEVICE_NAME_MAX]; @@ -198,7 +205,8 @@ int rt_posix_stdio_set_console(const char* device_name, int mode) return std_fd; } -int rt_posix_stdio_get_console(void) { +int rt_posix_stdio_get_console(void) +{ return std_fd; } #endif /* defined(RT_USING_NEWLIBC) */