syscalls.c 7.0 KB

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