1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2015-01-28     Bernard      first version
9  */
10 
11 #include <rtthread.h>
12 #include <LowLevelIOInterface.h>
13 #include <unistd.h>
14 #ifdef RT_USING_POSIX_STDIO
15 #include <posix/stdio.h>
16 #endif /* RT_USING_POSIX_STDIO */
17 #include <compiler_private.h>
18 #define DBG_TAG    "dlib.syscall.read"
19 #define DBG_LVL    DBG_INFO
20 #include <rtdbg.h>
21 
22 /*
23  * The "__read" function reads a number of bytes, at most "size" into
24  * the memory area pointed to by "buffer".  It returns the number of
25  * bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
26  * occurs.
27  *
28  * The template implementation below assumes that the application
29  * provides the function "MyLowLevelGetchar".  It should return a
30  * character value, or -1 on failure.
31  */
32 
33 #pragma module_name = "?__read"
34 
__read(int handle,unsigned char * buf,size_t len)35 size_t __read(int handle, unsigned char *buf, size_t len)
36 {
37 #ifdef DFS_USING_POSIX
38     int size;
39 
40     if (handle == _LLIO_STDIN)
41     {
42 #ifdef RT_USING_POSIX_STDIO
43         if (rt_posix_stdio_get_console() < 0)
44         {
45             LOG_W("Do not invoke standard input before initializing Compiler");
46             return 0; /* error, but keep going */
47         }
48         return read(STDIN_FILENO, buf, len); /* return the length of the data read */
49 #else
50         LOG_W(_WARNING_WITHOUT_STDIO);
51         return _LLIO_ERROR;
52 #endif /* RT_USING_POSIX_STDIO */
53     }
54     else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
55     {
56         return _LLIO_ERROR;
57     }
58 
59     size = read(handle, buf, len);
60     return size; /* return the length of the data read */
61 #else
62     LOG_W(_WARNING_WITHOUT_FS);
63     return _LLIO_ERROR;
64 #endif /* DFS_USING_POSIX */
65 }
66