syscalls.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <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_DEVIO
  25. #include "libc.h"
  26. #endif /* RT_USING_POSIX_DEVIO */
  27. #define DBG_TAG "armlibc.syscalls"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. #ifdef __CLANG_ARM
  31. __asm(".global __use_no_semihosting\n\t");
  32. #else
  33. #pragma import(__use_no_semihosting_swi)
  34. #endif /* __CLANG_ARM */
  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 0; /* 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 0; /* 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;
  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_DEVIO
  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_devio);
  152. return 0; /* error */
  153. #endif /* RT_USING_POSIX_DEVIO */
  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. #ifdef RT_USING_CONSOLE
  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 /* RT_USING_CONSOLE */
  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. return len - size; /* success */
  215. }
  216. else
  217. {
  218. return 0; /* error */
  219. }
  220. #else
  221. LOG_W("%s: %s", __func__, warning_without_fs);
  222. return 0; /* error */
  223. #endif /* DFS_USING_POSIX */
  224. }
  225. }
  226. /*
  227. * Move the file position to a given offset from the file start.
  228. * Returns >=0 on success, <0 on failure.
  229. */
  230. int _sys_seek(FILEHANDLE fh, long pos)
  231. {
  232. #ifdef DFS_USING_POSIX
  233. if (fh < STDERR)
  234. return 0; /* error */
  235. /* position is relative to the start of file fh */
  236. return lseek(fh, pos, 0);
  237. #else
  238. LOG_W("%s: %s", __func__, warning_without_fs);
  239. return 0; /* error */
  240. #endif /* DFS_USING_POSIX */
  241. }
  242. /**
  243. * used by tmpnam() or tmpfile()
  244. */
  245. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  246. {
  247. rt_snprintf(name, maxlength, "tem%03d", fileno);
  248. return 1;
  249. }
  250. char *_sys_command_string(char *cmd, int len)
  251. {
  252. /* no support */
  253. return RT_NULL;
  254. }
  255. /* This function writes a character to the console. */
  256. void _ttywrch(int ch)
  257. {
  258. #ifdef RT_USING_CONSOLE
  259. char c;
  260. c = (char)ch;
  261. rt_kprintf(&c);
  262. #endif /* RT_USING_CONSOLE */
  263. }
  264. /* for exit() and abort() */
  265. RT_WEAK void _sys_exit(int return_code)
  266. {
  267. extern void __rt_libc_exit(int status);
  268. __rt_libc_exit(return_code);
  269. while(1);
  270. }
  271. /**
  272. * return current length of file.
  273. *
  274. * @param fh - file handle
  275. * @return file length, or -1 on failed
  276. */
  277. long _sys_flen(FILEHANDLE fh)
  278. {
  279. #ifdef DFS_USING_POSIX
  280. struct stat stat;
  281. if (fh < STDERR)
  282. return 0; /* error */
  283. fstat(fh, &stat);
  284. return stat.st_size;
  285. #else
  286. LOG_W("%s: %s", __func__, warning_without_fs);
  287. return 0;
  288. #endif /* DFS_USING_POSIX */
  289. }
  290. int _sys_istty(FILEHANDLE fh)
  291. {
  292. if((STDIN <= fh) && (fh <= STDERR))
  293. return 1;
  294. else
  295. return 0;
  296. }
  297. int remove(const char *filename)
  298. {
  299. #ifdef DFS_USING_POSIX
  300. return unlink(filename);
  301. #else
  302. LOG_W("%s: %s", __func__, warning_without_fs);
  303. return 0; /* error */
  304. #endif /* DFS_USING_POSIX */
  305. }
  306. #ifdef __MICROLIB
  307. #include <stdio.h>
  308. int fputc(int c, FILE *f)
  309. {
  310. #ifdef RT_USING_CONSOLE
  311. char ch[2] = {0};
  312. ch[0] = c;
  313. rt_kprintf(&ch[0]);
  314. return 1;
  315. #else
  316. return 0; /* error */
  317. #endif /* RT_USING_CONSOLE */
  318. }
  319. int fgetc(FILE *f)
  320. {
  321. #ifdef RT_USING_POSIX_DEVIO
  322. char ch;
  323. if (libc_stdio_get_console() < 0)
  324. {
  325. LOG_W("Do not invoke standard output before initializing Compiler");
  326. return 0;
  327. }
  328. if(read(STDIN_FILENO, &ch, 1) == 1)
  329. return ch;
  330. #endif /* RT_USING_POSIX_DEVIO */
  331. LOG_W("%s: %s", __func__, warning_without_devio);
  332. return 0; /* error */
  333. }
  334. #endif /* __MICROLIB */