summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lib/stdio_compat.c
blob: 2adc4236d29f9918e20a56f9e84b51843ab24c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2013 by Marcin Bukat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "stdio_compat.h"
#include "errno.h"

static _FILE_ __file__[MAX_STDIO_FILES] = {
    {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0},
    {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0},
    {-1,-1,0},{-1,-1,0},{-1,-1,0}
};

_FILE_ *_stdout_ = NULL, *_stderr_ = NULL;

_FILE_ *_fopen_(const char *path, const char *mode)
{
    _FILE_ *f = __file__;
    int flags = O_RDONLY;
    int fd = -1;
    int i;

    /* look for free slot */
    for (i=0; i<MAX_STDIO_FILES; i++, f++)
        if (f->fd == -1)
            break;

    /* no empty slots */
    if (i == MAX_STDIO_FILES)
    {
        rb->splash(HZ, "no open slots");
        return NULL;
    }

    if (*mode != 'r')
    {
        /* Not read */
        flags = (O_WRONLY | O_CREAT | O_TRUNC);
        if (*mode != 'w')
        {
            /* Not write (create of append) */
            flags = (O_WRONLY | O_CREAT | O_APPEND);
            if (*mode != 'a')
            {
                /* Illegal */
                return NULL;
            }
        }
    }

    if (mode[1] == 'b')
    {
        /* Ignore */
        mode++;
    }

    if (mode[1] == '+')
    {
        /* Read and Write. */
        flags |= (O_RDONLY | O_WRONLY);
        flags += (O_RDWR - (O_RDONLY | O_WRONLY));
    }


    fd = rb->open(path, flags, 0666);

    if (fd < 0)
    {
        return NULL;
    }

    /* initialize */
    f->fd = fd;
    f->unget_char = -1;

    return f;
}

int _fflush_(_FILE_ *stream)
{
    (void)stream;
    /* no buffering so we are always in sync */
    return 0;
}

size_t _fread_(void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
{
    ssize_t ret = rb->read(stream->fd, (char *)ptr, size*nmemb);

    if (ret < (ssize_t)(size*nmemb))
        stream->error = -1;
    return ret / size;
}

size_t _fwrite_(const void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
{
    if(stream)
    {
        ssize_t ret = rb->write(stream->fd, ptr, size*nmemb);

        if (ret < (ssize_t)(size*nmemb))
            stream->error = -1;

        return ret / size;
    }
    /* stderr, stdout (disabled) */
    else
    {
        return size * nmemb;
    }
}

int _fseek_(_FILE_ *stream, long offset, int whence)
{
    if(rb->lseek(stream->fd, offset, whence) >= 0)
        return 0;
    else
    {
        rb->splashf(HZ, "lseek() failed: %d fd:%d", errno, stream->fd);
        return -1;
    }
}

long _ftell_(_FILE_ *stream)
{
    return rb->lseek(stream->fd, 0, SEEK_CUR);
}

int _fclose_(_FILE_ *stream)
{
    if (stream->fd == -1)
        return EOF;

    if (rb->close(stream->fd) == -1)
        return EOF;

    /* free the resource */
    stream->fd = -1;

    return 0;
}

int _fgetc_(_FILE_ *stream)
{
    unsigned char c;

    if (stream->unget_char != -1)
    {
        c = stream->unget_char;
        stream->unget_char = -1;
        return c;
    }

    if ( rb->read(stream->fd, &c, 1) != 1)
        return EOF;

    return c;
}

int _ungetc_(int c, _FILE_ *stream)
{
    stream->unget_char = c;

    return c;
}

int _fputc_(int c, _FILE_ *stream)
{
    unsigned char cb = c;

    if (rb->write(stream->fd, &cb, 1) != 1)
        return EOF;

    return cb;
}

char *_fgets_(char *s, int size, _FILE_ *stream)
{
    char *p = s;
    char c = '\0';

    for (int i=0; i<size; i++)
    {
        if ( rb->read(stream->fd, &c, 1) != 1)
            break;

        *p++ = c;

        if (c == '\n')
            break;
    }

    if (p == s)
        return NULL;

    *p = '\0';

    return s;
}

void _clearerr_(_FILE_ *stream)
{
    if (stream)
        stream->error = 0;
}

int _ferror_(_FILE_ *stream)
{
    return (stream) ? stream->error : -1;
}

int _feof_(_FILE_ *stream)
{
    int c = _fgetc_(stream);

    if (c != EOF)
    {
        _ungetc_(c, stream);
        return 0;
    }

    return 0;
}

int _fprintf_(_FILE_ *stream, const char *format, ...)
{
    int len;
    va_list ap;
    char buf[256];

    va_start(ap, format);
    len = rb->vsnprintf(buf, sizeof(buf), format, ap);
    va_end(ap);

    return _fwrite_(buf, 1, len, stream);
}