syscall_write.c 1.8 KB

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