mirror of
https://github.com/janet-lang/janet
synced 2025-01-08 06:30:28 +00:00
Merge pull request #1532 from strangepete/fstat-directory-test
file/open: check if directory
This commit is contained in:
commit
a85eacadda
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#ifndef JANET_WINDOWS
|
#ifndef JANET_WINDOWS
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
@ -164,6 +165,14 @@ JANET_CORE_FN(cfun_io_fopen,
|
|||||||
}
|
}
|
||||||
FILE *f = fopen((const char *)fname, (const char *)fmode);
|
FILE *f = fopen((const char *)fname, (const char *)fmode);
|
||||||
if (f != NULL) {
|
if (f != NULL) {
|
||||||
|
#ifndef JANET_WINDOWS
|
||||||
|
struct stat st;
|
||||||
|
fstat(fileno(f), &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);
|
size_t bufsize = janet_optsize(argv, argc, 2, BUFSIZ);
|
||||||
if (bufsize != BUFSIZ) {
|
if (bufsize != BUFSIZ) {
|
||||||
int result = setvbuf(f, NULL, bufsize ? _IOFBF : _IONBF, bufsize);
|
int result = setvbuf(f, NULL, bufsize ? _IOFBF : _IONBF, bufsize);
|
||||||
|
Loading…
Reference in New Issue
Block a user