stubs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-23 Yihui The first version
  9. * 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
  10. * 2014-08-03 bernard If using msh, use system() implementation
  11. * in msh.
  12. * 2020-08-05 Meco Man fixed _sys_flen() compiling-warning when
  13. * RT_USING_DFS is not defined
  14. * 2020-02-13 Meco Man re-implement exit() and abort()
  15. */
  16. #include <string.h>
  17. #include <rt_sys.h>
  18. #include "rtthread.h"
  19. #include "libc.h"
  20. #ifdef RT_USING_DFS
  21. #include "dfs_posix.h"
  22. #endif
  23. #ifdef __CLANG_ARM
  24. __asm(".global __use_no_semihosting\n\t");
  25. #else
  26. #pragma import(__use_no_semihosting_swi)
  27. #endif
  28. /* Standard IO device handles. */
  29. #define STDIN 0
  30. #define STDOUT 1
  31. #define STDERR 2
  32. /* Standard IO device name defines. */
  33. const char __stdin_name[] = "STDIN";
  34. const char __stdout_name[] = "STDOUT";
  35. const char __stderr_name[] = "STDERR";
  36. /**
  37. * required by fopen() and freopen().
  38. *
  39. * @param name - file name with path.
  40. * @param openmode - a bitmap hose bits mostly correspond directly to
  41. * the ISO mode specification.
  42. * @return -1 if an error occurs.
  43. */
  44. FILEHANDLE _sys_open(const char *name, int openmode)
  45. {
  46. #ifdef RT_USING_DFS
  47. int fd;
  48. int mode = O_RDONLY;
  49. #endif
  50. /* Register standard Input Output devices. */
  51. if (strcmp(name, __stdin_name) == 0)
  52. return (STDIN);
  53. if (strcmp(name, __stdout_name) == 0)
  54. return (STDOUT);
  55. if (strcmp(name, __stderr_name) == 0)
  56. return (STDERR);
  57. #ifndef RT_USING_DFS
  58. return -1;
  59. #else
  60. /* Correct openmode from fopen to open */
  61. if (openmode & OPEN_PLUS)
  62. {
  63. if (openmode & OPEN_W)
  64. {
  65. mode |= (O_RDWR | O_TRUNC | O_CREAT);
  66. }
  67. else if (openmode & OPEN_A)
  68. {
  69. mode |= (O_RDWR | O_APPEND | O_CREAT);
  70. }
  71. else
  72. mode |= O_RDWR;
  73. }
  74. else
  75. {
  76. if (openmode & OPEN_W)
  77. {
  78. mode |= (O_WRONLY | O_TRUNC | O_CREAT);
  79. }
  80. else if (openmode & OPEN_A)
  81. {
  82. mode |= (O_WRONLY | O_APPEND | O_CREAT);
  83. }
  84. }
  85. fd = open(name, mode, 0);
  86. if (fd < 0)
  87. return -1;
  88. else
  89. return fd;
  90. #endif
  91. }
  92. int _sys_close(FILEHANDLE fh)
  93. {
  94. #ifndef RT_USING_DFS
  95. return 0;
  96. #else
  97. if (fh <= STDERR) return 0;
  98. return close(fh);
  99. #endif
  100. }
  101. /*
  102. * Read from a file. Can return:
  103. * - zero if the read was completely successful
  104. * - the number of bytes _not_ read, if the read was partially successful
  105. * - the number of bytes not read, plus the top bit set (0x80000000), if
  106. * the read was partially successful due to end of file
  107. * - -1 if some error other than EOF occurred
  108. *
  109. * It is also legal to signal EOF by returning no data but
  110. * signalling no error (i.e. the top-bit-set mechanism need never
  111. * be used).
  112. *
  113. * So if (for example) the user is trying to read 8 bytes at a time
  114. * from a file in which only 5 remain, this routine can do three
  115. * equally valid things:
  116. *
  117. * - it can return 0x80000003 (3 bytes not read due to EOF)
  118. * - OR it can return 3 (3 bytes not read), and then return
  119. * 0x80000008 (8 bytes not read due to EOF) on the next attempt
  120. * - OR it can return 3 (3 bytes not read), and then return
  121. * 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
  122. * attempt
  123. *
  124. * `mode' exists for historical reasons and must be ignored.
  125. */
  126. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  127. {
  128. #ifdef RT_USING_DFS
  129. int size;
  130. #endif
  131. if (fh == STDIN)
  132. {
  133. #ifdef RT_USING_POSIX
  134. size = libc_stdio_read(buf, len);
  135. return len - size;
  136. #else
  137. /* no stdin */
  138. return -1;
  139. #endif
  140. }
  141. if ((fh == STDOUT) || (fh == STDERR))
  142. return -1;
  143. #ifndef RT_USING_DFS
  144. return 0;
  145. #else
  146. size = read(fh, buf, len);
  147. if (size >= 0)
  148. return len - size;
  149. else
  150. return -1;
  151. #endif
  152. }
  153. /*
  154. * Write to a file. Returns 0 on success, negative on error, and
  155. * the number of characters _not_ written on partial success.
  156. * `mode' exists for historical reasons and must be ignored.
  157. */
  158. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  159. {
  160. #ifdef RT_USING_DFS
  161. int size;
  162. #endif
  163. if ((fh == STDOUT) || (fh == STDERR))
  164. {
  165. #if !defined(RT_USING_CONSOLE) || !defined(RT_USING_DEVICE)
  166. return 0;
  167. #else
  168. #ifdef RT_USING_POSIX
  169. size = libc_stdio_write(buf, len);
  170. return len - size;
  171. #else
  172. if (rt_console_get_device())
  173. {
  174. rt_device_write(rt_console_get_device(), -1, buf, len);
  175. return 0;
  176. }
  177. return -1;
  178. #endif
  179. #endif
  180. }
  181. if (fh == STDIN) return -1;
  182. #ifndef RT_USING_DFS
  183. return 0;
  184. #else
  185. size = write(fh, buf, len);
  186. if (size >= 0)
  187. return len - size;
  188. else
  189. return -1;
  190. #endif
  191. }
  192. /*
  193. * Move the file position to a given offset from the file start.
  194. * Returns >=0 on success, <0 on failure.
  195. */
  196. int _sys_seek(FILEHANDLE fh, long pos)
  197. {
  198. if (fh < STDERR)
  199. return -1;
  200. #ifndef RT_USING_DFS
  201. return -1;
  202. #else
  203. /* position is relative to the start of file fh */
  204. return lseek(fh, pos, 0);
  205. #endif
  206. }
  207. /**
  208. * used by tmpnam() or tmpfile()
  209. */
  210. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  211. {
  212. return -1;
  213. }
  214. char *_sys_command_string(char *cmd, int len)
  215. {
  216. /* no support */
  217. return RT_NULL;
  218. }
  219. /* This function writes a character to the console. */
  220. void _ttywrch(int ch)
  221. {
  222. #ifdef RT_USING_CONSOLE
  223. char c;
  224. c = (char)ch;
  225. rt_kprintf(&c);
  226. #endif
  227. }
  228. RT_WEAK void _sys_exit(int return_code)
  229. {
  230. rt_thread_t self = rt_thread_self();
  231. #ifdef RT_USING_MODULE
  232. if (dlmodule_self())
  233. {
  234. dlmodule_exit(return_code);
  235. }
  236. #endif
  237. if (self != RT_NULL)
  238. {
  239. rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, return_code);
  240. rt_thread_suspend(self);
  241. rt_schedule();
  242. }
  243. while(1); /* noreturn */
  244. }
  245. /**
  246. * return current length of file.
  247. *
  248. * @param fh - file handle
  249. * @return file length, or -1 on failed
  250. */
  251. long _sys_flen(FILEHANDLE fh)
  252. {
  253. #ifdef RT_USING_DFS
  254. struct stat stat;
  255. if (fh < STDERR)
  256. return -1;
  257. fstat(fh, &stat);
  258. return stat.st_size;
  259. #else
  260. return -1;
  261. #endif
  262. }
  263. int _sys_istty(FILEHANDLE fh)
  264. {
  265. if((STDIN <= fh) && (fh <= STDERR))
  266. return 1;
  267. else
  268. return 0;
  269. }
  270. int remove(const char *filename)
  271. {
  272. #ifndef RT_USING_DFS
  273. return -1;
  274. #else
  275. return unlink(filename);
  276. #endif
  277. }
  278. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS)
  279. /* use system(const char *string) implementation in the msh */
  280. #else
  281. int system(const char *string)
  282. {
  283. RT_ASSERT(0);
  284. for (;;);
  285. }
  286. #endif
  287. #ifdef __MICROLIB
  288. #include <stdio.h>
  289. int fputc(int c, FILE *f)
  290. {
  291. char ch[2] = {0};
  292. ch[0] = c;
  293. rt_kprintf(&ch[0]);
  294. return 1;
  295. }
  296. int fgetc(FILE *f)
  297. {
  298. #ifdef RT_USING_POSIX
  299. char ch;
  300. if (libc_stdio_read(&ch, 1) == 1)
  301. return ch;
  302. #endif
  303. return -1;
  304. }
  305. #endif