stubs.c 7.2 KB

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