1
0

syscall_read.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifdef RT_USING_POSIX_STDIO
  14. #include "libc.h"
  15. #endif
  16. #define DBG_TAG "dlib.syscall_read"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. /*
  20. * The "__read" function reads a number of bytes, at most "size" into
  21. * the memory area pointed to by "buffer". It returns the number of
  22. * bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
  23. * occurs.
  24. *
  25. * The template implementation below assumes that the application
  26. * provides the function "MyLowLevelGetchar". It should return a
  27. * character value, or -1 on failure.
  28. */
  29. #pragma module_name = "?__read"
  30. size_t __read(int handle, unsigned char *buf, size_t len)
  31. {
  32. #ifdef RT_USING_POSIX
  33. int size;
  34. if (handle == _LLIO_STDIN)
  35. {
  36. #ifdef RT_USING_POSIX_STDIO
  37. if (libc_stdio_get_console() < 0)
  38. {
  39. LOG_W("Do not invoke standard input before initializing libc");
  40. return 0; /* error, but keep going */
  41. }
  42. return read(STDIN_FILENO, buf, len); /* return the length of the data read */
  43. #else
  44. return _LLIO_ERROR;
  45. #endif /* RT_USING_POSIX_STDIO */
  46. }
  47. else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
  48. {
  49. return _LLIO_ERROR;
  50. }
  51. size = read(handle, buf, len);
  52. return size; /* return the length of the data read */
  53. #else
  54. return _LLIO_ERROR;
  55. #endif /* RT_USING_POSIX */
  56. }