dos.library: Reverted 49008, mixing buffered and unbuffered reads causes wrong data...
[AROS.git] / rom / dos / ungetc.c
blob885ad183221910ff60093fcb98668f0cc8e84625
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
8 #include <aros/debug.h>
10 #include <proto/exec.h>
11 #include "dos_intern.h"
13 #ifndef EOF
14 #define EOF -1
15 #endif
17 /*****************************************************************************
19 NAME */
20 #include <proto/dos.h>
22 AROS_LH2(LONG, UnGetC,
24 /* SYNOPSIS */
25 AROS_LHA(BPTR, file, D1),
26 AROS_LHA(LONG, character, D2),
28 /* LOCATION */
30 struct DosLibrary *, DOSBase, 53, Dos)
32 /* FUNCTION
33 Push a character back into a read filehandle. If you've read
34 a character from that file you may always push at least 1 character
35 back. UnGetC(file,-1) ungets the last character read. This also
36 works for EOF.
38 INPUTS
39 file - Filehandle you've read from.
40 character - Character to push back or EOF.
42 RESULT
43 !=0 if all went well, 0 if the character couldn't be pushed back.
44 IoErr() gives additional information in that case.
46 NOTES
48 EXAMPLE
50 BUGS
52 SEE ALSO
53 FGetC(), IoErr()
55 INTERNALS
57 *****************************************************************************/
59 AROS_LIBFUNC_INIT
61 struct Process *me = (struct Process *)FindTask(NULL);
62 SIPTR *result;
64 ASSERT_VALID_PROCESS(me);
66 result=&me->pr_Result2;
68 /* Get pointer to filehandle */
69 struct FileHandle *fh=(struct FileHandle *)BADDR(file);
71 /* If the file is in write mode there was nothing read recently */
72 if(fh->fh_Flags&FHF_WRITE)
74 *result=ERROR_SEEK_ERROR;
75 return 0;
78 /* Unget EOF character if the last character read was an EOF */
79 if(character==EOF&&fh->fh_End==0)
81 fh->fh_Pos++;
82 return EOF;
85 /* Test if I may unget a character on this file */
86 if(fh->fh_Pos==0)
88 *result=ERROR_SEEK_ERROR;
89 return 0;
92 /* OK. Unget character and return. */
93 fh->fh_Pos--;
94 if(character!=EOF)
95 ((UBYTE *)BADDR(fh->fh_Buf))[fh->fh_Pos]=character;
96 return character?character:1;
97 AROS_LIBFUNC_EXIT
98 } /* UnGetC */