stdout_console.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* stdout_console.c */
  2. /*
  3. * Copyright (c) 2014 Wind River Systems, Inc.
  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. static int _stdout_hook_default(int c)
  12. {
  13. (void)(c); /* Prevent warning about unused argument */
  14. return EOF;
  15. }
  16. static int (*_stdout_hook)(int) = _stdout_hook_default;
  17. void __stdout_hook_install(int (*hook)(int))
  18. {
  19. _stdout_hook = hook;
  20. }
  21. void *__stdout_get_hook(void)
  22. {
  23. return _stdout_hook;
  24. }
  25. int z_impl_zephyr_fputc(int c, FILE *stream)
  26. {
  27. return (stream == stdout || stream == stderr) ? _stdout_hook(c) : EOF;
  28. }
  29. #ifdef CONFIG_USERSPACE
  30. static inline int z_vrfy_zephyr_fputc(int c, FILE *stream)
  31. {
  32. return z_impl_zephyr_fputc(c, stream);
  33. }
  34. #include <syscalls/zephyr_fputc_mrsh.c>
  35. #endif
  36. int fputc(int c, FILE *stream)
  37. {
  38. return zephyr_fputc(c, stream);
  39. }
  40. int fputs(const char *_MLIBC_RESTRICT s, FILE *_MLIBC_RESTRICT stream)
  41. {
  42. int len = strlen(s);
  43. int ret;
  44. ret = fwrite(s, 1, len, stream);
  45. return len == ret ? 0 : EOF;
  46. }
  47. size_t z_impl_zephyr_fwrite(const void *_MLIBC_RESTRICT ptr, size_t size,
  48. size_t nitems, FILE *_MLIBC_RESTRICT stream)
  49. {
  50. size_t i;
  51. size_t j;
  52. const unsigned char *p;
  53. if ((stream != stdout && stream != stderr) ||
  54. (nitems == 0) || (size == 0)) {
  55. return 0;
  56. }
  57. p = ptr;
  58. i = nitems;
  59. do {
  60. j = size;
  61. do {
  62. if (_stdout_hook((int) *p++) == EOF) {
  63. goto done;
  64. }
  65. j--;
  66. } while (j > 0);
  67. i--;
  68. } while (i > 0);
  69. done:
  70. return (nitems - i);
  71. }
  72. #ifdef CONFIG_USERSPACE
  73. static inline size_t z_vrfy_zephyr_fwrite(const void *_MLIBC_RESTRICT ptr,
  74. size_t size, size_t nitems,
  75. FILE *_MLIBC_RESTRICT stream)
  76. {
  77. Z_OOPS(Z_SYSCALL_MEMORY_ARRAY_READ(ptr, nitems, size));
  78. return z_impl_zephyr_fwrite((const void *_MLIBC_RESTRICT)ptr, size,
  79. nitems, (FILE *_MLIBC_RESTRICT)stream);
  80. }
  81. #include <syscalls/zephyr_fwrite_mrsh.c>
  82. #endif
  83. size_t fwrite(const void *_MLIBC_RESTRICT ptr, size_t size, size_t nitems,
  84. FILE *_MLIBC_RESTRICT stream)
  85. {
  86. return zephyr_fwrite(ptr, size, nitems, stream);
  87. }
  88. int puts(const char *s)
  89. {
  90. if (fputs(s, stdout) == EOF) {
  91. return EOF;
  92. }
  93. return fputc('\n', stdout) == EOF ? EOF : 0;
  94. }