syscalls.c 6.0 KB

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