[libc] 消除代码中依赖于RT_LIBC_USING_FILEIO的判断

This commit is contained in:
Meco Man
2021-10-27 14:42:25 -04:00
parent bb859e79e0
commit 4b9fb8f39c
10 changed files with 61 additions and 90 deletions

View File

@@ -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

View File

@@ -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 */

View File

@@ -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;
}