syscalls.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. * 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
  9. * 2020-02-13 Meco Man re-implement exit() and abort()
  10. * 2020-02-21 Meco Man improve and beautify syscalls
  11. * 2020-02-24 Meco Man fix bug of _isatty_r()
  12. */
  13. #include <reent.h>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <rtthread.h>
  18. #ifdef RT_USING_DFS
  19. #include <dfs_posix.h>
  20. #endif
  21. #ifdef RT_USING_MODULE
  22. #include <dlmodule.h>
  23. #endif
  24. /* Reentrant versions of system calls. */
  25. #ifndef _REENT_ONLY
  26. int *__errno ()
  27. {
  28. return _rt_errno();
  29. }
  30. #endif
  31. int _getpid_r(struct _reent *ptr)
  32. {
  33. return 0;
  34. }
  35. int _close_r(struct _reent *ptr, int fd)
  36. {
  37. #ifndef RT_USING_DFS
  38. /* return "not supported" */
  39. ptr->_errno = ENOTSUP;
  40. return -1;
  41. #else
  42. return close(fd);
  43. #endif
  44. }
  45. int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
  46. {
  47. /* return "not supported" */
  48. ptr->_errno = ENOTSUP;
  49. return -1;
  50. }
  51. int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
  52. {
  53. /* return "not supported" */
  54. ptr->_errno = ENOTSUP;
  55. return -1;
  56. }
  57. int _fork_r(struct _reent *ptr)
  58. {
  59. /* return "not supported" */
  60. ptr->_errno = ENOTSUP;
  61. return -1;
  62. }
  63. int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
  64. {
  65. /* return "not supported" */
  66. ptr->_errno = ENOTSUP;
  67. return -1;
  68. }
  69. int _isatty_r(struct _reent *ptr, int fd)
  70. {
  71. if (fd >=0 && fd < 3)
  72. {
  73. return 1;
  74. }
  75. else
  76. {
  77. return 0;
  78. }
  79. }
  80. int _kill_r(struct _reent *ptr, int pid, int sig)
  81. {
  82. /* return "not supported" */
  83. ptr->_errno = ENOTSUP;
  84. return -1;
  85. }
  86. int _link_r(struct _reent *ptr, const char *old, const char *new)
  87. {
  88. /* return "not supported" */
  89. ptr->_errno = ENOTSUP;
  90. return -1;
  91. }
  92. _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
  93. {
  94. #ifndef RT_USING_DFS
  95. /* return "not supported" */
  96. ptr->_errno = ENOTSUP;
  97. return -1;
  98. #else
  99. _off_t rc;
  100. rc = lseek(fd, pos, whence);
  101. return rc;
  102. #endif
  103. }
  104. int _mkdir_r(struct _reent *ptr, const char *name, int mode)
  105. {
  106. #ifndef RT_USING_DFS
  107. /* return "not supported" */
  108. ptr->_errno = ENOTSUP;
  109. return -1;
  110. #else
  111. int rc;
  112. rc = mkdir(name, mode);
  113. return rc;
  114. #endif
  115. }
  116. int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
  117. {
  118. #ifndef RT_USING_DFS
  119. /* return "not supported" */
  120. ptr->_errno = ENOTSUP;
  121. return -1;
  122. #else
  123. int rc;
  124. rc = open(file, flags, mode);
  125. return rc;
  126. #endif
  127. }
  128. _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
  129. {
  130. #ifndef RT_USING_DFS
  131. /* return "not supported" */
  132. ptr->_errno = ENOTSUP;
  133. return -1;
  134. #else
  135. _ssize_t rc;
  136. rc = read(fd, buf, nbytes);
  137. return rc;
  138. #endif
  139. }
  140. int _rename_r(struct _reent *ptr, const char *old, const char *new)
  141. {
  142. #ifndef RT_USING_DFS
  143. /* return "not supported" */
  144. ptr->_errno = ENOTSUP;
  145. return -1;
  146. #else
  147. int rc;
  148. rc = rename(old, new);
  149. return rc;
  150. #endif
  151. }
  152. int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
  153. {
  154. #ifndef RT_USING_DFS
  155. /* return "not supported" */
  156. ptr->_errno = ENOTSUP;
  157. return -1;
  158. #else
  159. int rc;
  160. rc = stat(file, pstat);
  161. return rc;
  162. #endif
  163. }
  164. int _unlink_r(struct _reent *ptr, const char *file)
  165. {
  166. #ifndef RT_USING_DFS
  167. /* return "not supported" */
  168. ptr->_errno = ENOTSUP;
  169. return -1;
  170. #else
  171. return unlink(file);
  172. #endif
  173. }
  174. int _wait_r(struct _reent *ptr, int *status)
  175. {
  176. /* return "not supported" */
  177. ptr->_errno = ENOTSUP;
  178. return -1;
  179. }
  180. _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  181. {
  182. #ifndef RT_USING_DFS
  183. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  184. if (fileno(stdout) == fd)
  185. {
  186. rt_device_t console;
  187. console = rt_console_get_device();
  188. if (console) return rt_device_write(console, -1, buf, nbytes);
  189. }
  190. return 0;
  191. #else
  192. /* return "not supported" */
  193. ptr->_errno = ENOTSUP;
  194. return -1;
  195. #endif /*RT_USING_DEVICE*/
  196. #else
  197. _ssize_t rc;
  198. rc = write(fd, buf, nbytes);
  199. return rc;
  200. #endif
  201. }
  202. #ifdef RT_USING_HEAP /* Memory routine */
  203. void *_malloc_r (struct _reent *ptr, size_t size)
  204. {
  205. void* result;
  206. result = (void*)rt_malloc (size);
  207. if (result == RT_NULL)
  208. {
  209. ptr->_errno = ENOMEM;
  210. }
  211. return result;
  212. }
  213. void *_realloc_r (struct _reent *ptr, void *old, size_t newlen)
  214. {
  215. void* result;
  216. result = (void*)rt_realloc (old, newlen);
  217. if (result == RT_NULL)
  218. {
  219. ptr->_errno = ENOMEM;
  220. }
  221. return result;
  222. }
  223. void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
  224. {
  225. void* result;
  226. result = (void*)rt_calloc (size, len);
  227. if (result == RT_NULL)
  228. {
  229. ptr->_errno = ENOMEM;
  230. }
  231. return result;
  232. }
  233. void _free_r (struct _reent *ptr, void *addr)
  234. {
  235. rt_free (addr);
  236. }
  237. #else
  238. void *
  239. _sbrk_r(struct _reent *ptr, ptrdiff_t incr)
  240. {
  241. return RT_NULL;
  242. }
  243. #endif /*RT_USING_HEAP*/
  244. /* for exit() and abort() */
  245. __attribute__ ((noreturn)) void _exit (int status)
  246. {
  247. extern void __rt_libc_exit(int status);
  248. __rt_libc_exit(status);
  249. while(1);
  250. }
  251. void _system(const char *s)
  252. {
  253. extern int __rt_libc_system(const char *string);
  254. __rt_libc_system(s);
  255. }
  256. void __libc_init_array(void)
  257. {
  258. /* we not use __libc init_aray to initialize C++ objects */
  259. /* __libc_init_array is ARM code, not Thumb; it will cause hardfault. */
  260. }
  261. mode_t umask(mode_t mask)
  262. {
  263. return 022;
  264. }
  265. int flock(int fd, int operation)
  266. {
  267. return 0;
  268. }
  269. /*
  270. These functions are implemented and replaced by the 'common/time.c' file
  271. int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
  272. _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
  273. */