syscalls.c 5.6 KB

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