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 #include <compiler_private.h>
15 #define DBG_TAG "dlib.syscall.write"
16 #define DBG_LVL DBG_INFO
17 #include <rtdbg.h>
18
19 /*
20 * The "__write" function should output "size" number of bytes from
21 * "buffer" in some application-specific way. It should return the
22 * number of characters written, or _LLIO_ERROR on failure.
23 *
24 * If "buffer" is zero then __write should perform flushing of
25 * internal buffers, if any. In this case "handle" can be -1 to
26 * indicate that all handles should be flushed.
27 *
28 * The template implementation below assumes that the application
29 * provides the function "MyLowLevelPutchar". It should return the
30 * character written, or -1 on failure.
31 */
32
33 #pragma module_name = "?__write"
34
__write(int handle,const unsigned char * buf,size_t len)35 size_t __write(int handle, const unsigned char *buf, size_t len)
36 {
37 #ifdef DFS_USING_POSIX
38 int size;
39 #endif /* DFS_USING_POSIX */
40
41 if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
42 {
43 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
44 rt_device_t console_device;
45
46 console_device = rt_console_get_device();
47 if (console_device)
48 {
49 rt_device_write(console_device, 0, buf, len);
50 }
51
52 return len; /* return the length of the data written */
53 #else
54 return _LLIO_ERROR;
55 #endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */
56 }
57 else if (handle == _LLIO_STDIN)
58 {
59 return _LLIO_ERROR;
60 }
61 else
62 {
63 #ifdef DFS_USING_POSIX
64 size = write(handle, buf, len);
65 return size; /* return the length of the data written */
66 #else
67 LOG_W(_WARNING_WITHOUT_FS);
68 return _LLIO_ERROR;
69 #endif /* DFS_USING_POSIX */
70 }
71 }
72