stubs.c 6.5 KB

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