syscalls.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. void *
  168. _sbrk_r(struct _reent *ptr, ptrdiff_t incr)
  169. {
  170. /* no use this routine to get memory */
  171. return RT_NULL;
  172. }
  173. int
  174. _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
  175. {
  176. #ifndef RT_USING_DFS
  177. /* return "not supported" */
  178. ptr->_errno = ENOTSUP;
  179. return -1;
  180. #else
  181. int rc;
  182. rc = stat(file, pstat);
  183. return rc;
  184. #endif
  185. }
  186. int
  187. _unlink_r(struct _reent *ptr, const char *file)
  188. {
  189. #ifndef RT_USING_DFS
  190. /* return "not supported" */
  191. ptr->_errno = ENOTSUP;
  192. return -1;
  193. #else
  194. return unlink(file);
  195. #endif
  196. }
  197. int
  198. _wait_r(struct _reent *ptr, int *status)
  199. {
  200. /* return "not supported" */
  201. ptr->_errno = ENOTSUP;
  202. return -1;
  203. }
  204. _ssize_t
  205. _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
  206. {
  207. #ifndef RT_USING_DFS
  208. #ifdef RT_USING_DEVICE
  209. if (fileno(stdout) == fd)
  210. {
  211. rt_device_t console;
  212. console = rt_console_get_device();
  213. if (console) return rt_device_write(console, -1, buf, nbytes);
  214. }
  215. return 0;
  216. #else
  217. /* return "not supported" */
  218. ptr->_errno = ENOTSUP;
  219. return -1;
  220. #endif /*RT_USING_DEVICE*/
  221. #else
  222. _ssize_t rc;
  223. rc = write(fd, buf, nbytes);
  224. return rc;
  225. #endif
  226. }
  227. /* Memory routine */
  228. void *
  229. _malloc_r (struct _reent *ptr, size_t size)
  230. {
  231. void* result;
  232. result = (void*)rt_malloc (size);
  233. if (result == RT_NULL)
  234. {
  235. ptr->_errno = ENOMEM;
  236. }
  237. return result;
  238. }
  239. void *
  240. _realloc_r (struct _reent *ptr, void *old, size_t newlen)
  241. {
  242. void* result;
  243. result = (void*)rt_realloc (old, newlen);
  244. if (result == RT_NULL)
  245. {
  246. ptr->_errno = ENOMEM;
  247. }
  248. return result;
  249. }
  250. void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
  251. {
  252. void* result;
  253. result = (void*)rt_calloc (size, len);
  254. if (result == RT_NULL)
  255. {
  256. ptr->_errno = ENOMEM;
  257. }
  258. return result;
  259. }
  260. void
  261. _free_r (struct _reent *ptr, void *addr)
  262. {
  263. rt_free (addr);
  264. }
  265. /* for exit() and abort() */
  266. __attribute__ ((noreturn)) void
  267. _exit (int status)
  268. {
  269. extern void __rt_libc_exit(int status);
  270. __rt_libc_exit(status);
  271. while(1);
  272. }
  273. void
  274. _system(const char *s)
  275. {
  276. extern int __rt_libc_system(const char *string);
  277. __rt_libc_system(s);
  278. }
  279. void __libc_init_array(void)
  280. {
  281. /* we not use __libc init_aray to initialize C++ objects */
  282. }
  283. mode_t umask(mode_t mask)
  284. {
  285. return 022;
  286. }
  287. int flock(int fd, int operation)
  288. {
  289. return 0;
  290. }
  291. /*
  292. These functions are implemented and replaced by the 'common/time.c' file
  293. int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
  294. _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
  295. */