libc-hooks.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2015, 2021 Intel Corporation.
  3. * Copyright (c) 2021 Synopsys.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <stdio.h>
  8. #include <sys/libc-hooks.h>
  9. #include <syscall_handler.h>
  10. #include <string.h>
  11. #include <sys/errno_private.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. static int _stdout_hook_default(int c)
  15. {
  16. ARG_UNUSED(c);
  17. return EOF;
  18. }
  19. static int (*_stdout_hook)(int) = _stdout_hook_default;
  20. void __stdout_hook_install(int (*hook)(int))
  21. {
  22. _stdout_hook = hook;
  23. }
  24. int z_impl_zephyr_write_stdout(const void *buffer, int nbytes)
  25. {
  26. const char *buf = buffer;
  27. int i;
  28. for (i = 0; i < nbytes; i++) {
  29. if (*(buf + i) == '\n') {
  30. _stdout_hook('\r');
  31. }
  32. _stdout_hook(*(buf + i));
  33. }
  34. return nbytes;
  35. }
  36. #ifdef CONFIG_USERSPACE
  37. static inline int z_vrfy_zephyr_write_stdout(const void *buf, int nbytes)
  38. {
  39. Z_OOPS(Z_SYSCALL_MEMORY_READ(buf, nbytes));
  40. return z_impl_zephyr_write_stdout(buf, nbytes);
  41. }
  42. #include <syscalls/zephyr_write_stdout_mrsh.c>
  43. #endif
  44. #ifndef CONFIG_POSIX_API
  45. int _write(int fd, const char *buf, unsigned int nbytes)
  46. {
  47. ARG_UNUSED(fd);
  48. return zephyr_write_stdout(buf, nbytes);
  49. }
  50. #endif
  51. /*
  52. * It's require to implement _isatty to have STDIN/STDOUT/STDERR buffered
  53. * properly.
  54. */
  55. int _isatty(int file)
  56. {
  57. if (file == STDIN_FILENO || file == STDOUT_FILENO || file == STDERR_FILENO) {
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. int *___errno(void)
  63. {
  64. return z_errno();
  65. }
  66. __weak void _exit(int status)
  67. {
  68. while (1) {
  69. }
  70. }