Skip to content

Commit b7903b2

Browse files
authored
Merge pull request #5087 from mysterywolf/libc_stdio
[libc标准化][syscall]移除libc_stdio_read/write函数,优化syscall
2 parents 94bd9f9 + 0a79965 commit b7903b2

File tree

9 files changed

+79
-63
lines changed

9 files changed

+79
-63
lines changed

components/libc/compilers/armlibc/libc.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
#ifndef __RTT_LIBC_H__
1111
#define __RTT_LIBC_H__
1212

13-
#include <stddef.h>
14-
1513
#ifdef __cplusplus
1614
extern "C" {
1715
#endif
18-
int libc_system_init(void);
1916

20-
int libc_stdio_set_console(const char* device_name, int mode);
17+
int libc_system_init(void);
2118
int libc_stdio_get_console(void);
22-
int libc_stdio_read(void* buffer, size_t size);
23-
int libc_stdio_write(const void* buffer, size_t size);
19+
int libc_stdio_set_console(const char* device_name, int mode);
20+
2421
#ifdef __cplusplus
2522
}
2623
#endif

components/libc/compilers/armlibc/stdio.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define STDIO_DEVICE_NAME_MAX 32
2222

2323
static int std_fd = -1;
24+
2425
int libc_stdio_set_console(const char* device_name, int mode)
2526
{
2627
int fd;
@@ -47,29 +48,4 @@ int libc_stdio_get_console(void)
4748
return std_fd;
4849
}
4950

50-
int libc_stdio_read(void *buffer, size_t size)
51-
{
52-
if (std_fd >= 0)
53-
{
54-
return read(std_fd, buffer, size);
55-
}
56-
else
57-
{
58-
rt_kprintf("Illegal stdio input!\n");
59-
return 0;
60-
}
61-
}
62-
63-
int libc_stdio_write(const void *buffer, size_t size)
64-
{
65-
if (std_fd >= 0)
66-
{
67-
return write(std_fd, buffer, size);
68-
}
69-
else
70-
{
71-
rt_kprintf("Illegal stdio output!\n");
72-
return size;
73-
}
74-
}
7551
#endif

components/libc/compilers/armlibc/syscalls.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <dfs_posix.h>
2626
#endif
2727

28+
#define DBG_TAG "armlibc.syscalls"
29+
#define DBG_LVL DBG_INFO
30+
#include <rtdbg.h>
31+
2832
#ifdef __CLANG_ARM
2933
__asm(".global __use_no_semihosting\n\t");
3034
#else
@@ -146,16 +150,22 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
146150
if (fh == STDIN)
147151
{
148152
#ifdef RT_USING_POSIX
149-
size = libc_stdio_read(buf, len);
153+
if (libc_stdio_get_console() < 0)
154+
{
155+
LOG_W("Do not invoke standard output before initializing libc");
156+
return 0;
157+
}
158+
size = read(STDIN_FILENO, buf, len);
150159
return len - size;
151160
#else
152161
/* no stdin */
153162
return -1;
154163
#endif
155164
}
156-
157-
if ((fh == STDOUT) || (fh == STDERR))
165+
else if ((fh == STDOUT) || (fh == STDERR))
166+
{
158167
return -1;
168+
}
159169

160170
#ifndef RT_USING_DFS
161171
return 0;
@@ -185,7 +195,12 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
185195
return 0;
186196
#else
187197
#ifdef RT_USING_POSIX
188-
size = libc_stdio_write(buf, len);
198+
if (libc_stdio_get_console() < 0)
199+
{
200+
LOG_W("Do not invoke standard input before initializing libc");
201+
return 0;
202+
}
203+
size = write(STDOUT_FILENO, buf, len);
189204
return len - size;
190205
#else
191206
if (rt_console_get_device())
@@ -198,8 +213,10 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
198213
#endif
199214
#endif
200215
}
201-
202-
if (fh == STDIN) return -1;
216+
else if (fh == STDIN)
217+
{
218+
return -1;
219+
}
203220

204221
#ifndef RT_USING_DFS
205222
return 0;

components/libc/compilers/dlib/libc.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
#ifndef __RTT_LIBC_H__
1212
#define __RTT_LIBC_H__
1313

14-
#include <stddef.h>
15-
1614
#ifdef __cplusplus
1715
extern "C" {
1816
#endif
19-
int libc_system_init(void);
2017

21-
int libc_stdio_set_console(const char* device_name, int mode);
18+
int libc_system_init(void);
2219
int libc_stdio_get_console(void);
23-
int libc_stdio_read(void* buffer, size_t size);
24-
int libc_stdio_write(const void* buffer, size_t size);
20+
int libc_stdio_set_console(const char* device_name, int mode);
21+
2522
#ifdef __cplusplus
2623
}
2724
#endif

components/libc/compilers/dlib/stdio.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,4 @@ int libc_stdio_get_console(void) {
4646
return std_fd;
4747
}
4848

49-
int libc_stdio_read(void *buffer, size_t size)
50-
{
51-
return read(std_fd, buffer, size);
52-
}
53-
54-
int libc_stdio_write(const void *buffer, size_t size)
55-
{
56-
return write(std_fd, buffer, size);
57-
}
5849
#endif

components/libc/compilers/dlib/syscall_read.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <yfuns.h>
1616
#include "libc.h"
1717

18+
#define DBG_TAG "dlib.syscall_read"
19+
#define DBG_LVL DBG_INFO
20+
#include <rtdbg.h>
21+
1822
#pragma module_name = "?__read"
1923
size_t __read(int handle, unsigned char *buf, size_t len)
2024
{
@@ -25,14 +29,20 @@ size_t __read(int handle, unsigned char *buf, size_t len)
2529
if (handle == _LLIO_STDIN)
2630
{
2731
#ifdef RT_USING_POSIX
28-
return libc_stdio_read(buf, len);
32+
if (libc_stdio_get_console() < 0)
33+
{
34+
LOG_W("Do not invoke standard input before initializing libc");
35+
return 0;
36+
}
37+
return read(STDIN_FILENO, buf, len);
2938
#else
3039
return _LLIO_ERROR;
3140
#endif
3241
}
33-
34-
if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
42+
else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
43+
{
3544
return _LLIO_ERROR;
45+
}
3646

3747
#ifndef RT_USING_DFS
3848
return _LLIO_ERROR;

components/libc/compilers/dlib/syscall_write.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <yfuns.h>
1616
#include "libc.h"
1717

18+
#define DBG_TAG "dlib.syscall_write"
19+
#define DBG_LVL DBG_INFO
20+
#include <rtdbg.h>
21+
1822
#pragma module_name = "?__write"
1923

2024
size_t __write(int handle, const unsigned char *buf, size_t len)
@@ -30,19 +34,29 @@ size_t __write(int handle, const unsigned char *buf, size_t len)
3034
#else
3135

3236
#ifdef RT_USING_POSIX
33-
return libc_stdio_write((void*)buf, len);
37+
if (libc_stdio_get_console() < 0)
38+
{
39+
LOG_W("Do not invoke standard output before initializing libc");
40+
return 0;
41+
}
42+
return write(STDOUT_FILENO, (void*)buf, len);
3443
#else
3544
rt_device_t console_device;
3645

3746
console_device = rt_console_get_device();
38-
if (console_device != 0) rt_device_write(console_device, 0, buf, len);
47+
if (console_device != 0)
48+
{
49+
rt_device_write(console_device, 0, buf, len);
50+
}
3951

4052
return len;
4153
#endif
4254
#endif
4355
}
44-
45-
if (handle == _LLIO_STDIN) return _LLIO_ERROR;
56+
else if (handle == _LLIO_STDIN)
57+
{
58+
return _LLIO_ERROR;
59+
}
4660

4761
#ifndef RT_USING_DFS
4862
return _LLIO_ERROR;

components/libc/compilers/newlib/libc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
extern "C" {
1919
#endif
2020
int libc_system_init(void);
21-
int libc_stdio_set_console(const char* device_name, int mode);
2221
int libc_stdio_get_console(void);
22+
int libc_stdio_set_console(const char* device_name, int mode);
2323

2424
#ifdef __cplusplus
2525
}

components/libc/compilers/newlib/syscalls.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
* 2020-02-24 Meco Man fix bug of _isatty_r()
1212
*/
1313

14+
#include <reent.h>
1415
#include <rtthread.h>
1516
#include <stddef.h>
17+
#include <unistd.h>
1618
#include <sys/errno.h>
1719

1820
#define DBG_TAG "newlib.syscalls"
@@ -90,6 +92,10 @@ void __libc_init_array(void)
9092
#include <dlmodule.h>
9193
#endif
9294

95+
#define DBG_TAG "newlib.syscalls"
96+
#define DBG_LVL DBG_INFO
97+
#include <rtdbg.h>
98+
9399
/* Reentrant versions of system calls. */
94100

95101
#ifndef _REENT_ONLY
@@ -219,7 +225,11 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
219225
return -1;
220226
#else
221227
_ssize_t rc;
222-
228+
if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
229+
{
230+
LOG_W("Do not invoke standard input before initializing libc");
231+
return 0;
232+
}
223233
rc = read(fd, buf, nbytes);
224234
return rc;
225235
#endif
@@ -275,7 +285,7 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
275285
{
276286
#ifndef RT_USING_DFS
277287
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
278-
if (fileno(stdout) == fd)
288+
if (STDOUT_FILENO == fd)
279289
{
280290
rt_device_t console;
281291

@@ -291,7 +301,11 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
291301
#endif /*RT_USING_DEVICE*/
292302
#else
293303
_ssize_t rc;
294-
304+
if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
305+
{
306+
LOG_W("Do not invoke standard output before initializing libc");
307+
return 0;
308+
}
295309
rc = write(fd, buf, nbytes);
296310
return rc;
297311
#endif

0 commit comments

Comments
 (0)