diff options
| author | guo <[email protected]> | 2021-10-28 10:30:20 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-28 10:30:20 +0800 |
| commit | e0f45a6ca415804dcdb9d1e968f617878f58f809 (patch) | |
| tree | f6a3fd9828abf9524c6629b52b029842e138ae8f /components/libc/compilers/gcc | |
| parent | ddaa8f3f32232f4460e09f5dab9f2f5c257fec92 (diff) | |
| parent | 4b9fb8f39caae9057a494611e8dae70d2809512d (diff) | |
Merge pull request #5239 from mysterywolf/POSIX
[libc] 消除代码中将RT_LIBC_USING_FILEIO的预编译判断条件
Diffstat (limited to 'components/libc/compilers/gcc')
| -rw-r--r-- | components/libc/compilers/gcc/newlib/libc.h | 2 | ||||
| -rw-r--r-- | components/libc/compilers/gcc/newlib/stdio.c | 43 | ||||
| -rw-r--r-- | components/libc/compilers/gcc/newlib/syscalls.c | 34 |
3 files changed, 25 insertions, 54 deletions
diff --git a/components/libc/compilers/gcc/newlib/libc.h b/components/libc/compilers/gcc/newlib/libc.h index 0ae1057730..2ea27b09f9 100644 --- a/components/libc/compilers/gcc/newlib/libc.h +++ b/components/libc/compilers/gcc/newlib/libc.h @@ -15,11 +15,13 @@ extern "C" { #endif int libc_system_init(void); + #ifdef RT_USING_POSIX int libc_stdio_get_console(void); int libc_stdio_set_console(const char* device_name, int mode); #endif /* RT_USING_POSIX */ + #ifdef __cplusplus } #endif diff --git a/components/libc/compilers/gcc/newlib/stdio.c b/components/libc/compilers/gcc/newlib/stdio.c index c0da1b1cec..e6ebc8c303 100644 --- a/components/libc/compilers/gcc/newlib/stdio.c +++ b/components/libc/compilers/gcc/newlib/stdio.c @@ -7,16 +7,14 @@ * Date Author Notes * 2017/10/15 bernard the first version */ -#include <rtthread.h> +#include <rtconfig.h> +#ifdef RT_USING_POSIX #include <stdio.h> +#include <stdlib.h> #include <fcntl.h> #include "libc.h" #define STDIO_DEVICE_NAME_MAX 32 - -#ifdef RT_LIBC_USING_FILEIO -#include <stdlib.h> - static FILE* std_console = NULL; int libc_stdio_set_console(const char* device_name, int mode) @@ -76,7 +74,8 @@ int libc_stdio_set_console(const char* device_name, int mode) _GLOBAL_REENT->__sdidinit = 1; } - if (std_console) return fileno(std_console); + if (std_console) + return fileno(std_console); return -1; } @@ -89,34 +88,4 @@ int libc_stdio_get_console(void) return -1; } -#elif defined(RT_USING_POSIX) -#include <unistd.h> -static int std_fd = -1; - -int libc_stdio_set_console(const char* device_name, int mode) -{ - int fd; - char name[STDIO_DEVICE_NAME_MAX]; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - fd = open(name, mode, 0); - if (fd >= 0) - { - if (std_fd >= 0) - { - close(std_fd); - } - std_fd = fd; - } - - return std_fd; -} - -int libc_stdio_get_console(void) -{ - return std_fd; -} - -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ diff --git a/components/libc/compilers/gcc/newlib/syscalls.c b/components/libc/compilers/gcc/newlib/syscalls.c index 6cb60e1776..9e9df3f625 100644 --- a/components/libc/compilers/gcc/newlib/syscalls.c +++ b/components/libc/compilers/gcc/newlib/syscalls.c @@ -14,6 +14,7 @@ #include <reent.h> #include <rtthread.h> #include <stdio.h> +#include <string.h> #include <stddef.h> #include <fcntl.h> #include <unistd.h> @@ -175,7 +176,7 @@ int flock(int fd, int operation) _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX _off_t rc; rc = lseek(fd, pos, whence); @@ -183,12 +184,12 @@ _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } int _mkdir_r(struct _reent *ptr, const char *name, int mode) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX int rc; rc = mkdir(name, mode); @@ -196,25 +197,24 @@ int _mkdir_r(struct _reent *ptr, const char *name, int mode) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } int _open_r(struct _reent *ptr, const char *file, int flags, int mode) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX int rc; - rc = open(file, flags, mode); return rc; #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO) { @@ -226,12 +226,12 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } int _rename_r(struct _reent *ptr, const char *old, const char *new) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX int rc; rc = rename(old, new); @@ -239,12 +239,12 @@ int _rename_r(struct _reent *ptr, const char *old, const char *new) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX int rc; rc = stat(file, pstat); @@ -252,22 +252,22 @@ int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } int _unlink_r(struct _reent *ptr, const char *file) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX return unlink(file); #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ } _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { -#ifdef RT_LIBC_USING_FILEIO +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO) { @@ -285,7 +285,7 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) if (console) return rt_device_write(console, -1, buf, nbytes); } -#endif /* RT_LIBC_USING_FILEIO */ +#endif /* RT_USING_POSIX */ ptr->_errno = ENOTSUP; return -1; } |
