syscalls.c 5.6 KB

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