$NetBSD: patch-ac,v 1.3 2019/09/02 21:41:40 nia Exp $

--- sys/bsd/fileio.c.orig	1988-07-03 17:50:24.000000000 +0000
+++ sys/bsd/fileio.c
@@ -2,15 +2,25 @@
  *		bsd (4.2, others?), Sun (3.2, ?) and Ultrix-32 (?) file I/O.
  */
 #include	"def.h"
+#include <unistd.h>    /* refling added this per compiler */
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 static	FILE	*ffp;
-extern	char	*getenv(), *strncpy();
 char	*adjustname();
 
 /*
  * Open a file for reading.
  */
 ffropen(fn) char *fn; {
+	// prevent opening non-regular files - refling
+	struct stat st;
+	if (fn == NULL || 0 == strlen(fn) || (0 == stat(fn, &st) && ((st.st_mode & S_IFMT) != S_IFREG))) {
+		ewprintf("not a regular file");
+		return(FIOERR);
+	}
 	if ((ffp=fopen(fn, "r")) == NULL)
 		return (FIOFNF);
 	return (FIOSUC);
@@ -22,6 +32,12 @@ ffropen(fn) char *fn; {
  * FALSE on error (cannot create).
  */
 ffwopen(fn) char *fn; {
+	// prevent opening non-regular files - refling
+	struct stat st;
+	if (fn == NULL || 0 == strlen(fn) || (0 == stat(fn, &st) && ((st.st_mode & S_IFMT) != S_IFREG))) {
+		ewprintf("not a regular file");
+		return(FIOERR);
+	}
 	if ((ffp=fopen(fn, "w")) == NULL) {
 		ewprintf("Cannot open file for writing");
 		return (FIOERR);
@@ -276,7 +292,7 @@ char *suffix;
 	if ((file = getenv("HOME")) == NULL) goto notfound;
 	if (strlen(file)+7 >= NFILEN - 1) goto notfound;
 	(VOID) strcpy(home, file);
-	(VOID) strcat(home, "/.mg");
+	(VOID) strcat(home, "/.mgrc");
 	if (suffix != NULL) {
 		(VOID) strcat(home, "-");
 		(VOID) strcat(home, suffix);
@@ -295,6 +311,18 @@ notfound:
 	if (access(file, F_OK ) == 0) return file;
 #endif
 
+
+/* refling - try to open a file name given by MGRC env var */
+	if ((file = getenv("MGRC")) == NULL) return NULL;
+	if (strlen(file)+7 >= NFILEN - 1) goto notfound;
+	(VOID) strcpy(home, file);
+	if (suffix != NULL) {
+		(VOID) strcat(home, "-");
+		(VOID) strcat(home, suffix);
+	}
+	if (access(home, F_OK ) == 0) return home;
+
+
 	return NULL;
 }
 #endif
@@ -318,7 +346,8 @@ char *frname, *toname;
 	execve("cp", eargv, (char **)NULL);
 	_exit(1);	/* shouldn't happen */
     }
-    while(wait(&status) != pid) {}
+/*    while(wait(&status) != pid) {}   refling, was this */
+    while(wait((int *)&status) != pid) {}
     return status.w_status == 0;
 }
 
