stubs.c 7.1 KB

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