aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--FIXES5
-rw-r--r--main.c2
-rw-r--r--run.c29
3 files changed, 26 insertions, 10 deletions
diff --git a/FIXES b/FIXES
index f6eb98c..8e49fe9 100644
--- a/FIXES
+++ b/FIXES
@@ -25,6 +25,11 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987.
+December 8, 2021:
+ The error handling in closefile and closeall was mangled. Long
+ standing warnings had been made fatal and some fatal errors went
+ undetected. Thanks to Miguel Pineiro Jr. <mpj@pineiro.cc>.
+
Nov 03, 2021:
getline accesses uninitialized data after getrec()
returns 0 on EOF and leaves the contents of buf unchanged.
diff --git a/main.c b/main.c
index ee8b82b..986f1a3 100644
--- a/main.c
+++ b/main.c
@@ -22,7 +22,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
-const char *version = "version 20211103";
+const char *version = "version 20211208";
#define DEBUG
#include <stdio.h>
diff --git a/run.c b/run.c
index 6bff3e1..915ce72 100644
--- a/run.c
+++ b/run.c
@@ -1872,8 +1872,14 @@ Cell *closefile(Node **a, int n)
for (i = 0; i < nfiles; i++) {
if (!files[i].fname || strcmp(x->sval, files[i].fname) != 0)
continue;
- if (ferror(files[i].fp))
- FATAL("i/o error occurred on %s", files[i].fname);
+ fflush(files[i].fp);
+ if (ferror(files[i].fp)) {
+ if ((files[i].mode == GT && files[i].fp != stderr)
+ || files[i].mode == '|')
+ FATAL("write error on %s", files[i].fname);
+ else
+ WARNING("i/o error occurred on %s", files[i].fname);
+ }
if (files[i].fp == stdin || files[i].fp == stdout ||
files[i].fp == stderr)
stat = freopen("/dev/null", "r+", files[i].fp) == NULL;
@@ -1882,7 +1888,7 @@ Cell *closefile(Node **a, int n)
else
stat = fclose(files[i].fp) == EOF;
if (stat)
- FATAL("i/o error occurred closing %s", files[i].fname);
+ WARNING("i/o error occurred closing %s", files[i].fname);
if (i > 2) /* don't do /dev/std... */
xfree(files[i].fname);
files[i].fname = NULL; /* watch out for ref thru this */
@@ -1903,18 +1909,23 @@ void closeall(void)
for (i = 0; i < nfiles; i++) {
if (! files[i].fp)
continue;
- if (ferror(files[i].fp))
- FATAL( "i/o error occurred on %s", files[i].fname );
- if (files[i].fp == stdin)
+ fflush(files[i].fp);
+ if (ferror(files[i].fp)) {
+ if ((files[i].mode == GT && files[i].fp != stderr)
+ || files[i].mode == '|')
+ FATAL("write error on %s", files[i].fname);
+ else
+ WARNING("i/o error occurred on %s", files[i].fname);
+ }
+ if (files[i].fp == stdin || files[i].fp == stdout ||
+ files[i].fp == stderr)
continue;
if (files[i].mode == '|' || files[i].mode == LE)
stat = pclose(files[i].fp) == -1;
- else if (files[i].fp == stdout || files[i].fp == stderr)
- stat = fflush(files[i].fp) == EOF;
else
stat = fclose(files[i].fp) == EOF;
if (stat)
- FATAL( "i/o error occurred while closing %s", files[i].fname );
+ WARNING("i/o error occurred while closing %s", files[i].fname);
}
}