stubs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. */
  16. #include <string.h>
  17. #include <rt_sys.h>
  18. #include "rtthread.h"
  19. #ifdef RT_USING_DFS
  20. #include "dfs_posix.h"
  21. #endif
  22. #pragma import(__use_no_semihosting_swi)
  23. /* TODO: Standard IO device handles. */
  24. #define STDIN 1
  25. #define STDOUT 2
  26. #define STDERR 3
  27. /* Standard IO device name defines. */
  28. const char __stdin_name[] = "STDIN";
  29. const char __stdout_name[] = "STDOUT";
  30. const char __stderr_name[] = "STDERR";
  31. /**
  32. * required by fopen() and freopen().
  33. *
  34. * @param name - file name with path.
  35. * @param openmode - a bitmap hose bits mostly correspond directly to
  36. * the ISO mode specification.
  37. * @return -1 if an error occurs.
  38. */
  39. FILEHANDLE _sys_open(const char *name, int openmode)
  40. {
  41. /* Register standard Input Output devices. */
  42. if (strcmp(name, __stdin_name) == 0)
  43. return (STDIN);
  44. if (strcmp(name, __stdout_name) == 0)
  45. return (STDOUT);
  46. if (strcmp(name, __stderr_name) == 0)
  47. return (STDERR);
  48. #ifndef RT_USING_DFS
  49. return -1;
  50. #else
  51. /* TODO: adjust open file mode */
  52. return open(name, openmode, 0);
  53. #endif
  54. }
  55. int _sys_close(FILEHANDLE fh)
  56. {
  57. #ifndef RT_USING_DFS
  58. return 0;
  59. #else
  60. if (fh < 3)
  61. return 0;
  62. return close(fh);
  63. #endif
  64. }
  65. /**
  66. * read data
  67. *
  68. * @param fh - file handle
  69. * @param buf - buffer to save read data
  70. * @param len - max length of data buffer
  71. * @param mode - useless, for historical reasons
  72. * @return actual read data length
  73. */
  74. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  75. {
  76. if (fh == STDIN)
  77. {
  78. /* TODO */
  79. return 0;
  80. }
  81. #ifndef RT_USING_DFS
  82. return 0;
  83. #else
  84. return read(fh, buf, len);
  85. #endif
  86. }
  87. /**
  88. * write data
  89. *
  90. * @param fh - file handle
  91. * @param buf - data buffer
  92. * @param len - buffer length
  93. * @param mode - useless, for historical reasons
  94. * @return actual written data length
  95. */
  96. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  97. {
  98. if ((fh == STDOUT) || (fh == STDERR))
  99. {
  100. #ifndef RT_USING_CONSOLE
  101. return 0;
  102. #else
  103. rt_device_t console_device;
  104. extern rt_device_t rt_console_get_device(void);
  105. console_device = rt_console_get_device();
  106. if (console_device != 0) rt_device_write(console_device, 0, buf, len);
  107. return len;
  108. #endif
  109. }
  110. #ifndef RT_USING_DFS
  111. return 0;
  112. #else
  113. return write(fh, buf, len);
  114. #endif
  115. }
  116. /**
  117. * put he file pointer at offset pos from the beginning of the file.
  118. *
  119. * @param pos - offset
  120. * @return the current file position, or -1 on failed
  121. */
  122. int _sys_seek(FILEHANDLE fh, long pos)
  123. {
  124. #ifndef RT_USING_DFS
  125. return -1;
  126. #else
  127. /* position is relative to the start of file fh */
  128. return lseek(fh, pos, 0);
  129. #endif
  130. }
  131. /**
  132. * used by tmpnam() or tmpfile()
  133. */
  134. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  135. {
  136. return -1;
  137. }
  138. char *_sys_command_string(char *cmd, int len)
  139. {
  140. /* no support */
  141. return cmd;
  142. }
  143. void _ttywrch(int ch)
  144. {
  145. /* TODO */
  146. }
  147. void _sys_exit(int return_code)
  148. {
  149. /* TODO: perhaps exit the thread which is invoking this function */
  150. while (1);
  151. }
  152. /**
  153. * return current length of file.
  154. *
  155. * @param fh - file handle
  156. * @return file length, or -1 on failed
  157. */
  158. long _sys_flen(FILEHANDLE fh)
  159. {
  160. return -1;
  161. }
  162. int _sys_istty(FILEHANDLE fh)
  163. {
  164. return 0;
  165. }
  166. int remove(const char *filename)
  167. {
  168. #ifndef RT_USING_DFS
  169. return -1;
  170. #else
  171. return unlink(filename);
  172. #endif
  173. }
  174. /* rename() is defined in dfs_posix.c instead */
  175. #if 0
  176. int rename(const char *old, const char *new)
  177. {
  178. return -1;
  179. }
  180. #endif
  181. int system(const char *string)
  182. {
  183. RT_ASSERT(0);
  184. for(;;);
  185. }