$NetBSD: patch-ah 00/05/31 16:39:51 refling $

--- main.c.orig	Sun Jul  3 10:49:25 1988
+++ main.c	Wed May 31 16:39:32 2000
@@ -6,6 +6,7 @@
 #include	"macro.h"
 #endif
 
+static  char *idstr = "mg2a v. 1.11 - refling";
 int	thisflag;			/* Flags, this command		*/
 int	lastflag;			/* Flags, last command		*/
 int	curgoal;			/* Goal column			*/
@@ -20,6 +21,35 @@
 
 static VOID	edinit();
 
+// refling all the next few lines....
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+
+char *getrc(char *progname, char *tmpr)
+{
+char *p, *e;
+int gotit = 0;
+char tmp[1000];
+struct stat statb;
+if (NULL == strrchr(progname, '/')) {
+   p = (char *)strcat(getenv("PATH"), ":");
+   while (NULL != (e = (char *)strchr(p, ':')))
+     {*e = 0;
+     if (!stat(strcat(strcpy(tmp, p), "/mg.rc"), &statb)) return(strcpy(tmpr, tmp));
+     p = ++e;}}
+else {
+   strcpy(tmp, progname);
+   *(char *)strrchr(tmp, '/') = 0;
+   strcat(tmp, "/mg.rc");
+   if (!stat(tmp, &statb)) return(strcpy(tmpr, tmp));}
+return(NULL);
+}
+
+// refling end.........
+
+
 VOID
 main(argc, argv)
 int  argc;
@@ -27,11 +57,21 @@
 {
 #ifndef NO_STARTUP
 	char	*startupfile();
+	char	*getrc();   // refling
+	char	tmppp[200];  // refling
 #endif
 	char	*cp;
 	VOID	vtinit(), makename(), eerase();
 	BUFFER	*findbuffer();
 
+	// cheap attempt to avoid starting up if illegal file - refling
+	struct stat st;
+	if (argc > 1 && 0 == stat(argv[1], &st))
+	if ((st.st_mode & S_IFMT) != S_IFREG) {
+		fprintf(stderr, "%s is not a regular file (0x%x)\n", argv[1], st.st_mode & S_IFMT);
+		exit(1);
+	}
+
 #ifdef SYSINIT
 	SYSINIT;				/* system dependent.	*/
 #endif
@@ -47,7 +87,9 @@
 	 */
 	update();
 #ifndef NO_STARTUP				/* User startup file.	*/
-	if ((cp = startupfile((char *)NULL)) != NULL)
+//	if ((cp = startupfile((char *)NULL)) != NULL)
+//		(VOID) load(cp);
+	if ((cp = getrc(argv[0], tmppp)) != NULL)  // refling
 		(VOID) load(cp);
 #endif
 	while (--argc > 0) {
