stubs.c 6.8 KB

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