syscalls.c 7.3 KB

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