From 9f0f015c4daf7d3eacb21c19b4f3912cac56e68e Mon Sep 17 00:00:00 2001 From: Myrrh Periwinkle Date: Fri, 13 Jun 2025 10:53:04 +0700 Subject: [PATCH] libDtWidget: Do not mix wide and narrow operations on the same stream The current behavior is not standards conformant, and will cause the format operation to result in empty text as glibc fails any calls to getwc against a narrow stream with WEOF. Additionally, narrow write operations against a wide stream appears to bypass the buffer, causing incorrect formatting results. Fix this by changing all write operations against the reformatted output file stream to be wide operations, and reopening the temporary input file before performing any wide read operations. --- cde/lib/DtWidget/Editor.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cde/lib/DtWidget/Editor.c b/cde/lib/DtWidget/Editor.c index 4edb0df76..9d6c750b4 100644 --- a/cde/lib/DtWidget/Editor.c +++ b/cde/lib/DtWidget/Editor.c @@ -5776,7 +5776,7 @@ Center (FormatData *data, if (! haveword) { - putc ('\n', data->pAdj->outfp); /* "eat" any whitespace */ + putwc ('\n', data->pAdj->outfp); /* "eat" any whitespace */ data->centerstartpara = True; /* expect new paragraph */ } @@ -5823,7 +5823,7 @@ Center (FormatData *data, { putwc(outline[i], data->pAdj->outfp); } - putc('\n', data->pAdj->outfp); + putwc('\n', data->pAdj->outfp); } } /* else */ @@ -5972,7 +5972,7 @@ Fill (FormatData *data, { data->inlinenum--; /* don't count empty lines */ Dump (data, True); /* force end paragraph */ - fputc ('\n', data->pAdj->outfp); /* put this empty line */ + putwc ('\n', data->pAdj->outfp); /* put this empty line */ data->inlinenum = 0; /* start new paragraph */ } else /* have text on line */ @@ -6246,11 +6246,11 @@ PrintIndent (FormatData *data, { while (indent >= data->tabsize) { - putc ('\t', data->pAdj->outfp); + putwc ('\t', data->pAdj->outfp); indent -= data->tabsize; } } - fprintf (data->pAdj->outfp, "%*s", indent, "");/*[remaining] blanks */ + fwprintf (data->pAdj->outfp, L"%*s", indent, "");/*[remaining] blanks */ } } /* PrintIndent */ @@ -6963,7 +6963,7 @@ DoAdjust( */ (void)tmpnam(tempName1); (void)tmpnam(tempName2); - if ((adjRec.infp = fopen(tempName1, "w+")) != (FILE *)NULL) { + if ((adjRec.infp = fopen(tempName1, "w")) != (FILE *)NULL) { /* * Successfully opened the first temporary file @@ -6979,8 +6979,7 @@ DoAdjust( fixLeftMarginAndNewlines( editor, adjRec.infp, leftMargin, rightMargin, (int)start, (int)end ); - fflush(adjRec.infp); - rewind(adjRec.infp); + freopen(NULL, "r", adjRec.infp); FormatText(&adjRec);