]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/fillbuffer.c
Addressed ticket #40 (non-standard errno values).
[pdclib] / platform / example_cygwin / 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->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 probably 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 #include <_PDCLIB_test.h>
70
71 int main( void )
72 {
73     /* Testing covered by ftell.c */
74     return TEST_RESULTS;
75 }
76
77 #endif
78