syscall_write.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifdef RT_USING_DFS
  12. #include <dfs_posix.h>
  13. #endif
  14. #include <yfuns.h>
  15. #include "libc.h"
  16. #define DBG_TAG "dlib.syscall_write"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. #pragma module_name = "?__write"
  20. size_t __write(int handle, const unsigned char *buf, size_t len)
  21. {
  22. #ifdef RT_USING_DFS
  23. int size;
  24. #endif
  25. if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
  26. {
  27. #ifndef RT_USING_CONSOLE
  28. return _LLIO_ERROR;
  29. #else
  30. #ifdef RT_USING_POSIX
  31. if (libc_stdio_get_console() < 0)
  32. {
  33. LOG_E("invoke standard output before initializing libc");
  34. return _LLIO_ERROR;
  35. }
  36. return write(STDOUT_FILENO, (void*)buf, len);
  37. #else
  38. rt_device_t console_device;
  39. console_device = rt_console_get_device();
  40. if (console_device != 0)
  41. {
  42. rt_device_write(console_device, 0, buf, len);
  43. }
  44. return len;
  45. #endif
  46. #endif
  47. }
  48. else if (handle == _LLIO_STDIN)
  49. {
  50. return _LLIO_ERROR;
  51. }
  52. #ifndef RT_USING_DFS
  53. return _LLIO_ERROR;
  54. #else
  55. size = write(handle, buf, len);
  56. return size;
  57. #endif
  58. }