blob: 7bc8f7833d8358e23385f98ba31b6e54c2c91c9a [file] [log] [blame]
Harald Welte361ed202019-02-24 21:15:39 +01001/**
2 * \file
3 *
4 * \brief STDIO redirection
5 *
6 * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33
34#include <stdio_io.h>
35#include <stdio.h>
36
37#ifndef _UNIT_TEST_
38#include <yfuns.h>
39#else
40#define _STD_BEGIN
41#define _STD_END
42#define _LLIO_ERROR ((size_t)-1) /* For __read and __write. */
43#define _LLIO_STDIN 0
44#define _LLIO_STDOUT 1
45#define _LLIO_STDERR 2
46#endif
47
48#if (__VER__ < 8010000)
49/* Refer http://ftp.iar.se/WWWfiles/arm/webic/doc/EWARM_MigrationGuide.ENU.pdf */
50_STD_BEGIN
51#endif
52
53#pragma module_name = "?__write"
54
55/*! \brief Writes a number of bytes, at most \a size, from the memory area
56 * pointed to by \a buffer.
57 *
58 * If \a buffer is zero then \ref __write performs flushing of internal buffers,
59 * if any. In this case, \a handle can be \c -1 to indicate that all handles
60 * should be flushed.
61 *
62 * \param handle File handle to write to.
63 * \param buffer Pointer to buffer to read bytes to write from.
64 * \param size Number of bytes to write.
65 *
66 * \return The number of bytes written, or \c _LLIO_ERROR on failure.
67 */
68size_t __write(int handle, const unsigned char *buffer, size_t size)
69{
70 int n = 0;
71
72 if (buffer == 0) {
73 /* This means that we should flush internal buffers. */
74 return 0;
75 }
76
77 /* This implementation only writes to stdout and stderr.
78 * For all other file handles, it returns failure. */
79 if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
80 return _LLIO_ERROR;
81 }
82
83 n = stdio_io_write((const uint8_t *)buffer, size);
84 if (n < 0) {
85 return _LLIO_ERROR;
86 }
87
88 return n;
89}
90
91#if (__VER__ < 8010000)
92/* Refer http://ftp.iar.se/WWWfiles/arm/webic/doc/EWARM_MigrationGuide.ENU.pdf */
93_STD_END
94#endif