syscall_write.c 1.4 KB

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