syscalls.c 7.6 KB

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