syscalls.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  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_LIBC_USING_FILEIO
  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_LIBC_USING_FILEIO
  62. return 0; /* error */
  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 0; /* error */
  92. else
  93. return fd;
  94. #endif /* RT_LIBC_USING_FILEIO */
  95. }
  96. int _sys_close(FILEHANDLE fh)
  97. {
  98. #ifdef RT_LIBC_USING_FILEIO
  99. if (fh <= STDERR)
  100. return 0; /* error */
  101. return close(fh);
  102. #else
  103. return 0;
  104. #endif /* RT_LIBC_USING_FILEIO */
  105. }
  106. /*
  107. * Read from a file. Can return:
  108. * - zero if the read was completely successful
  109. * - the number of bytes _not_ read, if the read was partially successful
  110. * - the number of bytes not read, plus the top bit set (0x80000000), if
  111. * the read was partially successful due to end of file
  112. * - -1 if some error other than EOF occurred
  113. *
  114. * It is also legal to signal EOF by returning no data but
  115. * signalling no error (i.e. the top-bit-set mechanism need never
  116. * be used).
  117. *
  118. * So if (for example) the user is trying to read 8 bytes at a time
  119. * from a file in which only 5 remain, this routine can do three
  120. * equally valid things:
  121. *
  122. * - it can return 0x80000003 (3 bytes not read due to EOF)
  123. * - OR it can return 3 (3 bytes not read), and then return
  124. * 0x80000008 (8 bytes not read due to EOF) on the next attempt
  125. * - OR it can return 3 (3 bytes not read), and then return
  126. * 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
  127. * attempt
  128. *
  129. * `mode' exists for historical reasons and must be ignored.
  130. */
  131. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  132. {
  133. #ifdef RT_LIBC_USING_FILEIO
  134. int size;
  135. if (fh == STDIN)
  136. {
  137. if (libc_stdio_get_console() < 0)
  138. {
  139. LOG_W("Do not invoke standard output before initializing libc");
  140. return 0;
  141. }
  142. size = read(STDIN_FILENO, buf, len);
  143. return len - size;
  144. }
  145. else if ((fh == STDOUT) || (fh == STDERR))
  146. {
  147. return 0; /* error */
  148. }
  149. size = read(fh, buf, len);
  150. if (size >= 0)
  151. return len - size;
  152. else
  153. return 0; /* error */
  154. #else
  155. return 0; /* error */
  156. #endif /* RT_LIBC_USING_FILEIO */
  157. }
  158. /*
  159. * Write to a file. Returns 0 on success, negative on error, and
  160. * the number of characters _not_ written on partial success.
  161. * `mode' exists for historical reasons and must be ignored.
  162. */
  163. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  164. {
  165. #ifdef RT_LIBC_USING_FILEIO
  166. int size;
  167. #endif /* RT_LIBC_USING_FILEIO */
  168. if ((fh == STDOUT) || (fh == STDERR))
  169. {
  170. #ifdef RT_LIBC_USING_FILEIO
  171. if (libc_stdio_get_console() < 0)
  172. {
  173. LOG_W("Do not invoke standard input before initializing libc");
  174. return 0;
  175. }
  176. size = write(STDOUT_FILENO, buf, len);
  177. return len - size;
  178. #elif defined(RT_USING_CONSOLE)
  179. if (rt_console_get_device())
  180. {
  181. rt_device_write(rt_console_get_device(), -1, buf, len);
  182. }
  183. return 0; /* error */
  184. #endif /* RT_LIBC_USING_FILEIO */
  185. }
  186. else if (fh == STDIN)
  187. {
  188. return 0; /* error */
  189. }
  190. #ifdef RT_LIBC_USING_FILEIO
  191. size = write(fh, buf, len);
  192. if (size >= 0)
  193. return len - size;
  194. else
  195. return 0; /* error */
  196. #else
  197. return 0;
  198. #endif /* RT_LIBC_USING_FILEIO */
  199. }
  200. /*
  201. * Move the file position to a given offset from the file start.
  202. * Returns >=0 on success, <0 on failure.
  203. */
  204. int _sys_seek(FILEHANDLE fh, long pos)
  205. {
  206. #ifdef RT_LIBC_USING_FILEIO
  207. if (fh < STDERR)
  208. return 0; /* error */
  209. /* position is relative to the start of file fh */
  210. return lseek(fh, pos, 0);
  211. #else
  212. return 0; /* error */
  213. #endif /* RT_LIBC_USING_FILEIO */
  214. }
  215. /**
  216. * used by tmpnam() or tmpfile()
  217. */
  218. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  219. {
  220. rt_snprintf(name, maxlength, "tem%03d", fileno);
  221. return 1;
  222. }
  223. char *_sys_command_string(char *cmd, int len)
  224. {
  225. /* no support */
  226. return RT_NULL;
  227. }
  228. /* This function writes a character to the console. */
  229. void _ttywrch(int ch)
  230. {
  231. #ifdef RT_USING_CONSOLE
  232. char c;
  233. c = (char)ch;
  234. rt_kprintf(&c);
  235. #endif /* RT_USING_CONSOLE */
  236. }
  237. /* for exit() and abort() */
  238. RT_WEAK void _sys_exit(int return_code)
  239. {
  240. extern void __rt_libc_exit(int status);
  241. __rt_libc_exit(return_code);
  242. while(1);
  243. }
  244. /**
  245. * return current length of file.
  246. *
  247. * @param fh - file handle
  248. * @return file length, or -1 on failed
  249. */
  250. long _sys_flen(FILEHANDLE fh)
  251. {
  252. #ifdef RT_LIBC_USING_FILEIO
  253. struct stat stat;
  254. if (fh < STDERR)
  255. return 0; /* error */
  256. fstat(fh, &stat);
  257. return stat.st_size;
  258. #else
  259. return 0;
  260. #endif /* RT_LIBC_USING_FILEIO */
  261. }
  262. int _sys_istty(FILEHANDLE fh)
  263. {
  264. if((STDIN <= fh) && (fh <= STDERR))
  265. return 1;
  266. else
  267. return 0;
  268. }
  269. int remove(const char *filename)
  270. {
  271. #ifdef RT_LIBC_USING_FILEIO
  272. return unlink(filename);
  273. #else
  274. return 0; /* error */
  275. #endif /* RT_LIBC_USING_FILEIO */
  276. }
  277. #ifdef __MICROLIB
  278. #include <stdio.h>
  279. int fputc(int c, FILE *f)
  280. {
  281. #ifdef RT_USING_CONSOLE
  282. char ch[2] = {0};
  283. ch[0] = c;
  284. rt_kprintf(&ch[0]);
  285. return 1;
  286. #else
  287. return 0; /* error */
  288. #endif /* RT_USING_CONSOLE */
  289. }
  290. int fgetc(FILE *f)
  291. {
  292. #ifdef RT_LIBC_USING_FILEIO
  293. char ch;
  294. if (libc_stdio_get_console() < 0)
  295. {
  296. LOG_W("Do not invoke standard output before initializing libc");
  297. return 0;
  298. }
  299. if(read(STDIN_FILENO, &ch, 1) == 1)
  300. return ch;
  301. #endif /* RT_LIBC_USING_FILEIO */
  302. return 0; /* error */
  303. }
  304. #endif /* __MICROLIB */