]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/prepread.c
Addressed ticket #40 (non-standard errno values).
[pdclib] / functions / _PDCLIB / prepread.c
1 /* $Id$ */
2
3 /* _PDCLIB_prepread( struct _PDCLIB_file_t * )
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 #include <stdio.h>
10
11 #define _PDCLIB_GLUE_H _PDCLIB_GLUE_H
12 #include <_PDCLIB_glue.h>
13
14 int _PDCLIB_prepread( struct _PDCLIB_file_t * stream )
15 {
16     if ( ( stream->bufidx > stream->bufend ) ||
17          ( stream->status & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_ERRORFLAG | _PDCLIB_WIDESTREAM | _PDCLIB_EOFFLAG ) ) ||
18          ! ( stream->status & ( _PDCLIB_FREAD | _PDCLIB_FRW ) ) )
19     {
20         /* Function called on illegal (e.g. output) stream.
21            See comments on implementation-defined errno values in
22            <_PDCLIB_config.h>.
23         */
24         _PDCLIB_errno = _PDCLIB_ERROR;
25         stream->status |= _PDCLIB_ERRORFLAG;
26         return EOF;
27     }
28     stream->status |= _PDCLIB_FREAD | _PDCLIB_BYTESTREAM;
29     if ( ( stream->bufidx == stream->bufend ) && ( stream->ungetidx == 0 ) )
30     {
31         return _PDCLIB_fillbuffer( stream );
32     }
33     else
34     {
35         return 0;
36     }
37 }
38
39 #ifdef TEST
40 #include <_PDCLIB_test.h>
41
42 int main( void )
43 {
44     /* Testing covered by ftell.c */
45     return TEST_RESULTS;
46 }
47
48 #endif
49