mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +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:
		| @@ -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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 peteee
					peteee