1 /*
2 Copyright © 2019 Keith Packard
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdio.h>
34 #include <math.h>
35 #include <unistd.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 
40 #ifdef TINY_STDIO
41 /* Buffered output routines for newlib-nano stdio */
42 
43 static char write_buf[512];
44 static int write_len;
45 
46 static int
ao_flush(FILE * ignore)47 ao_flush(FILE *ignore)
48 {
49 	if (write_len) {
50 		int off = 0;
51 		while (write_len) {
52 			int this = write(1, write_buf + off, write_len);
53 			if (this < 0) {
54 				write_len = 0;
55 				return -1;
56 			}
57 			off += this;
58 			write_len -= this;
59 		}
60 	}
61 	return 0;
62 }
63 
64 static int
ao_putc(char c,FILE * ignore)65 ao_putc(char c, FILE *ignore)
66 {
67 	write_buf[write_len++] = c;
68 	if (write_len < sizeof (write_buf))
69 		return 0;
70 	return ao_flush(ignore);
71 }
72 
73 static char read_buf[512];
74 static int read_len;
75 static int read_off;
76 
77 static int
ao_getc(FILE * ignore)78 ao_getc(FILE *ignore)
79 {
80 	if (read_off >= read_len) {
81 		(void) ao_flush(ignore);
82 		read_off = 0;
83 		read_len = read(0, read_buf, sizeof (read_buf));
84 		if (read_len <= 0) {
85 			read_len = 0;
86 			return -1;
87 		}
88 	}
89 	return (unsigned char) read_buf[read_off++];
90 }
91 
92 __attribute__((destructor))
ao_exit(void)93 static void ao_exit(void)
94 {
95 	ao_flush(stdout);
96 }
97 
98 static FILE __stdio = {
99 	.flags = __SRD|__SWR,
100 	.put = ao_putc,
101 	.get = ao_getc,
102 	.flush = ao_flush,
103 };
104 
105 #ifdef __strong_reference
106 #define STDIO_ALIAS(x) __strong_reference(stdin, x);
107 #else
108 #define STDIO_ALIAS(x) FILE *const x = &__stdio;
109 #endif
110 
111 FILE *const stdin = &__stdio;
112 STDIO_ALIAS(stdout);
113 STDIO_ALIAS(stderr);
114 
115 #else
fstat(int fd,struct stat * statb)116 int fstat(int fd, struct stat *statb) { errno = ENOTTY; return -1; }
117 #endif
118