]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/fillbuffer.c
8da93ad345fb8a1ff0963eb28cb95d06f81ff1b4
[pdclib] / platform / example / functions / _PDCLIB / fillbuffer.c
1 /* $Id$ */
2
3 /* _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* This is an example implementation of _PDCLIB_fillbuffer() fit for
10    use with POSIX kernels.
11 */
12
13 #include <stdio.h>
14
15 #ifndef REGTEST
16 #include <_PDCLIB_glue.h>
17
18 #include </usr/include/errno.h>
19
20 typedef long ssize_t;
21 extern ssize_t read( int fd, void * buf, size_t count );
22
23 int _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream )
24 {
25     /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
26     ssize_t rc = read( stream->handle, stream->buffer, stream->bufsize );
27     if ( rc > 0 )
28     {
29         /* Reading successful. */
30         if ( ! ( stream->status & _PDCLIB_FBIN ) )
31         {
32             /* TODO: Text stream conversion here */
33         }
34         stream->pos.offset += rc;
35         stream->bufend = rc;
36         stream->bufidx = 0;
37         return 0;
38     }
39     if ( rc < 0 )
40     {
41         /* Reading error */
42         switch ( errno )
43         {
44             case EBADF:
45             case EFAULT:
46             case EINTR:
47             case EINVAL:
48             case EIO:
49                 _PDCLIB_errno = _PDCLIB_EIO;
50                 break;
51             default:
52                 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
53                 break;
54         }
55         stream->status |= _PDCLIB_ERRORFLAG;
56         return EOF;
57     }
58     /* End-of-File */
59     stream->status |= _PDCLIB_EOFFLAG;
60     return EOF;
61 }
62
63 #endif
64
65 #ifdef TEST
66 #include <_PDCLIB_test.h>
67
68 int main( void )
69 {
70     /* Testing covered by ftell.c */
71     return TEST_RESULTS;
72 }
73
74 #endif
75