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 <sys/libc-hooks.h>
11 #include <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) = _stdout_hook_default;
22
__stdout_hook_install(int (* hook)(int))23 void __stdout_hook_install(int (*hook)(int))
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 <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 * _MLIBC_RESTRICT s,FILE * _MLIBC_RESTRICT stream)46 int fputs(const char *_MLIBC_RESTRICT s, FILE *_MLIBC_RESTRICT 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
z_impl_zephyr_fwrite(const void * _MLIBC_RESTRICT ptr,size_t size,size_t nitems,FILE * _MLIBC_RESTRICT stream)56 size_t z_impl_zephyr_fwrite(const void *_MLIBC_RESTRICT ptr, size_t size,
57 size_t nitems, FILE *_MLIBC_RESTRICT stream)
58 {
59 size_t i;
60 size_t j;
61 const unsigned char *p;
62
63 if ((stream != stdout && stream != stderr) ||
64 (nitems == 0) || (size == 0)) {
65 return 0;
66 }
67
68 p = ptr;
69 i = nitems;
70 do {
71 j = size;
72 do {
73 if (_stdout_hook((int) *p++) == EOF) {
74 goto done;
75 }
76 j--;
77 } while (j > 0);
78
79 i--;
80 } while (i > 0);
81
82 done:
83 return (nitems - i);
84 }
85
86 #ifdef CONFIG_USERSPACE
z_vrfy_zephyr_fwrite(const void * _MLIBC_RESTRICT ptr,size_t size,size_t nitems,FILE * _MLIBC_RESTRICT stream)87 static inline size_t z_vrfy_zephyr_fwrite(const void *_MLIBC_RESTRICT ptr,
88 size_t size, size_t nitems,
89 FILE *_MLIBC_RESTRICT stream)
90 {
91
92 Z_OOPS(Z_SYSCALL_MEMORY_ARRAY_READ(ptr, nitems, size));
93 return z_impl_zephyr_fwrite((const void *_MLIBC_RESTRICT)ptr, size,
94 nitems, (FILE *_MLIBC_RESTRICT)stream);
95 }
96 #include <syscalls/zephyr_fwrite_mrsh.c>
97 #endif
98
fwrite(const void * _MLIBC_RESTRICT ptr,size_t size,size_t nitems,FILE * _MLIBC_RESTRICT stream)99 size_t fwrite(const void *_MLIBC_RESTRICT ptr, size_t size, size_t nitems,
100 FILE *_MLIBC_RESTRICT stream)
101 {
102 return zephyr_fwrite(ptr, size, nitems, stream);
103 }
104
105
puts(const char * s)106 int puts(const char *s)
107 {
108 if (fputs(s, stdout) == EOF) {
109 return EOF;
110 }
111
112 return fputc('\n', stdout) == EOF ? EOF : 0;
113 }
114