1 /* stdout_console.c */
2
3 /*
4 * Copyright (c) 2014 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <stdio.h>
10 #include <zephyr/sys/libc-hooks.h>
11 #include <zephyr/internal/syscall_handler.h>
12 #include <string.h>
13
_stdout_hook_default(int c)14 static int _stdout_hook_default(int c)
15 {
16 (void)(c); /* Prevent warning about unused argument */
17
18 return EOF;
19 }
20
21 static int (*_stdout_hook)(int c) = _stdout_hook_default;
22
__stdout_hook_install(int (* hook)(int c))23 void __stdout_hook_install(int (*hook)(int c))
24 {
25 _stdout_hook = hook;
26 }
27
z_impl_zephyr_fputc(int c,FILE * stream)28 int z_impl_zephyr_fputc(int c, FILE *stream)
29 {
30 return ((stream == stdout) || (stream == stderr)) ? _stdout_hook(c) : EOF;
31 }
32
33 #ifdef CONFIG_USERSPACE
z_vrfy_zephyr_fputc(int c,FILE * stream)34 static inline int z_vrfy_zephyr_fputc(int c, FILE *stream)
35 {
36 return z_impl_zephyr_fputc(c, stream);
37 }
38 #include <zephyr/syscalls/zephyr_fputc_mrsh.c>
39 #endif
40
fputc(int c,FILE * stream)41 int fputc(int c, FILE *stream)
42 {
43 return zephyr_fputc(c, stream);
44 }
45
fputs(const char * ZRESTRICT s,FILE * ZRESTRICT stream)46 int fputs(const char *ZRESTRICT s, FILE *ZRESTRICT stream)
47 {
48 int len = strlen(s);
49 int ret;
50
51 ret = fwrite(s, 1, len, stream);
52
53 return (len == ret) ? 0 : EOF;
54 }
55
56 #undef putc
putc(int c,FILE * stream)57 int putc(int c, FILE *stream)
58 {
59 return zephyr_fputc(c, stream);
60 }
61
62 #undef putchar
putchar(int c)63 int putchar(int c)
64 {
65 return zephyr_fputc(c, stdout);
66 }
67
z_impl_zephyr_fwrite(const void * ZRESTRICT ptr,size_t size,size_t nitems,FILE * ZRESTRICT stream)68 size_t z_impl_zephyr_fwrite(const void *ZRESTRICT ptr, size_t size,
69 size_t nitems, FILE *ZRESTRICT stream)
70 {
71 size_t i;
72 size_t j;
73 const unsigned char *p;
74
75 if (((stream != stdout) && (stream != stderr)) ||
76 (nitems == 0) || (size == 0)) {
77 return 0;
78 }
79
80 p = ptr;
81 i = nitems;
82 do {
83 j = size;
84 do {
85 if (_stdout_hook((int) *p++) == EOF) {
86 goto done;
87 }
88 j--;
89 } while (j > 0);
90
91 i--;
92 } while (i > 0);
93
94 done:
95 return (nitems - i);
96 }
97
98 #ifdef CONFIG_USERSPACE
z_vrfy_zephyr_fwrite(const void * ZRESTRICT ptr,size_t size,size_t nitems,FILE * ZRESTRICT stream)99 static inline size_t z_vrfy_zephyr_fwrite(const void *ZRESTRICT ptr,
100 size_t size, size_t nitems,
101 FILE *ZRESTRICT stream)
102 {
103
104 K_OOPS(K_SYSCALL_MEMORY_ARRAY_READ(ptr, nitems, size));
105 return z_impl_zephyr_fwrite(ptr, size, nitems, stream);
106 }
107 #include <zephyr/syscalls/zephyr_fwrite_mrsh.c>
108 #endif
109
fwrite(const void * ZRESTRICT ptr,size_t size,size_t nitems,FILE * ZRESTRICT stream)110 size_t fwrite(const void *ZRESTRICT ptr, size_t size, size_t nitems,
111 FILE *ZRESTRICT stream)
112 {
113 return zephyr_fwrite(ptr, size, nitems, stream);
114 }
115
116
puts(const char * s)117 int puts(const char *s)
118 {
119 if (fputs(s, stdout) == EOF) {
120 return EOF;
121 }
122
123 return (fputc('\n', stdout) == EOF) ? EOF : 0;
124 }
125