From 6ec92ac5c0708786d2e904d9e41263f534147f8b Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sat, 19 Dec 2009 19:57:20 -0200 Subject: [PATCH] handle correctly file rewind in the presence of ungetc (obs from Jose Santos). --- C/iopreds.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/C/iopreds.c b/C/iopreds.c index da4a75f97..7e93e55bc 100755 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -3924,9 +3924,15 @@ static Int #if HAVE_FGETPOS fpos_t rpos; #endif + int ungetc_oldc = 0; + int had_ungetc = FALSE; /* two cases where we can seek: memory and console */ if (seekable) { + if (Stream[inp_stream].stream_getc == PlUnGetc) { + had_ungetc = TRUE; + ungetc_oldc = Stream[inp_stream].och; + } if (Stream[inp_stream].status & InMemory_Stream_f) { cpos = Stream[inp_stream].u.mem_string.pos; } else { @@ -3946,6 +3952,10 @@ static Int if (Yap_Error_TYPE != YAP_NO_ERROR && seekable) { H = old_H; Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable); + if (had_ungetc) { + Stream[inp_stream].stream_getc = PlUnGetc; + Stream[inp_stream].och = ungetc_oldc; + } if (Stream[inp_stream].status & InMemory_Stream_f) { Stream[inp_stream].u.mem_string.pos = cpos; } else {