1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-07 06:10:25 +00:00

file/open: check if directory

Adds fstat() directory test after fopen(), which can return non-NULL when passed a directory name on Linux
This commit is contained in:
peteee 2024-12-13 00:20:44 -05:00 committed by GitHub
parent 83e8aab289
commit ff173047f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,6 +31,7 @@
#ifndef JANET_WINDOWS
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
@ -164,6 +165,14 @@ JANET_CORE_FN(cfun_io_fopen,
}
FILE *f = fopen((const char *)fname, (const char *)fmode);
if (f != NULL) {
#ifndef JANET_WINDOWS
struct stat st;
fstat(f->_fileno, &st);
if (S_ISDIR(st.st_mode)) {
fclose(f);
janet_panicf("cannot open directory: %s", fname);
}
#endif
size_t bufsize = janet_optsize(argv, argc, 2, BUFSIZ);
if (bufsize != BUFSIZ) {
int result = setvbuf(f, NULL, bufsize ? _IOFBF : _IONBF, bufsize);