syscall_write.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-01-28 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <LowLevelIOInterface.h>
  12. #include <unistd.h>
  13. #ifdef RT_USING_POSIX_STDIO
  14. #include "libc.h"
  15. #endif
  16. #define DBG_TAG "IAR.dlib.syscall_write"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. /*
  20. * The "__write" function should output "size" number of bytes from
  21. * "buffer" in some application-specific way. It should return the
  22. * number of characters written, or _LLIO_ERROR on failure.
  23. *
  24. * If "buffer" is zero then __write should perform flushing of
  25. * internal buffers, if any. In this case "handle" can be -1 to
  26. * indicate that all handles should be flushed.
  27. *
  28. * The template implementation below assumes that the application
  29. * provides the function "MyLowLevelPutchar". It should return the
  30. * character written, or -1 on failure.
  31. */
  32. #pragma module_name = "?__write"
  33. size_t __write(int handle, const unsigned char *buf, size_t len)
  34. {
  35. #ifdef RT_USING_POSIX
  36. int size;
  37. #endif /* RT_USING_POSIX */
  38. if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
  39. {
  40. #ifdef RT_USING_CONSOLE
  41. rt_device_t console_device;
  42. console_device = rt_console_get_device();
  43. if (console_device)
  44. {
  45. rt_device_write(console_device, 0, buf, len);
  46. }
  47. return len; /* return the length of the data written */
  48. #else
  49. return _LLIO_ERROR;
  50. #endif /* RT_USING_CONSOLE */
  51. }
  52. else if (handle == _LLIO_STDIN)
  53. {
  54. return _LLIO_ERROR;
  55. }
  56. else
  57. {
  58. #ifdef RT_USING_POSIX
  59. size = write(handle, buf, len);
  60. return size; /* return the length of the data written */
  61. #else
  62. return _LLIO_ERROR;
  63. #endif /* RT_USING_POSIX */
  64. }
  65. }