1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2017/10/15 bernard the first version
9 * 2023/08/07 Meco Man rename as posix/stdio.c
10 */
11
12 #include <rtthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <sys/time.h>
20 #include <sys/errno.h>
21 #include "posix/stdio.h"
22
23 #define STDIO_DEVICE_NAME_MAX 32
24
25 int sys_dup2(int oldfd, int new);
26
rt_posix_stdio_init(void)27 int rt_posix_stdio_init(void)
28 {
29 rt_device_t dev_console;
30
31 dev_console = rt_console_get_device();
32 if (dev_console)
33 {
34 int fd = rt_posix_stdio_set_console(dev_console->parent.name, O_RDWR);
35 if (fd < 0)
36 {
37 return -1;
38 }
39 /* set fd (0, 1, 2) */
40 sys_dup2(fd, 0);
41 sys_dup2(fd, 1);
42 sys_dup2(fd, 2);
43 }
44 return 0;
45 }
46 INIT_ENV_EXPORT(rt_posix_stdio_init);
47
48 #if defined(RT_USING_NEWLIBC)
49
50 #define NEWLIB_VERSION_NUM (__NEWLIB__ * 10000U + __NEWLIB_MINOR__ * 100U + __NEWLIB_PATCHLEVEL__)
51
52 static FILE* std_console = NULL;
rt_posix_stdio_set_console(const char * device_name,int mode)53 int rt_posix_stdio_set_console(const char* device_name, int mode)
54 {
55 FILE *fp;
56 char name[STDIO_DEVICE_NAME_MAX];
57 char *file_mode;
58
59 rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
60 name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
61
62 if (mode == O_RDWR)
63 {
64 file_mode = "r+";
65 }
66 else if (mode == O_WRONLY)
67 {
68 file_mode = "wb";
69 }
70 else
71 {
72 file_mode = "rb";
73 }
74
75 fp = fopen(name, file_mode);
76 if (fp)
77 {
78 setvbuf(fp, NULL, _IONBF, 0);
79
80 if (std_console)
81 {
82 fclose(std_console);
83 std_console = NULL;
84 }
85 std_console = fp;
86
87 if (mode == O_RDWR)
88 {
89 _GLOBAL_REENT->_stdin = std_console;
90 }
91 else
92 {
93 _GLOBAL_REENT->_stdin = NULL;
94 }
95
96 if (mode == O_RDONLY)
97 {
98 _GLOBAL_REENT->_stdout = NULL;
99 _GLOBAL_REENT->_stderr = NULL;
100 }
101 else
102 {
103 _GLOBAL_REENT->_stdout = std_console;
104 _GLOBAL_REENT->_stderr = std_console;
105 }
106
107 #if (NEWLIB_VERSION_NUM < 30400U) || (NEWLIB_VERSION_NUM >= 40000U && NEWLIB_VERSION_NUM < 40300U)
108 _GLOBAL_REENT->__sdidinit = 1; /* __sdidinit is obselete */
109 #endif
110 }
111
112 if (std_console)
113 return fileno(std_console);
114
115 return -1;
116 }
117
rt_posix_stdio_get_console(void)118 int rt_posix_stdio_get_console(void)
119 {
120 if (std_console)
121 return fileno(std_console);
122 else
123 return -1;
124 }
125
126 #elif defined(RT_USING_MUSLLIBC)
127
128 static FILE* std_console = NULL;
129
rt_posix_stdio_set_console(const char * device_name,int mode)130 int rt_posix_stdio_set_console(const char* device_name, int mode)
131 {
132 FILE *fp;
133 char name[STDIO_DEVICE_NAME_MAX];
134 char *file_mode;
135
136 rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
137 name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
138
139 if (mode == O_RDWR) file_mode = "r+";
140 else if (mode == O_WRONLY) file_mode = "wb";
141 else file_mode = "rb";
142
143 fp = fopen(name, file_mode);
144 if (fp)
145 {
146 setvbuf(fp, NULL, _IONBF, 0);
147
148 if (std_console)
149 {
150 fclose(std_console);
151 std_console = NULL;
152 }
153 std_console = fp;
154 }
155
156 if (std_console)
157 {
158 int fd = fileno(std_console);
159
160 return fd;
161 }
162
163 return -1;
164 }
165
rt_posix_stdio_get_console(void)166 int rt_posix_stdio_get_console(void)
167 {
168 int ret = -1;
169 if (std_console)
170 {
171 ret = fileno(std_console);
172 }
173
174 return ret;
175 }
176
177 #else
178
179 static int std_fd = -1;
rt_posix_stdio_set_console(const char * device_name,int mode)180 int rt_posix_stdio_set_console(const char* device_name, int mode)
181 {
182 int fd;
183 char name[STDIO_DEVICE_NAME_MAX];
184
185 rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
186 name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
187
188 fd = open(name, mode, 0);
189 if (fd >= 0)
190 {
191 if (std_fd >= 0)
192 {
193 close(std_fd);
194 }
195 std_fd = fd;
196 }
197
198 return std_fd;
199 }
200
rt_posix_stdio_get_console(void)201 int rt_posix_stdio_get_console(void) {
202 return std_fd;
203 }
204 #endif /* defined(RT_USING_NEWLIBC) */
205
getdelim(char ** lineptr,size_t * n,int delim,FILE * stream)206 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
207 {
208 char *cur_pos, *new_lineptr;
209 size_t new_lineptr_len;
210 int c;
211
212 if (lineptr == NULL || n == NULL || stream == NULL)
213 {
214 errno = EINVAL;
215 return -1;
216 }
217
218 if (*lineptr == NULL)
219 {
220 *n = 128; /* init len */
221 if ((*lineptr = (char *)malloc(*n)) == NULL)
222 {
223 errno = ENOMEM;
224 return -1;
225 }
226 }
227
228 cur_pos = *lineptr;
229 for (;;)
230 {
231 c = getc(stream);
232
233 if (ferror(stream) || (c == EOF && cur_pos == *lineptr))
234 return -1;
235
236 if (c == EOF)
237 break;
238
239 if ((*lineptr + *n - cur_pos) < 2)
240 {
241 if (LONG_MAX / 2 < *n)
242 {
243 #ifdef EOVERFLOW
244 errno = EOVERFLOW;
245 #else
246 errno = ERANGE; /* no EOVERFLOW defined */
247 #endif
248 return -1;
249 }
250 new_lineptr_len = *n * 2;
251
252 if ((new_lineptr = (char *)realloc(*lineptr, new_lineptr_len)) == NULL)
253 {
254 errno = ENOMEM;
255 return -1;
256 }
257 cur_pos = new_lineptr + (cur_pos - *lineptr);
258 *lineptr = new_lineptr;
259 *n = new_lineptr_len;
260 }
261
262 *cur_pos++ = (char)c;
263
264 if (c == delim)
265 break;
266 }
267
268 *cur_pos = '\0';
269 return (ssize_t)(cur_pos - *lineptr);
270 }
271
getline(char ** lineptr,size_t * n,FILE * stream)272 ssize_t getline(char **lineptr, size_t *n, FILE *stream)
273 {
274 return getdelim(lineptr, n, '\n', stream);
275 }
276