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