stubs.c 4.6 KB

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