syscall_read.c 1.7 KB

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