Added AROS port of all shell applications:
[cake.git] / rom / dos / fgetc.c
blobfc9cf5547c11171ae3ad30e34dbdef7f7cbcaa59
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
8 #include <exec/memory.h>
9 #include <proto/exec.h>
11 #include <dos/stdio.h>
12 #include <dos/dosextens.h>
14 #include "dos_intern.h"
17 /*****************************************************************************
19 NAME */
20 #include <proto/dos.h>
22 AROS_LH1(LONG, FGetC,
24 /* SYNOPSIS */
25 AROS_LHA(BPTR, file, D1),
27 /* LOCATION */
28 struct DosLibrary *, DOSBase, 51, Dos)
30 /* FUNCTION
31 Get a character from a buffered file. Buffered I/O is more efficient
32 for small amounts of data but less for big chunks. You have to
33 use Flush() between buffered and non-buffered I/O or you'll
34 clutter your I/O stream.
36 INPUTS
37 file - filehandle
39 RESULT
40 The character read or EOF if the file ended or an error happened.
41 IoErr() gives additional information in that case.
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 IoErr(), Flush()
52 INTERNALS
54 *****************************************************************************/
56 AROS_LIBFUNC_INIT
58 /* Get pointer to filehandle */
59 struct FileHandle *fh = (struct FileHandle *)BADDR(file);
61 LONG size;
63 if (fh == NULL)
65 return EOF;
68 /* If the file is in write mode... */
69 if(fh->fh_Flags & FHF_WRITE)
71 /* write the buffer (in many pieces if the first one isn't enough). */
72 UBYTE *pos = fh->fh_Buf;
74 while(pos != fh->fh_Pos)
76 size = Write(file, pos, fh->fh_Pos - pos);
78 /* An error happened? Return it. */
79 if(size < 0)
81 return EOF;
84 pos += size;
87 /* Reinit filehandle. */
88 fh->fh_Flags &= ~FHF_WRITE;
89 fh->fh_Pos = fh->fh_End = fh->fh_Buf;
92 /* No normal characters left. */
93 if(fh->fh_Pos >= fh->fh_End)
95 /* Check for a pushed back EOF. */
96 if(fh->fh_Pos > fh->fh_End)
98 /* Reinit filehandle and return EOF. */
99 fh->fh_Pos = fh->fh_End;
100 SetIoErr(0);
102 return EOF;
105 /* Is there a buffer? */
106 if(fh->fh_Buf == NULL)
108 if (NULL == vbuf_alloc(fh, IOBUFSIZE, DOSBase))
110 return(EOF);
114 /* Fill the buffer. */
115 size = Read(file, fh->fh_Buf, fh->fh_Size);
117 /* Prepare filehandle for data. */
118 if(size <= 0)
119 size = 0;
121 fh->fh_Pos = fh->fh_Buf;
122 fh->fh_End = fh->fh_Buf + size;
124 /* No data read? Return EOF. */
125 if(size == 0)
127 return EOF;
131 /* All OK. Get data. */
132 return *fh->fh_Pos++;
134 AROS_LIBFUNC_EXIT
135 } /* FGetC */