summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/stdio_compat.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lib/stdio_compat.c')
-rw-r--r--apps/plugins/lib/stdio_compat.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/apps/plugins/lib/stdio_compat.c b/apps/plugins/lib/stdio_compat.c
new file mode 100644
index 0000000000..d2b8f9bbc7
--- /dev/null
+++ b/apps/plugins/lib/stdio_compat.c
@@ -0,0 +1,231 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2013 by Marcin Bukat
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include "plugin.h"
22#include "stdio_compat.h"
23
24static _FILE_ __file__[MAX_STDIO_FILES] = {
25 {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0},
26 {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0}
27};
28
29_FILE_ *_fopen_(const char *path, const char *mode)
30{
31 _FILE_ *f = __file__;
32 int flags = O_RDONLY;
33 int fd = -1;
34 int i;
35
36 /* look for free slot */
37 for (i=0; i<MAX_OPEN_FILES; i++, f++)
38 if (f->fd == -1)
39 break;
40
41 /* no empty slots */
42 if (i == MAX_STDIO_FILES)
43 return NULL;
44
45 if (*mode != 'r')
46 {
47 /* Not read */
48 flags = (O_WRONLY | O_CREAT | O_TRUNC);
49 if (*mode != 'w')
50 {
51 /* Not write (create of append) */
52 flags = (O_WRONLY | O_CREAT | O_APPEND);
53 if (*mode != 'a')
54 {
55 /* Illegal */
56 return NULL;
57 }
58 }
59 }
60
61 if (mode[1] == 'b')
62 {
63 /* Ignore */
64 mode++;
65 }
66
67 if (mode[1] == '+')
68 {
69 /* Read and Write. */
70 flags |= (O_RDONLY | O_WRONLY);
71 flags += (O_RDWR - (O_RDONLY | O_WRONLY));
72 }
73
74
75 fd = rb->open(path, flags);
76
77 if (fd == -1)
78 return NULL;
79
80 /* initialize */
81 f->fd = fd;
82 f->unget_char = -1;
83
84 return f;
85}
86
87int _fflush_(_FILE_ *stream)
88{
89 (void)stream;
90 /* no buffering so we are always in sync */
91 return 0;
92}
93
94size_t _fread_(void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
95{
96 ssize_t ret = rb->read(stream->fd, (char *)ptr, size*nmemb);
97
98 if (ret < (ssize_t)(size*nmemb))
99 stream->error = -1;
100
101 return ret;
102}
103
104size_t _fwrite_(const void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
105{
106 ssize_t ret = rb->write(stream->fd, ptr, size*nmemb);
107
108 if (ret < (ssize_t)(size*nmemb))
109 stream->error = -1;
110
111 return ret;
112}
113
114int _fseek_(_FILE_ *stream, long offset, int whence)
115{
116 return rb->lseek(stream->fd, offset, whence);
117}
118
119long _ftell_(_FILE_ *stream)
120{
121 return rb->lseek(stream->fd, 0, SEEK_CUR);
122}
123
124int _fclose_(_FILE_ *stream)
125{
126 if (stream->fd == -1)
127 return EOF;
128
129 if (rb->close(stream->fd) == -1)
130 return EOF;
131
132 /* free the resource */
133 stream->fd = -1;
134
135 return 0;
136}
137
138int _fgetc_(_FILE_ *stream)
139{
140 unsigned char c;
141
142 if (stream->unget_char != -1)
143 {
144 c = stream->unget_char;
145 stream->unget_char = -1;
146 return c;
147 }
148
149 if ( rb->read(stream->fd, &c, 1) != 1)
150 return EOF;
151
152 return c;
153}
154
155int _ungetc_(int c, _FILE_ *stream)
156{
157 stream->unget_char = c;
158
159 return c;
160}
161
162int _fputc_(int c, _FILE_ *stream)
163{
164 unsigned char cb = c;
165
166 if (rb->write(stream->fd, &cb, 1) != 1)
167 return EOF;
168
169 return cb;
170}
171
172char *_fgets_(char *s, int size, _FILE_ *stream)
173{
174 char *p = s;
175 char c = '\0';
176
177 for (int i=0; i<size; i++)
178 {
179 if ( rb->read(stream->fd, &c, 1) != 1)
180 break;
181
182 *p++ = c;
183
184 if (c == '\n')
185 break;
186 }
187
188 if (p == s)
189 return NULL;
190
191 *p = '\0';
192
193 return s;
194}
195
196void _clearerr_(_FILE_ *stream)
197{
198 if (stream)
199 stream->error = 0;
200}
201
202int _ferror_(_FILE_ *stream)
203{
204 return (stream) ? stream->error : -1;
205}
206
207int _feof_(_FILE_ *stream)
208{
209 int c = _fgetc_(stream);
210
211 if (c != EOF)
212 {
213 _ungetc_(c, stream);
214 return 0;
215 }
216
217 return 0;
218}
219
220int _fprintf_(_FILE_ *stream, const char *format, ...)
221{
222 int len;
223 va_list ap;
224 char buf[256];
225
226 va_start(ap, format);
227 len = rb->vsnprintf(buf, sizeof(buf), format, ap);
228 va_end(ap);
229
230 return _fwrite_(buf, 1, len, stream);
231}