[libc][syscall]移除libc_stdio_read/write函数,优化syscall

This commit is contained in:
Meco Man 2021-09-21 12:51:26 -04:00
parent 13b0b60b14
commit c8c632512a
8 changed files with 30 additions and 60 deletions

View File

@ -10,17 +10,14 @@
#ifndef __RTT_LIBC_H__ #ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__ #define __RTT_LIBC_H__
#include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode); int libc_system_init(void);
int libc_stdio_get_console(void); int libc_stdio_get_console(void);
int libc_stdio_read(void* buffer, size_t size); int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_write(const void* buffer, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -21,6 +21,7 @@
#define STDIO_DEVICE_NAME_MAX 32 #define STDIO_DEVICE_NAME_MAX 32
static int std_fd = -1; static int std_fd = -1;
int libc_stdio_set_console(const char* device_name, int mode) int libc_stdio_set_console(const char* device_name, int mode)
{ {
int fd; int fd;
@ -47,29 +48,4 @@ int libc_stdio_get_console(void)
return std_fd; return std_fd;
} }
int libc_stdio_read(void *buffer, size_t size)
{
if (std_fd >= 0)
{
return read(std_fd, buffer, size);
}
else
{
rt_kprintf("Illegal stdio input!\n");
return 0;
}
}
int libc_stdio_write(const void *buffer, size_t size)
{
if (std_fd >= 0)
{
return write(std_fd, buffer, size);
}
else
{
rt_kprintf("Illegal stdio output!\n");
return size;
}
}
#endif #endif

View File

@ -146,16 +146,17 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
if (fh == STDIN) if (fh == STDIN)
{ {
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
size = libc_stdio_read(buf, len); size = read(STDIN_FILENO, buf, len);
return len - size; return len - size;
#else #else
/* no stdin */ /* no stdin */
return -1; return -1;
#endif #endif
} }
else if ((fh == STDOUT) || (fh == STDERR))
if ((fh == STDOUT) || (fh == STDERR)) {
return -1; return -1;
}
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
@ -185,7 +186,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
return 0; return 0;
#else #else
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
size = libc_stdio_write(buf, len); size = write(STDOUT_FILENO, buf, len);
return len - size; return len - size;
#else #else
if (rt_console_get_device()) if (rt_console_get_device())
@ -198,8 +199,10 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#endif #endif
#endif #endif
} }
else if (fh == STDIN)
if (fh == STDIN) return -1; {
return -1;
}
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;

View File

@ -11,17 +11,14 @@
#ifndef __RTT_LIBC_H__ #ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__ #define __RTT_LIBC_H__
#include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode); int libc_system_init(void);
int libc_stdio_get_console(void); int libc_stdio_get_console(void);
int libc_stdio_read(void* buffer, size_t size); int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_write(const void* buffer, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -46,13 +46,4 @@ int libc_stdio_get_console(void) {
return std_fd; return std_fd;
} }
int libc_stdio_read(void *buffer, size_t size)
{
return read(std_fd, buffer, size);
}
int libc_stdio_write(const void *buffer, size_t size)
{
return write(std_fd, buffer, size);
}
#endif #endif

View File

@ -25,14 +25,15 @@ size_t __read(int handle, unsigned char *buf, size_t len)
if (handle == _LLIO_STDIN) if (handle == _LLIO_STDIN)
{ {
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
return libc_stdio_read(buf, len); return read(STDIN_FILENO, buf, len);
#else #else
return _LLIO_ERROR; return _LLIO_ERROR;
#endif #endif
} }
else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) {
return _LLIO_ERROR; return _LLIO_ERROR;
}
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return _LLIO_ERROR; return _LLIO_ERROR;

View File

@ -30,19 +30,24 @@ size_t __write(int handle, const unsigned char *buf, size_t len)
#else #else
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
return libc_stdio_write((void*)buf, len); return write(STDOUT_FILENO, (void*)buf, len);
#else #else
rt_device_t console_device; rt_device_t console_device;
console_device = rt_console_get_device(); console_device = rt_console_get_device();
if (console_device != 0) rt_device_write(console_device, 0, buf, len); if (console_device != 0)
{
rt_device_write(console_device, 0, buf, len);
}
return len; return len;
#endif #endif
#endif #endif
} }
else if (handle == _LLIO_STDIN)
if (handle == _LLIO_STDIN) return _LLIO_ERROR; {
return _LLIO_ERROR;
}
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return _LLIO_ERROR; return _LLIO_ERROR;

View File

@ -18,8 +18,8 @@
extern "C" { extern "C" {
#endif #endif
int libc_system_init(void); int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_get_console(void); int libc_stdio_get_console(void);
int libc_stdio_set_console(const char* device_name, int mode);
#ifdef __cplusplus #ifdef __cplusplus
} }