Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions components/libc/posix/io/stdio/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <sys/errno.h>
#include "posix/stdio.h"

#define STDIO_DEVICE_NAME_MAX 32
#define STDIO_DEVICE_NAME_MAX 32

int sys_dup2(int oldfd, int new);

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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];
Expand All @@ -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)
Expand Down Expand Up @@ -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];
Expand All @@ -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) */
Expand Down