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