Subject: Re: bin/18785: config can read directories
To: None <gnats-bugs@gnats.netbsd.org>
From: Julio Merino <jmmv@menta.net>
List: netbsd-bugs
Date: 10/23/2002 22:57:00
This is an alternative patch which uses stat() on files to determine
their types. Don't know if this approach is better (at least, it seems
to me ;-).

Index: scan.l
===================================================================
RCS file: /cvsroot/syssrc/usr.sbin/config/scan.l,v
retrieving revision 1.33
diff -u -u -r1.33 scan.l
--- scan.l	2002/09/06 13:24:18	1.33
+++ scan.l	2002/10/23 20:51:52
@@ -46,6 +46,7 @@
  */
 
 #include <sys/param.h>
+#include <sys/stat.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -195,12 +196,21 @@
 int
 firstfile(const char *fname)
 {
-
-	if ((yyin = fopen(fname, "r")) == NULL)
+	struct stat sb;
+	if (stat(fname, &sb) == -1)
+		return (-1);
+	switch (sb.st_mode & S_IFMT) {
+	case S_IFLNK:
+	case S_IFREG:
+		if ((yyin = fopen(fname, "r")) == NULL)
+			return (-1);
+		yyfile = conffile = fname;
+		yyline = 1;
+		return (0);
+	default:
+		errno = EFTYPE; /* XXX */
 		return (-1);
-	yyfile = conffile = fname;
-	yyline = 1;
-	return (0);
+	}
 }
 
 /*

-- 
Julio Merino (http://jmmv.dyndns.org/) <jmmv@menta.net>