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