ad7b3b
diff --git a/lib/isc/include/isc/stdio.h b/lib/isc/include/isc/stdio.h
ad7b3b
index 1f44b5a..a3625f9 100644
ad7b3b
--- a/lib/isc/include/isc/stdio.h
ad7b3b
+++ b/lib/isc/include/isc/stdio.h
ad7b3b
@@ -69,6 +69,9 @@ isc_stdio_sync(FILE *f);
ad7b3b
  * direct counterpart in the stdio library.
ad7b3b
  */
ad7b3b
 
ad7b3b
+isc_result_t
ad7b3b
+isc_stdio_fgetc(FILE *f, int *ret);
ad7b3b
+
ad7b3b
 ISC_LANG_ENDDECLS
ad7b3b
 
ad7b3b
 #endif /* ISC_STDIO_H */
ad7b3b
diff --git a/lib/isc/lex.c b/lib/isc/lex.c
ad7b3b
index a8955bc..fc6103b 100644
ad7b3b
--- a/lib/isc/lex.c
ad7b3b
+++ b/lib/isc/lex.c
ad7b3b
@@ -434,17 +434,14 @@ isc_lex_gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *tokenp) {
Adam Tkac 3475d4
 			if (source->is_file) {
Adam Tkac 3475d4
 				stream = source->input;
Adam Tkac 3475d4
 
Adam Tkac 3475d4
-#if defined(HAVE_FLOCKFILE) && defined(HAVE_GETCUNLOCKED)
Adam Tkac 3475d4
-				c = getc_unlocked(stream);
Adam Tkac 3475d4
-#else
Adam Tkac 3475d4
-				c = getc(stream);
Adam Tkac 3475d4
-#endif
Adam Tkac 3475d4
-				if (c == EOF) {
Adam Tkac 3475d4
-					if (ferror(stream)) {
Adam Tkac 3475d4
-						source->result = ISC_R_IOERROR;
Adam Tkac 3475d4
-						result = source->result;
Adam Tkac 3475d4
+				result = isc_stdio_fgetc(stream, &c);
Adam Tkac 3475d4
+
Adam Tkac 3475d4
+				if (result != ISC_R_SUCCESS) {
Adam Tkac 3475d4
+					if (result != ISC_R_EOF) {
Adam Tkac 3475d4
+						source->result = result;
Adam Tkac 3475d4
 						goto done;
Adam Tkac 3475d4
 					}
Adam Tkac 3475d4
+
ad7b3b
 					source->at_eof = true;
Adam Tkac 3475d4
 				}
Adam Tkac 3475d4
 			} else {
ad7b3b
diff --git a/lib/isc/unix/errno2result.c b/lib/isc/unix/errno2result.c
ad7b3b
index 2f12bcc..5bfd648 100644
ad7b3b
--- a/lib/isc/unix/errno2result.c
ad7b3b
+++ b/lib/isc/unix/errno2result.c
ad7b3b
@@ -40,6 +40,7 @@ isc___errno2result(int posixerrno, bool dolog,
Adam Tkac 3475d4
 	case EINVAL:		/* XXX sometimes this is not for files */
Adam Tkac 3475d4
 	case ENAMETOOLONG:
Adam Tkac 3475d4
 	case EBADF:
Adam Tkac 3475d4
+	case EISDIR:
Adam Tkac 3475d4
 		return (ISC_R_INVALIDFILE);
Adam Tkac 3475d4
 	case ENOENT:
Adam Tkac 3475d4
 		return (ISC_R_FILENOTFOUND);
ad7b3b
diff --git a/lib/isc/unix/stdio.c b/lib/isc/unix/stdio.c
ad7b3b
index e60fa65..77f0b13 100644
ad7b3b
--- a/lib/isc/unix/stdio.c
ad7b3b
+++ b/lib/isc/unix/stdio.c
ad7b3b
@@ -149,3 +149,22 @@ isc_stdio_sync(FILE *f) {
Adam Tkac 3475d4
 		return (isc__errno2result(errno));
Adam Tkac 3475d4
 }
Adam Tkac 3475d4
 
Adam Tkac 3475d4
+isc_result_t
Adam Tkac 3475d4
+isc_stdio_fgetc(FILE *f, int *ret) {
Adam Tkac 3475d4
+	int r;
Adam Tkac 3475d4
+	isc_result_t result = ISC_R_SUCCESS;
Adam Tkac 3475d4
+
Adam Tkac 3475d4
+#if defined(HAVE_FLOCKFILE) && defined(HAVE_GETCUNLOCKED)
Adam Tkac 3475d4
+	r = fgetc_unlocked(f);
Adam Tkac 3475d4
+#else
Adam Tkac 3475d4
+	r = fgets(f);
Adam Tkac 3475d4
+#endif
Adam Tkac 3475d4
+
Adam Tkac 3475d4
+	if (r == EOF)
Adam Tkac 3475d4
+		result = ferror(f) ? isc__errno2result(errno) : ISC_R_EOF;
Adam Tkac 3475d4
+
Adam Tkac 3475d4
+	*ret = r;
Adam Tkac 3475d4
+
Adam Tkac 3475d4
+	return result;
Adam Tkac 3475d4
+}
Adam Tkac 3475d4
+