stubs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * File : stubs.c
  3. * Brief : reimplement some basic functions of arm standard c library
  4. *
  5. * This file is part of Device File System in RT-Thread RTOS
  6. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  7. *
  8. * The license and distribution terms for this file may be
  9. * found in the file LICENSE in this distribution or at
  10. * http://www.rt-thread.org/license/LICENSE.
  11. *
  12. * Change Logs:
  13. * Date Author Notes
  14. * 2012-11-23 Yihui The first version
  15. * 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
  16. */
  17. #include <string.h>
  18. #include <rt_sys.h>
  19. #include "rtthread.h"
  20. #ifdef RT_USING_DFS
  21. #include "dfs_posix.h"
  22. #endif
  23. #pragma import(__use_no_semihosting_swi)
  24. /* TODO: Standard IO device handles. */
  25. #define STDIN 1
  26. #define STDOUT 2
  27. #define STDERR 3
  28. /* Standard IO device name defines. */
  29. const char __stdin_name[] = "STDIN";
  30. const char __stdout_name[] = "STDOUT";
  31. const char __stderr_name[] = "STDERR";
  32. /**
  33. * required by fopen() and freopen().
  34. *
  35. * @param name - file name with path.
  36. * @param openmode - a bitmap hose bits mostly correspond directly to
  37. * the ISO mode specification.
  38. * @return -1 if an error occurs.
  39. */
  40. FILEHANDLE _sys_open(const char *name, int openmode)
  41. {
  42. #ifdef RT_USING_DFS
  43. int fd;
  44. int mode = O_RDONLY;
  45. #endif
  46. /* Register standard Input Output devices. */
  47. if (strcmp(name, __stdin_name) == 0)
  48. return (STDIN);
  49. if (strcmp(name, __stdout_name) == 0)
  50. return (STDOUT);
  51. if (strcmp(name, __stderr_name) == 0)
  52. return (STDERR);
  53. #ifndef RT_USING_DFS
  54. return -1;
  55. #else
  56. /* Correct openmode from fopen to open */
  57. if (openmode & OPEN_PLUS)
  58. {
  59. if (openmode & OPEN_W)
  60. {
  61. mode |= (O_RDWR | O_TRUNC | O_CREAT);
  62. }
  63. else if (openmode & OPEN_A)
  64. {
  65. mode |= (O_RDWR | O_APPEND | O_CREAT);
  66. }
  67. else
  68. mode |= O_RDWR;
  69. }
  70. else
  71. {
  72. if (openmode & OPEN_W)
  73. {
  74. mode |= (O_WRONLY | O_TRUNC | O_CREAT);
  75. }
  76. else if (openmode & OPEN_A)
  77. {
  78. mode |= (O_WRONLY | O_APPEND | O_CREAT);
  79. }
  80. }
  81. fd = open(name, mode, 0);
  82. if(fd < 0)
  83. return -1;
  84. else
  85. return fd + STDERR + 1;
  86. #endif
  87. }
  88. int _sys_close(FILEHANDLE fh)
  89. {
  90. #ifndef RT_USING_DFS
  91. return 0;
  92. #else
  93. if (fh < STDERR)
  94. return 0;
  95. return close(fh - STDERR - 1);
  96. #endif
  97. }
  98. /**
  99. * read data
  100. *
  101. * @param fh - file handle
  102. * @param buf - buffer to save read data
  103. * @param len - max length of data buffer
  104. * @param mode - useless, for historical reasons
  105. * @return The number of bytes not read.
  106. */
  107. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  108. {
  109. #ifdef RT_USING_DFS
  110. int size;
  111. #endif
  112. if (fh == STDIN)
  113. {
  114. /* TODO */
  115. return 0;
  116. }
  117. if ((fh == STDOUT) || (fh == STDERR))
  118. return -1;
  119. #ifndef RT_USING_DFS
  120. return 0;
  121. #else
  122. size = read(fh - STDERR - 1, buf, len);
  123. if(size >= 0)
  124. return len - size;
  125. else
  126. return -1;
  127. #endif
  128. }
  129. /**
  130. * write data
  131. *
  132. * @param fh - file handle
  133. * @param buf - data buffer
  134. * @param len - buffer length
  135. * @param mode - useless, for historical reasons
  136. * @return a positive number representing the number of characters not written.
  137. */
  138. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  139. {
  140. #ifdef RT_USING_DFS
  141. int size;
  142. #endif
  143. if ((fh == STDOUT) || (fh == STDERR))
  144. {
  145. #ifndef RT_USING_CONSOLE
  146. return 0;
  147. #else
  148. rt_device_t console_device;
  149. extern rt_device_t rt_console_get_device(void);
  150. console_device = rt_console_get_device();
  151. if (console_device != 0) rt_device_write(console_device, 0, buf, len);
  152. return len;
  153. #endif
  154. }
  155. if(fh == STDIN)
  156. return -1;
  157. #ifndef RT_USING_DFS
  158. return 0;
  159. #else
  160. size = write(fh - STDERR - 1, buf, len);
  161. if(size >= 0)
  162. return len - size;
  163. else
  164. return -1;
  165. #endif
  166. }
  167. /**
  168. * put he file pointer at offset pos from the beginning of the file.
  169. *
  170. * @param pos - offset
  171. * @return the current file position, or -1 on failed
  172. */
  173. int _sys_seek(FILEHANDLE fh, long pos)
  174. {
  175. if (fh < STDERR)
  176. return -1;
  177. #ifndef RT_USING_DFS
  178. return -1;
  179. #else
  180. /* position is relative to the start of file fh */
  181. return lseek(fh - STDERR - 1, pos, 0);
  182. #endif
  183. }
  184. /**
  185. * used by tmpnam() or tmpfile()
  186. */
  187. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  188. {
  189. return -1;
  190. }
  191. char *_sys_command_string(char *cmd, int len)
  192. {
  193. /* no support */
  194. return cmd;
  195. }
  196. void _ttywrch(int ch)
  197. {
  198. char c;
  199. c = (char)ch;
  200. rt_kprintf(&c);
  201. }
  202. void _sys_exit(int return_code)
  203. {
  204. /* TODO: perhaps exit the thread which is invoking this function */
  205. while (1);
  206. }
  207. /**
  208. * return current length of file.
  209. *
  210. * @param fh - file handle
  211. * @return file length, or -1 on failed
  212. */
  213. long _sys_flen(FILEHANDLE fh)
  214. {
  215. return -1;
  216. }
  217. int _sys_istty(FILEHANDLE fh)
  218. {
  219. return 0;
  220. }
  221. int remove(const char *filename)
  222. {
  223. #ifndef RT_USING_DFS
  224. return -1;
  225. #else
  226. return unlink(filename);
  227. #endif
  228. }
  229. /* rename() is defined in dfs_posix.c instead */
  230. #if 0
  231. int rename(const char *old, const char *new)
  232. {
  233. return -1;
  234. }
  235. #endif
  236. int system(const char *string)
  237. {
  238. RT_ASSERT(0);
  239. for(;;);
  240. }