]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/fillbuffer.c
ecfb96859bcc668b63bfe866d6aca7b4017e5ba8
[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             case EBADF:
44             case EFAULT:
45             case EINTR:
46             case EINVAL:
47             case EIO:
48                 _PDCLIB_errno = _PDCLIB_EIO;
49                 break;
50             default:
51                 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
52                 break;
53         }
54         stream->status |= _PDCLIB_ERRORFLAG;
55         return EOF;
56     }
57     /* End-of-File */
58     stream->status |= _PDCLIB_EOFFLAG;
59     return EOF;
60 }
61
62 #endif
63
64 #ifdef TEST
65 #include <_PDCLIB_test.h>
66
67 int main( void )
68 {
69     /* Testing covered by ftell.c */
70     return TEST_RESULTS;
71 }
72
73 #endif
74