syscall_write.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <yfuns.h>
  12. #include <unistd.h>
  13. #ifdef RT_USING_POSIX
  14. #include "libc.h"
  15. #endif
  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_POSIX
  23. int size;
  24. #endif /* RT_USING_POSIX */
  25. if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
  26. {
  27. #ifdef RT_USING_POSIX
  28. if (libc_stdio_get_console() < 0)
  29. {
  30. LOG_W("Do not invoke standard output before initializing libc");
  31. return 0;
  32. }
  33. return write(STDOUT_FILENO, (void*)buf, len);
  34. #elif defined(RT_USING_CONSOLE)
  35. rt_device_t console_device;
  36. console_device = rt_console_get_device();
  37. if (console_device != 0)
  38. {
  39. rt_device_write(console_device, 0, buf, len);
  40. }
  41. return len;
  42. #else
  43. return _LLIO_ERROR;
  44. #endif /* RT_USING_POSIX */
  45. }
  46. else if (handle == _LLIO_STDIN)
  47. {
  48. return _LLIO_ERROR;
  49. }
  50. #ifdef RT_USING_POSIX
  51. size = write(handle, buf, len);
  52. return size;
  53. #else
  54. return _LLIO_ERROR;
  55. #endif /* RT_USING_POSIX */
  56. }