syscalls.c 7.5 KB

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