diff --git a/os/format.c b/os/format.c index cdec7893f..8e5ccf014 100644 --- a/os/format.c +++ b/os/format.c @@ -1079,13 +1079,13 @@ static Int with_output_to(USES_REGS1) { return false; } if (IsApplTerm(tin) && (f = FunctorOfTerm(tin))) { - if(f == FunctorAtom || f == FunctorString || f == FunctorCodes1 || - f == FunctorCodes || f == FunctorChars1 || f == FunctorChars)) { - output_stream = Yap_OpenBufWriteStream(PASS_REGS1); - my_mem_stream = true; - - } - } else { + if (f == FunctorAtom || f == FunctorString || f == FunctorCodes1 || + f == FunctorCodes || f == FunctorChars1 || f == FunctorChars) { + output_stream = Yap_OpenBufWriteStream(PASS_REGS1); + my_mem_stream = true; + } + } + if (my_mem_stream){ /* needs to change LOCAL_c_output_stream for write */ output_stream = Yap_CheckStream(ARG1, Output_Stream_f, "format/3"); my_mem_stream = false; @@ -1121,15 +1121,17 @@ static Int format(Term tf, Term tas, Term tout USES_REGS) { return false; } yhandle_t hl = Yap_StartHandles(), yo = Yap_PushHandle(tout); - if (IsApplTerm(tout) && (f = FunctorOfTerm(tout)) && - (f == FunctorAtom || f == FunctorString1 || f == FunctorCodes1 || - f == FunctorCodes || f == FunctorChars1 || f == FunctorChars)) { + if (IsApplTerm(tout) && (f = FunctorOfTerm(tout))) { + if (f == FunctorAtom || f == FunctorString1 || f == FunctorCodes1 || + f == FunctorCodes || f == FunctorChars1 || f == FunctorChars){ output_stream = Yap_OpenBufWriteStream(PASS_REGS1); mem_stream = true; - } else { + } + if (!mem_stream) { /* needs to change LOCAL_c_output_stream for write */ output_stream = Yap_CheckStream(tout, Output_Stream_f, "format/3"); } + } if (output_stream == -1) { UNLOCK(GLOBAL_Stream[output_stream].streamlock); return false;