1
0

syscall_write.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #define DBG_TAG "dlib.syscall_write"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. /*
  17. * The "__write" function should output "size" number of bytes from
  18. * "buffer" in some application-specific way. It should return the
  19. * number of characters written, or _LLIO_ERROR on failure.
  20. *
  21. * If "buffer" is zero then __write should perform flushing of
  22. * internal buffers, if any. In this case "handle" can be -1 to
  23. * indicate that all handles should be flushed.
  24. *
  25. * The template implementation below assumes that the application
  26. * provides the function "MyLowLevelPutchar". It should return the
  27. * character written, or -1 on failure.
  28. */
  29. #pragma module_name = "?__write"
  30. size_t __write(int handle, const unsigned char *buf, size_t len)
  31. {
  32. #ifdef DFS_USING_POSIX
  33. int size;
  34. #endif /* DFS_USING_POSIX */
  35. if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
  36. {
  37. #ifdef RT_USING_CONSOLE
  38. rt_device_t console_device;
  39. console_device = rt_console_get_device();
  40. if (console_device)
  41. {
  42. rt_device_write(console_device, 0, buf, len);
  43. }
  44. return len; /* return the length of the data written */
  45. #else
  46. return _LLIO_ERROR;
  47. #endif /* RT_USING_CONSOLE */
  48. }
  49. else if (handle == _LLIO_STDIN)
  50. {
  51. return _LLIO_ERROR;
  52. }
  53. else
  54. {
  55. #ifdef DFS_USING_POSIX
  56. size = write(handle, buf, len);
  57. return size; /* return the length of the data written */
  58. #else
  59. return _LLIO_ERROR;
  60. #endif /* DFS_USING_POSIX */
  61. }
  62. }