syscalls.c 5.6 KB

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