shithub: trueawk

Download patch

ref: ed43b8f6bf63acdacaaf6788e2ba5f0ec202147c
parent: 5c261eb19a5e92bb6d68e818cf575524067c1e6a
author: ozan yigit <ozan.yigit@gmail.com>
date: Sat Jan 4 15:20:04 EST 2025

fix issue #249 don't try to read or write a directory

--- a/run.c
+++ b/run.c
@@ -35,6 +35,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 #include "awk.h"
 #include "awkgram.tab.h"
@@ -2258,9 +2259,11 @@
 	size_t i;
 	int m;
 	FILE *fp = NULL;
+	struct stat sbuf;
 
 	if (*s == '\0')
 		FATAL("null file name in print or getline");
+
 	for (i = 0; i < nfiles; i++)
 		if (files[i].fname && strcmp(s, files[i].fname) == 0 &&
 		    (a == files[i].mode || (a==APPEND && files[i].mode==GT) ||
@@ -2271,7 +2274,6 @@
 		}
 	if (a == FFLUSH)	/* didn't find it, so don't create it! */
 		return NULL;
-
 	for (i = 0; i < nfiles; i++)
 		if (files[i].fp == NULL)
 			break;
@@ -2285,7 +2287,14 @@
 		nfiles = nnf;
 		files = nf;
 	}
+
 	fflush(stdout);	/* force a semblance of order */
+
+	/* don't try to read or write a directory */
+	if (a == LT || a == GT || a == APPEND)
+		if (stat(s, &sbuf) == 0 && S_ISDIR(sbuf.st_mode))
+				return NULL;
+
 	m = a;
 	if (a == GT) {
 		fp = fopen(s, "w");
--