stubs.c 5.3 KB

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