syscalls.c 5.7 KB

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