syscall_read.c 1.6 KB
Newer Older
B
Bernard Xiong 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
Bernard Xiong 已提交
5 6 7 8 9 10 11
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-01-28     Bernard      first version
 */

#include <rtthread.h>
12
#include <LowLevelIOInterface.h>
13
#include <unistd.h>
mysterywolf's avatar
mysterywolf 已提交
14
#ifdef RT_USING_POSIX_STDIO
B
bernard 已提交
15
#include "libc.h"
16
#endif
B
Bernard Xiong 已提交
17

18 19 20 21
#define DBG_TAG    "dlib.syscall_read"
#define DBG_LVL    DBG_INFO
#include <rtdbg.h>

mysterywolf's avatar
mysterywolf 已提交
22 23 24 25 26 27 28 29 30 31 32
/*
 * The "__read" function reads a number of bytes, at most "size" into
 * the memory area pointed to by "buffer".  It returns the number of
 * bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
 * occurs.
 *
 * The template implementation below assumes that the application
 * provides the function "MyLowLevelGetchar".  It should return a
 * character value, or -1 on failure.
 */

B
Bernard Xiong 已提交
33
#pragma module_name = "?__read"
mysterywolf's avatar
mysterywolf 已提交
34

B
Bernard Xiong 已提交
35 36
size_t __read(int handle, unsigned char *buf, size_t len)
{
37
#ifdef RT_USING_POSIX
B
Bernard Xiong 已提交
38 39 40 41
    int size;

    if (handle == _LLIO_STDIN)
    {
42
#ifdef RT_USING_POSIX_STDIO
43 44
        if (libc_stdio_get_console() < 0)
        {
45
            LOG_W("Do not invoke standard input before initializing libc");
46
            return 0; /* error, but keep going */
47
        }
48 49 50 51
        return read(STDIN_FILENO, buf, len); /* return the length of the data read */
#else
        return _LLIO_ERROR;
#endif /* RT_USING_POSIX_STDIO */
B
Bernard Xiong 已提交
52
    }
53 54
    else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
    {
B
Bernard Xiong 已提交
55
        return _LLIO_ERROR;
56
    }
B
Bernard Xiong 已提交
57

B
bernard 已提交
58
    size = read(handle, buf, len);
59
    return size; /* return the length of the data read */
60 61
#else
    return _LLIO_ERROR;
62
#endif /* RT_USING_POSIX */
B
Bernard Xiong 已提交
63
}