Skip to content

Commit a85eaca

Browse files
authored
Merge pull request #1532 from strangepete/fstat-directory-test
file/open: check if directory
2 parents 83e8aab + ed63987 commit a85eaca

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/core/io.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#ifndef JANET_WINDOWS
3333
#include <fcntl.h>
34+
#include <sys/stat.h>
3435
#include <sys/wait.h>
3536
#include <unistd.h>
3637
#endif
@@ -164,6 +165,14 @@ JANET_CORE_FN(cfun_io_fopen,
164165
}
165166
FILE *f = fopen((const char *)fname, (const char *)fmode);
166167
if (f != NULL) {
168+
#ifndef JANET_WINDOWS
169+
struct stat st;
170+
fstat(fileno(f), &st);
171+
if (S_ISDIR(st.st_mode)) {
172+
fclose(f);
173+
janet_panicf("cannot open directory: %s", fname);
174+
}
175+
#endif
167176
size_t bufsize = janet_optsize(argv, argc, 2, BUFSIZ);
168177
if (bufsize != BUFSIZ) {
169178
int result = setvbuf(f, NULL, bufsize ? _IOFBF : _IONBF, bufsize);

0 commit comments

Comments
 (0)