stubs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #endif
  47. /* Register standard Input Output devices. */
  48. if (strcmp(name, __stdin_name) == 0)
  49. return (STDIN);
  50. if (strcmp(name, __stdout_name) == 0)
  51. return (STDOUT);
  52. if (strcmp(name, __stderr_name) == 0)
  53. return (STDERR);
  54. #ifndef RT_USING_DFS
  55. return -1;
  56. #else
  57. /* TODO: adjust open file mode */
  58. fd = open(name, openmode, 0);
  59. if(fd < 0)
  60. return -1;
  61. else
  62. return fd + STDERR + 1;
  63. #endif
  64. }
  65. int _sys_close(FILEHANDLE fh)
  66. {
  67. #ifndef RT_USING_DFS
  68. return 0;
  69. #else
  70. if (fh < STDERR)
  71. return 0;
  72. return close(fh - STDERR - 1);
  73. #endif
  74. }
  75. /**
  76. * read data
  77. *
  78. * @param fh - file handle
  79. * @param buf - buffer to save read data
  80. * @param len - max length of data buffer
  81. * @param mode - useless, for historical reasons
  82. * @return The number of bytes not read.
  83. */
  84. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  85. {
  86. #ifdef RT_USING_DFS
  87. int size;
  88. #endif
  89. if (fh == STDIN)
  90. {
  91. /* TODO */
  92. return 0;
  93. }
  94. if ((fh == STDOUT) || (fh == STDERR))
  95. return -1;
  96. #ifndef RT_USING_DFS
  97. return 0;
  98. #else
  99. size = read(fh - STDERR - 1, buf, len);
  100. if(size >= 0)
  101. return len - size;
  102. else
  103. return -1;
  104. #endif
  105. }
  106. /**
  107. * write data
  108. *
  109. * @param fh - file handle
  110. * @param buf - data buffer
  111. * @param len - buffer length
  112. * @param mode - useless, for historical reasons
  113. * @return a positive number representing the number of characters not written.
  114. */
  115. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  116. {
  117. #ifdef RT_USING_DFS
  118. int size;
  119. #endif
  120. if ((fh == STDOUT) || (fh == STDERR))
  121. {
  122. #ifndef RT_USING_CONSOLE
  123. return 0;
  124. #else
  125. rt_device_t console_device;
  126. extern rt_device_t rt_console_get_device(void);
  127. console_device = rt_console_get_device();
  128. if (console_device != 0) rt_device_write(console_device, 0, buf, len);
  129. return len;
  130. #endif
  131. }
  132. if(fh == STDIN)
  133. return -1;
  134. #ifndef RT_USING_DFS
  135. return 0;
  136. #else
  137. size = write(fh - STDERR - 1, buf, len);
  138. if(size >= 0)
  139. return len - size;
  140. else
  141. return -1;
  142. #endif
  143. }
  144. /**
  145. * put he file pointer at offset pos from the beginning of the file.
  146. *
  147. * @param pos - offset
  148. * @return the current file position, or -1 on failed
  149. */
  150. int _sys_seek(FILEHANDLE fh, long pos)
  151. {
  152. if (fh < STDERR)
  153. return -1;
  154. #ifndef RT_USING_DFS
  155. return -1;
  156. #else
  157. /* position is relative to the start of file fh */
  158. return lseek(fh - STDERR - 1, pos, 0);
  159. #endif
  160. }
  161. /**
  162. * used by tmpnam() or tmpfile()
  163. */
  164. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  165. {
  166. return -1;
  167. }
  168. char *_sys_command_string(char *cmd, int len)
  169. {
  170. /* no support */
  171. return cmd;
  172. }
  173. /* This function writes a character to the console. */
  174. void _ttywrch(int ch)
  175. {
  176. char c;
  177. c = (char)ch;
  178. rt_kprintf(&c);
  179. }
  180. void _sys_exit(int return_code)
  181. {
  182. /* TODO: perhaps exit the thread which is invoking this function */
  183. while (1);
  184. }
  185. /**
  186. * return current length of file.
  187. *
  188. * @param fh - file handle
  189. * @return file length, or -1 on failed
  190. */
  191. long _sys_flen(FILEHANDLE fh)
  192. {
  193. return -1;
  194. }
  195. int _sys_istty(FILEHANDLE fh)
  196. {
  197. return 0;
  198. }
  199. int remove(const char *filename)
  200. {
  201. #ifndef RT_USING_DFS
  202. return -1;
  203. #else
  204. return unlink(filename);
  205. #endif
  206. }
  207. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS)
  208. /* use system implementation in the msh */
  209. #else
  210. int system(const char *string)
  211. {
  212. RT_ASSERT(0);
  213. for(;;);
  214. }
  215. #endif