Subversion Repositories HelenOS

Rev

Rev 4514 | Rev 4590 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4514 Rev 4541
Line 33... Line 33...
33
 */
33
 */
34
 
34
 
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <unistd.h>
36
#include <unistd.h>
37
#include <fcntl.h>
37
#include <fcntl.h>
-
 
38
#include <assert.h>
38
#include <string.h>
39
#include <string.h>
39
#include <errno.h>
40
#include <errno.h>
40
#include <bool.h>
41
#include <bool.h>
41
#include <malloc.h>
42
#include <malloc.h>
42
#include <io/klog.h>
43
#include <io/klog.h>
43
#include <vfs/vfs.h>
44
#include <vfs/vfs.h>
44
#include <ipc/devmap.h>
45
#include <ipc/devmap.h>
45
#include <adt/list.h>
46
#include <adt/list.h>
46
 
47
 
-
 
48
static void _fflushbuf(FILE *stream);
-
 
49
 
47
static FILE stdin_null = {
50
static FILE stdin_null = {
48
    .fd = -1,
51
    .fd = -1,
49
    .error = true,
52
    .error = true,
50
    .eof = true,
53
    .eof = true,
51
    .klog = false,
54
    .klog = false,
52
    .phone = -1
55
    .phone = -1,
-
 
56
    .btype = _IONBF,
-
 
57
    .buf = NULL,
-
 
58
    .buf_size = 0,
-
 
59
    .buf_head = NULL
53
};
60
};
54
 
61
 
55
static FILE stdout_klog = {
62
static FILE stdout_klog = {
56
    .fd = -1,
63
    .fd = -1,
57
    .error = false,
64
    .error = false,
58
    .eof = false,
65
    .eof = false,
59
    .klog = true,
66
    .klog = true,
60
    .phone = -1
67
    .phone = -1,
-
 
68
    .btype = _IOLBF,
-
 
69
    .buf = NULL,
-
 
70
    .buf_size = BUFSIZ,
-
 
71
    .buf_head = NULL
61
};
72
};
62
 
73
 
63
static FILE stderr_klog = {
74
static FILE stderr_klog = {
64
    .fd = -1,
75
    .fd = -1,
65
    .error = false,
76
    .error = false,
66
    .eof = false,
77
    .eof = false,
67
    .klog = true,
78
    .klog = true,
68
    .phone = -1
79
    .phone = -1,
-
 
80
    .btype = _IONBF,
-
 
81
    .buf = NULL,
-
 
82
    .buf_size = 0,
-
 
83
    .buf_head = NULL
69
};
84
};
70
 
85
 
71
FILE *stdin = NULL;
86
FILE *stdin = NULL;
72
FILE *stdout = NULL;
87
FILE *stdout = NULL;
73
FILE *stderr = NULL;
88
FILE *stderr = NULL;
Line 154... Line 169...
154
    }
169
    }
155
   
170
   
156
    return true;
171
    return true;
157
}
172
}
158
 
173
 
-
 
174
/** Set stream buffer. */
-
 
175
void setvbuf(FILE *stream, void *buf, int mode, size_t size)
-
 
176
{
-
 
177
    stream->btype = mode;
-
 
178
    stream->buf = buf;
-
 
179
    stream->buf_size = size;
-
 
180
    stream->buf_head = stream->buf;
-
 
181
}
-
 
182
 
-
 
183
/** Allocate stream buffer. */
-
 
184
static int _fallocbuf(FILE *stream)
-
 
185
{
-
 
186
    assert(stream->buf == NULL);
-
 
187
 
-
 
188
    stream->buf = malloc(stream->buf_size);
-
 
189
    if (stream->buf == NULL) {
-
 
190
        errno = ENOMEM;
-
 
191
        return -1;
-
 
192
    }
-
 
193
 
-
 
194
    stream->buf_head = stream->buf;
-
 
195
    return 0;
-
 
196
}
-
 
197
 
159
/** Open a stream.
198
/** Open a stream.
160
 *
199
 *
161
 * @param path Path of the file to open.
200
 * @param path Path of the file to open.
162
 * @param mode Mode string, (r|w|a)[b|t][+].
201
 * @param mode Mode string, (r|w|a)[b|t][+].
163
 *
202
 *
Line 184... Line 223...
184
   
223
   
185
    stream->error = false;
224
    stream->error = false;
186
    stream->eof = false;
225
    stream->eof = false;
187
    stream->klog = false;
226
    stream->klog = false;
188
    stream->phone = -1;
227
    stream->phone = -1;
-
 
228
 
-
 
229
    /* FIXME: Should select buffering type based on what was opened. */
-
 
230
    setvbuf(stream, NULL, _IOFBF, BUFSIZ);
189
   
231
   
190
    list_append(&stream->link, &files);
232
    list_append(&stream->link, &files);
191
   
233
   
192
    return stream;
234
    return stream;
193
}
235
}
Line 204... Line 246...
204
    stream->fd = fd;
246
    stream->fd = fd;
205
    stream->error = false;
247
    stream->error = false;
206
    stream->eof = false;
248
    stream->eof = false;
207
    stream->klog = false;
249
    stream->klog = false;
208
    stream->phone = -1;
250
    stream->phone = -1;
-
 
251
 
-
 
252
    /* FIXME: Should select buffering type based on what was opened. */
-
 
253
    setvbuf(stream, NULL, _IOLBF, BUFSIZ);
209
   
254
   
210
    list_append(&stream->link, &files);
255
    list_append(&stream->link, &files);
211
   
256
   
212
    return stream;
257
    return stream;
213
}
258
}
Line 234... Line 279...
234
   
279
   
235
    stream->error = false;
280
    stream->error = false;
236
    stream->eof = false;
281
    stream->eof = false;
237
    stream->klog = false;
282
    stream->klog = false;
238
    stream->phone = -1;
283
    stream->phone = -1;
-
 
284
 
-
 
285
    /* FIXME: Should select buffering type based on what was opened. */
-
 
286
    setvbuf(stream, NULL, _IOLBF, BUFSIZ);
239
   
287
   
240
    list_append(&stream->link, &files);
288
    list_append(&stream->link, &files);
241
   
289
   
242
    return stream;
290
    return stream;
243
}
291
}
Line 281... Line 329...
281
 */
329
 */
282
size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
330
size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
283
{
331
{
284
    size_t left = size * nmemb;
332
    size_t left = size * nmemb;
285
    size_t done = 0;
333
    size_t done = 0;
-
 
334
 
-
 
335
    /* Make sure no data is pending write. */
-
 
336
    _fflushbuf(stream);
286
   
337
 
287
    while ((left > 0) && (!stream->error) && (!stream->eof)) {
338
    while ((left > 0) && (!stream->error) && (!stream->eof)) {
288
        ssize_t rd = read(stream->fd, buf + done, left);
339
        ssize_t rd = read(stream->fd, buf + done, left);
289
       
340
       
290
        if (rd < 0)
341
        if (rd < 0)
291
            stream->error = true;
342
            stream->error = true;
Line 298... Line 349...
298
    }
349
    }
299
   
350
   
300
    return (done / size);
351
    return (done / size);
301
}
352
}
302
 
353
 
303
/** Write to a stream.
-
 
304
 *
-
 
305
 * @param buf    Source buffer.
-
 
306
 * @param size   Size of each record.
-
 
307
 * @param nmemb  Number of records to write.
-
 
308
 * @param stream Pointer to the stream.
-
 
309
 *
-
 
310
 */
-
 
311
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
354
static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
312
{
355
{
313
    size_t left = size * nmemb;
356
    size_t left = size * nmemb;
314
    size_t done = 0;
357
    size_t done = 0;
315
   
358
   
316
    while ((left > 0) && (!stream->error)) {
359
    while ((left > 0) && (!stream->error)) {
Line 330... Line 373...
330
    }
373
    }
331
   
374
   
332
    return (done / size);
375
    return (done / size);
333
}
376
}
334
 
377
 
-
 
378
/** Drain stream buffer, do not sync stream. */
-
 
379
static void _fflushbuf(FILE *stream)
-
 
380
{
-
 
381
    size_t bytes_used;
-
 
382
 
-
 
383
    if (!stream->buf || stream->btype == _IONBF || stream->error)
-
 
384
        return;
-
 
385
 
-
 
386
    bytes_used = stream->buf_head - stream->buf;
-
 
387
    if (bytes_used == 0)
-
 
388
        return;
-
 
389
 
-
 
390
    (void) _fwrite(stream->buf, 1, bytes_used, stream);
-
 
391
    stream->buf_head = stream->buf;
-
 
392
}
-
 
393
 
-
 
394
/** Write to a stream.
-
 
395
 *
-
 
396
 * @param buf    Source buffer.
-
 
397
 * @param size   Size of each record.
-
 
398
 * @param nmemb  Number of records to write.
-
 
399
 * @param stream Pointer to the stream.
-
 
400
 *
-
 
401
 */
-
 
402
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
-
 
403
{
-
 
404
    uint8_t *data;
-
 
405
    size_t bytes_left;
-
 
406
    size_t now;
-
 
407
    size_t buf_free;
-
 
408
    size_t total_written;
-
 
409
    size_t i;
-
 
410
    uint8_t b;
-
 
411
    bool need_flush;
-
 
412
 
-
 
413
    /* If not buffered stream, write out directly. */
-
 
414
    if (stream->btype == _IONBF)
-
 
415
        return _fwrite(buf, size, nmemb, stream);
-
 
416
 
-
 
417
    /* Perform lazy allocation of stream buffer. */
-
 
418
    if (stream->buf == NULL) {
-
 
419
        if (_fallocbuf(stream) != 0)
-
 
420
            return 0; /* Errno set by _fallocbuf(). */
-
 
421
    }
-
 
422
 
-
 
423
    data = (uint8_t *) buf;
-
 
424
    bytes_left = size * nmemb;
-
 
425
    total_written = 0;
-
 
426
    need_flush = false;
-
 
427
 
-
 
428
    while (!stream->error && bytes_left > 0) {
-
 
429
 
-
 
430
        buf_free = stream->buf_size - (stream->buf_head - stream->buf);
-
 
431
        if (bytes_left > buf_free)
-
 
432
            now = buf_free;
-
 
433
        else
-
 
434
            now = bytes_left;
-
 
435
 
-
 
436
        for (i = 0; i < now; i++) {
-
 
437
            b = data[i];
-
 
438
            stream->buf_head[i] = b;
-
 
439
 
-
 
440
            if (b == '\n' && stream->btype == _IOLBF)
-
 
441
                need_flush = true;
-
 
442
        }
-
 
443
 
-
 
444
        buf += now;
-
 
445
        stream->buf_head += now;
-
 
446
        buf_free -= now;
-
 
447
        bytes_left -= now;
-
 
448
        total_written += now;
-
 
449
 
-
 
450
        if (buf_free == 0) {
-
 
451
            /* Only need to drain buffer. */
-
 
452
            _fflushbuf(stream);
-
 
453
            need_flush = false;
-
 
454
        }
-
 
455
    }
-
 
456
 
-
 
457
    if (need_flush)
-
 
458
        fflush(stream);
-
 
459
 
-
 
460
    return (total_written / size);
-
 
461
}
-
 
462
 
335
int fputc(wchar_t c, FILE *stream)
463
int fputc(wchar_t c, FILE *stream)
336
{
464
{
337
    char buf[STR_BOUNDS(1)];
465
    char buf[STR_BOUNDS(1)];
338
    size_t sz = 0;
466
    size_t sz = 0;
339
   
467
   
Line 403... Line 531...
403
    (void) fseek(stream, 0, SEEK_SET);
531
    (void) fseek(stream, 0, SEEK_SET);
404
}
532
}
405
 
533
 
406
int fflush(FILE *stream)
534
int fflush(FILE *stream)
407
{
535
{
-
 
536
    _fflushbuf(stream);
-
 
537
 
408
    if (stream->klog) {
538
    if (stream->klog) {
409
        klog_update();
539
        klog_update();
410
        return EOK;
540
        return EOK;
411
    }
541
    }
412
   
542