stubs.c 7.2 KB

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