blob: 8e094d25975f065a62511b3546ae9ca7dade0b07 [file] [log] [blame]
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +03001/*
2 * Copyright (c) 2015, 2021 Intel Corporation.
Evgeniy Paltsevab130332021-07-09 00:52:31 +03003 * Copyright (c) 2021 Synopsys.
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +03004 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#include <stdio.h>
Gerard Marull-Paretascbd31d72022-05-06 11:23:05 +02009#include <zephyr/sys/libc-hooks.h>
Anas Nashif4e396172023-09-26 22:46:01 +000010#include <zephyr/internal/syscall_handler.h>
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030011#include <string.h>
Gerard Marull-Paretascbd31d72022-05-06 11:23:05 +020012#include <zephyr/sys/errno_private.h>
Evgeniy Paltsevb84a0fe2021-08-05 19:41:37 +030013#include <unistd.h>
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030014#include <errno.h>
15
Nikolay Agishev6e940c52023-06-07 15:02:39 +040016#ifndef STDIN_FILENO
17 #define STDIN_FILENO 0
18#endif
19
20#ifndef STDOUT_FILENO
21 #define STDOUT_FILENO 1
22#endif
23
24#ifndef STDERR_FILENO
25 #define STDERR_FILENO 2
26#endif
27
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030028static int _stdout_hook_default(int c)
29{
30 ARG_UNUSED(c);
31
32 return EOF;
33}
34
35static int (*_stdout_hook)(int) = _stdout_hook_default;
36
37void __stdout_hook_install(int (*hook)(int))
38{
39 _stdout_hook = hook;
40}
41
Stephanos Ioannidis92f0f702021-09-10 16:33:11 +090042int z_impl_zephyr_write_stdout(const void *buffer, int nbytes)
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030043{
44 const char *buf = buffer;
45 int i;
46
47 for (i = 0; i < nbytes; i++) {
48 if (*(buf + i) == '\n') {
49 _stdout_hook('\r');
50 }
51 _stdout_hook(*(buf + i));
52 }
53 return nbytes;
54}
55
56#ifdef CONFIG_USERSPACE
Stephanos Ioannidis92f0f702021-09-10 16:33:11 +090057static inline int z_vrfy_zephyr_write_stdout(const void *buf, int nbytes)
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030058{
Anas Nashifa08bfeb2023-09-27 11:20:28 +000059 K_OOPS(K_SYSCALL_MEMORY_READ(buf, nbytes));
Stephanos Ioannidis92f0f702021-09-10 16:33:11 +090060 return z_impl_zephyr_write_stdout(buf, nbytes);
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030061}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080062#include <zephyr/syscalls/zephyr_write_stdout_mrsh.c>
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030063#endif
64
65#ifndef CONFIG_POSIX_API
66int _write(int fd, const char *buf, unsigned int nbytes)
67{
68 ARG_UNUSED(fd);
69
Stephanos Ioannidis92f0f702021-09-10 16:33:11 +090070 return zephyr_write_stdout(buf, nbytes);
Evgeniy Paltseva5788ff2021-06-04 00:03:23 +030071}
72#endif
Evgeniy Paltsevab130332021-07-09 00:52:31 +030073
Evgeniy Paltsevb84a0fe2021-08-05 19:41:37 +030074/*
75 * It's require to implement _isatty to have STDIN/STDOUT/STDERR buffered
76 * properly.
77 */
78int _isatty(int file)
79{
80 if (file == STDIN_FILENO || file == STDOUT_FILENO || file == STDERR_FILENO) {
81 return 1;
82 }
83
84 return 0;
85}
86
Evgeniy Paltsevab130332021-07-09 00:52:31 +030087int *___errno(void)
88{
89 return z_errno();
90}
Evgeniy Paltsev9f948a82021-08-03 20:24:56 +030091
92__weak void _exit(int status)
93{
94 while (1) {
95 }
96}