]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/fillbuffer.c
Addressed ticket #40 (non-standard errno values).
[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             /* See comments on implementation-defined errno values in
45                <_PDCLIB_config.h>.
46             */
47             case EBADF:
48             case EFAULT:
49             case EINTR:
50             case EINVAL:
51             case EIO:
52                 _PDCLIB_errno = _PDCLIB_ERROR;
53                 break;
54             default:
55                 /* This should probably be something like EUNKNOWN. */
56                 _PDCLIB_errno = _PDCLIB_ERROR;
57                 break;
58         }
59         stream->status |= _PDCLIB_ERRORFLAG;
60         return EOF;
61     }
62     /* End-of-File */
63     stream->status |= _PDCLIB_EOFFLAG;
64     return EOF;
65 }
66
67 #endif
68
69 #ifdef TEST
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
79