deadpixi / sam

An updated version of the sam text editor.
Other
436 stars 47 forks source link

fgetwc return wint_t, not wchar_t #118

Open olehjalmar opened 3 years ago

olehjalmar commented 3 years ago

sam compiles out of the box on Cygwin, but hangs when opening files. I traced the problem to the test on WEOF following the assignment of fgetwc to a wchar_t. The following patch fixes the problem, and believe the original code is actually wrong on all systems, not only on Cygwin.

diff --git a/sam/cmd.c b/sam/cmd.c
index c69201e..12fb9d0 100644
--- a/sam/cmd.c
+++ b/sam/cmd.c
@@ -109,8 +109,9 @@ inputc(void)
             terminp = termoutp = termline;
     } else{
         int olderr = errno;
-        r = fgetwc(stdin);
-        if (r == WEOF && errno == EILSEQ){
+        wint_t err;
+        r = err = fgetwc(stdin);
+        if (err = WEOF && errno == EILSEQ){
             clearerr(stdin);
             fflush(stdin);
             fgetc(stdin);
diff --git a/sam/io.c b/sam/io.c
index 080cdfd..46f568c 100644
--- a/sam/io.c
+++ b/sam/io.c
@@ -90,8 +90,9 @@ readio(File *f, bool *nulls, bool setdate)

     wchar_t buf[2] = {0};
     while (true){
-        buf[0] = fgetwc(io);
-        if (buf[0] == WEOF){
+        wint_t err;
+        buf[0] = err = fgetwc(io);
+        if (err == WEOF){
             if (errno == EILSEQ){
                 clearerr(io);
                 fflush(io);
deadpixi commented 3 years ago

Thanks for this. Could you put the patch into a PR please?