1 /*
2  * Copyright (c) 2025 IAR Systems AB
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stdio.h>
9 
_stdout_hook_default(int c)10 static int _stdout_hook_default(int c)
11 {
12 	(void)(c); /* Prevent warning about unused argument */
13 
14 	return EOF;
15 }
16 
17 static int (*_stdout_hook)(int) = _stdout_hook_default;
18 
__stdout_hook_install(int (* hook)(int))19 void __stdout_hook_install(int (*hook)(int))
20 {
21 	_stdout_hook = hook;
22 }
23 
fputc(int c,FILE * f)24 int fputc(int c, FILE *f)
25 {
26 	return (_stdout_hook)(c);
27 }
28 
29 #pragma weak __write
__write(int handle,const unsigned char * buf,size_t bufSize)30 size_t __write(int handle, const unsigned char *buf, size_t bufSize)
31 {
32 	size_t nChars = 0;
33 	/* Check for the command to flush all handles */
34 	if (handle == -1) {
35 		return 0;
36 	}
37 	/* Check for stdout and stderr
38 	 * (only necessary if FILE descriptors are enabled.)
39 	 */
40 	if (handle != 1 && handle != 2) {
41 		return -1;
42 	}
43 	for (/* Empty */; bufSize > 0; --bufSize) {
44 		int ret = (_stdout_hook)(*buf);
45 
46 		if (ret == EOF) {
47 			break;
48 		}
49 		++buf;
50 		++nChars;
51 	}
52 	return nChars;
53 }
54