1
0

syscalls.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2006-2022, 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 <rt_sys.h>
  18. #include <rtthread.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <compiler_private.h>
  24. #ifdef RT_USING_POSIX_STDIO
  25. #include "libc.h"
  26. #endif /* RT_USING_POSIX_STDIO */
  27. #define DBG_TAG "armlibc.syscalls"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. #ifdef __clang__
  31. __asm(".global __use_no_semihosting\n\t");
  32. #else
  33. #pragma import(__use_no_semihosting_swi)
  34. #endif
  35. /* Standard IO device handles. */
  36. #define STDIN 0
  37. #define STDOUT 1
  38. #define STDERR 2
  39. /* Standard IO device name defines. */
  40. const char __stdin_name[] = "STDIN";
  41. const char __stdout_name[] = "STDOUT";
  42. const char __stderr_name[] = "STDERR";
  43. /**
  44. * required by fopen() and freopen().
  45. *
  46. * @param name - file name with path.
  47. * @param openmode - a bitmap hose bits mostly correspond directly to
  48. * the ISO mode specification.
  49. * @return -1 if an error occurs.
  50. */
  51. FILEHANDLE _sys_open(const char *name, int openmode)
  52. {
  53. #ifdef DFS_USING_POSIX
  54. int fd;
  55. int mode = O_RDONLY;
  56. #endif /* DFS_USING_POSIX */
  57. /* Register standard Input Output devices. */
  58. if (strcmp(name, __stdin_name) == 0)
  59. return (STDIN);
  60. if (strcmp(name, __stdout_name) == 0)
  61. return (STDOUT);
  62. if (strcmp(name, __stderr_name) == 0)
  63. return (STDERR);
  64. #ifndef DFS_USING_POSIX
  65. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  66. return -1; /* error */
  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; /* error */
  96. else
  97. return fd;
  98. #endif /* DFS_USING_POSIX */
  99. }
  100. int _sys_close(FILEHANDLE fh)
  101. {
  102. #ifdef DFS_USING_POSIX
  103. if (fh <= STDERR)
  104. return 0; /* error */
  105. return close(fh);
  106. #else
  107. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  108. return 0; /* error */
  109. #endif /* DFS_USING_POSIX */
  110. }
  111. /*
  112. * Read from a file. Can return:
  113. * - zero if the read was completely successful
  114. * - the number of bytes _not_ read, if the read was partially successful
  115. * - the number of bytes not read, plus the top bit set (0x80000000), if
  116. * the read was partially successful due to end of file
  117. * - -1 if some error other than EOF occurred
  118. *
  119. * It is also legal to signal EOF by returning no data but
  120. * signalling no error (i.e. the top-bit-set mechanism need never
  121. * be used).
  122. *
  123. * So if (for example) the user is trying to read 8 bytes at a time
  124. * from a file in which only 5 remain, this routine can do three
  125. * equally valid things:
  126. *
  127. * - it can return 0x80000003 (3 bytes not read due to EOF)
  128. * - OR it can return 3 (3 bytes not read), and then return
  129. * 0x80000008 (8 bytes not read due to EOF) on the next attempt
  130. * - OR it can return 3 (3 bytes not read), and then return
  131. * 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
  132. * attempt
  133. *
  134. * `mode' exists for historical reasons and must be ignored.
  135. */
  136. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  137. {
  138. #ifdef DFS_USING_POSIX
  139. int size;
  140. if (fh == STDIN)
  141. {
  142. #ifdef RT_USING_POSIX_STDIO
  143. if (libc_stdio_get_console() < 0)
  144. {
  145. LOG_W("Do not invoke standard output before initializing Compiler");
  146. return 0; /* error, but keep going */
  147. }
  148. size = read(STDIN_FILENO, buf, len);
  149. return len - size; /* success */
  150. #else
  151. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
  152. return 0; /* error */
  153. #endif /* RT_USING_POSIX_STDIO */
  154. }
  155. else if (fh == STDOUT || fh == STDERR)
  156. {
  157. return -1; /* 100% error */
  158. }
  159. else
  160. {
  161. size = read(fh, buf, len);
  162. if (size >= 0)
  163. {
  164. return len - size; /* success */
  165. }
  166. else
  167. {
  168. return 0; /* error */
  169. }
  170. }
  171. #else
  172. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  173. return 0; /* error */
  174. #endif /* DFS_USING_POSIX */
  175. }
  176. /*
  177. * Write to a file. Returns 0 on success, negative on error, and
  178. * the number of characters _not_ written on partial success.
  179. * `mode' exists for historical reasons and must be ignored.
  180. * The return value is either:
  181. * A positive number representing the number of characters not written
  182. * (so any nonzero return value denotes a failure of some sort).
  183. * A negative number indicating an error.
  184. */
  185. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  186. {
  187. #ifdef DFS_USING_POSIX
  188. int size;
  189. #endif /* DFS_USING_POSIX */
  190. if (fh == STDOUT || fh == STDERR)
  191. {
  192. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  193. rt_device_t console;
  194. console = rt_console_get_device();
  195. if (console)
  196. {
  197. rt_device_write(console, -1, buf, len);
  198. }
  199. return 0; /* success */
  200. #else
  201. return 0; /* error */
  202. #endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */
  203. }
  204. else if (fh == STDIN)
  205. {
  206. return -1; /* 100% error */
  207. }
  208. else
  209. {
  210. #ifdef DFS_USING_POSIX
  211. size = write(fh, buf, len);
  212. if (size >= 0)
  213. {
  214. /*
  215. fflush doesn't have a good solution in Keil-MDK,
  216. so it has to sync/flush when for each writen.
  217. */
  218. fsync(fh);
  219. return len - size; /* success */
  220. }
  221. else
  222. {
  223. return 0; /* error */
  224. }
  225. #else
  226. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  227. return 0; /* error */
  228. #endif /* DFS_USING_POSIX */
  229. }
  230. }
  231. /*
  232. * Flush any OS buffers associated with fh, ensuring that the file
  233. * is up to date on disk. Result is >=0 if OK, negative for an
  234. * error.
  235. * This function is deprecated. It is never called by any other library function,
  236. * and you are not required to re-implement it if you are retargeting standard I/O (stdio).
  237. */
  238. int _sys_ensure(FILEHANDLE fh)
  239. {
  240. #ifdef DFS_USING_POSIX
  241. return fsync(fh);
  242. #else
  243. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  244. return 0; /* error */
  245. #endif /* DFS_USING_POSIX */
  246. }
  247. /*
  248. * Move the file position to a given offset from the file start.
  249. * Returns >=0 on success, <0 on failure.
  250. */
  251. int _sys_seek(FILEHANDLE fh, long pos)
  252. {
  253. #ifdef DFS_USING_POSIX
  254. if (fh < STDERR)
  255. return 0; /* error */
  256. /* position is relative to the start of file fh */
  257. return lseek(fh, pos, 0);
  258. #else
  259. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  260. return 0; /* error */
  261. #endif /* DFS_USING_POSIX */
  262. }
  263. /**
  264. * used by tmpnam() or tmpfile()
  265. */
  266. #if __ARMCC_VERSION >= 6190000
  267. void _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  268. {
  269. rt_snprintf(name, maxlength, "tem%03d", fileno);
  270. }
  271. #else
  272. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  273. {
  274. rt_snprintf(name, maxlength, "tem%03d", fileno);
  275. return 1;
  276. }
  277. #endif /* __ARMCC_VERSION >= 6190000 */
  278. char *_sys_command_string(char *cmd, int len)
  279. {
  280. /* no support */
  281. return RT_NULL;
  282. }
  283. /* This function writes a character to the console. */
  284. void _ttywrch(int ch)
  285. {
  286. #ifdef RT_USING_CONSOLE
  287. rt_kprintf("%c", (char)ch);
  288. #endif /* RT_USING_CONSOLE */
  289. }
  290. /* for exit() and abort() */
  291. rt_weak void _sys_exit(int return_code)
  292. {
  293. extern void __rt_libc_exit(int status);
  294. __rt_libc_exit(return_code);
  295. while (1);
  296. }
  297. /**
  298. * return current length of file.
  299. *
  300. * @param fh - file handle
  301. * @return file length, or -1 on failed
  302. */
  303. long _sys_flen(FILEHANDLE fh)
  304. {
  305. #ifdef DFS_USING_POSIX
  306. struct stat stat;
  307. if (fh < STDERR)
  308. return 0; /* error */
  309. fstat(fh, &stat);
  310. return stat.st_size;
  311. #else
  312. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  313. return 0;
  314. #endif /* DFS_USING_POSIX */
  315. }
  316. int _sys_istty(FILEHANDLE fh)
  317. {
  318. if ((STDIN <= fh) && (fh <= STDERR))
  319. return 1;
  320. else
  321. return 0;
  322. }
  323. int remove(const char *filename)
  324. {
  325. #ifdef DFS_USING_POSIX
  326. return unlink(filename);
  327. #else
  328. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  329. return 0; /* error */
  330. #endif /* DFS_USING_POSIX */
  331. }
  332. #ifdef __MICROLIB
  333. #include <stdio.h>
  334. int fputc(int c, FILE *f)
  335. {
  336. #ifdef RT_USING_CONSOLE
  337. rt_kprintf("%c", (char)c);
  338. return 1;
  339. #else
  340. return 0; /* error */
  341. #endif /* RT_USING_CONSOLE */
  342. }
  343. int fgetc(FILE *f)
  344. {
  345. #ifdef RT_USING_POSIX_STDIO
  346. char ch;
  347. if (libc_stdio_get_console() < 0)
  348. {
  349. LOG_W("Do not invoke standard output before initializing Compiler");
  350. return 0;
  351. }
  352. if (read(STDIN_FILENO, &ch, 1) == 1)
  353. return ch;
  354. #endif /* RT_USING_POSIX_STDIO */
  355. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
  356. return 0; /* error */
  357. }
  358. #endif /* __MICROLIB */