syscalls.c 5.0 KB

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