]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/fillbuffer.c
Comment cleanups.
[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 #include <_PDCLIB_glue.h>
15
16 #include </usr/include/errno.h>
17
18 typedef long ssize_t;
19 extern ssize_t read( int fd, void * buf, size_t count );
20
21 int _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream )
22 {
23     /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
24     ssize_t rc = read( stream->handle, stream->buffer, stream->bufsize );
25     if ( rc > 0 )
26     {
27         /* Reading successful. */
28         if ( ! ( stream->status & _PDCLIB_FBIN ) )
29         {
30             /* TODO: Text stream conversion here */
31         }
32         stream->pos.offset += rc;
33         stream->bufend = rc;
34         stream->bufidx = 0;
35         return 0;
36     }
37     if ( rc < 0 )
38     {
39         /* Reading error */
40         switch ( errno )
41         {
42             /* See comments on implementation-defined errno values in
43                <_PDCLIB_config.h>.
44             */
45             case EBADF:
46             case EFAULT:
47             case EINTR:
48             case EINVAL:
49             case EIO:
50                 _PDCLIB_errno = _PDCLIB_ERROR;
51                 break;
52             default:
53                 /* This should be something like EUNKNOWN. */
54                 _PDCLIB_errno = _PDCLIB_ERROR;
55                 break;
56         }
57         stream->status |= _PDCLIB_ERRORFLAG;
58         return EOF;
59     }
60     /* End-of-File */
61     stream->status |= _PDCLIB_EOFFLAG;
62     return EOF;
63 }
64
65 #endif
66
67 #ifdef TEST
68 #include <_PDCLIB_test.h>
69
70 int main( void )
71 {
72     /* Testing covered by ftell.c */
73     return TEST_RESULTS;
74 }
75
76 #endif
77