(5) By Stephan Beal (stephan) on 2020-07-17 17:37:05 in reply to 4 updated by 5.1
> Those are the C library interfaces. Try the POSIX ones, without the leading “f”. I assume the Unix VFS uses the latter. `open()` and `read()` do the same on my system (Mint Linux 20): ```c #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { int e; size_t i; char buf[256]; int fd = open(".", O_RDONLY); printf("%d\n", fd); if (!fd<0) return -1; i = (ssize_t) read(fd, buf, sizeof(buf)); e = errno; printf("%llu %d %s\n", (unsigned long long)i, e, strerror(e)); close(fd); return 0; } ``` Output: ``` 3 18446744073709551615 21 Is a directory ``` Whether or not that's a standard behaviour, i don't know. It was a good 5 or 6 years ago that i had a piece of software simply open/fopen and read/fread a directory handle - perhaps that was specific to those C libs. No man pages i've found make any mention of it being an error to open/fopen a directory. [IEEE fopen() lists only one error case][ieee] which sounds relevant: [ieee]: https://pubs.opengroup.org/onlinepubs/9699919799/ ``` [EISDIR] [CX] [Option Start] The named file is a directory and mode requires write access. ``` strongly implying that opening a directory *is* acceptable in read-only mode. /shrugs shoulders
(5.1) By Stephan Beal (stephan) on 2020-07-17 17:43:51 edited from 5.0 in reply to 4 updated by 5.2 [link]
> Those are the C library interfaces. Try the POSIX ones, without the leading “f”. I assume the Unix VFS uses the latter. `open()` and `read()` do the same on my system (Mint Linux 20): ```c #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { int e; size_t i; char buf[256]; int fd = open(".", O_RDONLY); printf("%d\n", fd); if (!fd<0) return -1; i = (ssize_t) read(fd, buf, sizeof(buf)); e = errno; printf("%llu %d %s\n", (unsigned long long)i, e, strerror(e)); close(fd); return 0; } ``` Output: ``` 3 18446744073709551615 21 Is a directory ``` Whether or not that's a standard behaviour, i don't know. It was a good 5 or 6 years ago that i had a piece of software simply open/fopen and read/fread a directory handle - perhaps that was specific to those C libs. No man pages i've found make any mention of it being an error to open/fopen a directory. [IEEE fopen() lists only one error case][ieee] which sounds relevant: [ieee]: https://pubs.opengroup.org/onlinepubs/9699919799/ ``` [EISDIR] [CX] [Option Start] The named file is a directory and mode requires write access. ``` strongly implying that opening a directory *is* acceptable in read-only mode. Edit: in any case, the opening is succeeding, both with open() and fopen(), but the reading, both with read() and fread(), on my current system. [This StackOverflow answer](https://stackoverflow.com/a/24981747) says that the ability to read() a directory entry existed historically but was eventually removed. /shrugs shoulders
(5.2) By Stephan Beal (stephan) on 2020-07-17 17:48:20 edited from 5.1 in reply to 4 [link]
> Those are the C library interfaces. Try the POSIX ones, without the leading “f”. I assume the Unix VFS uses the latter. `open()` and `read()` do the same on my system (Mint Linux 20): ```c #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { int e; size_t i; char buf[256]; int fd = open(".", O_RDONLY); printf("%d\n", fd); if (!fd<0) return -1; i = (ssize_t) read(fd, buf, sizeof(buf)); e = errno; printf("%llu %d %s\n", (unsigned long long)i, e, strerror(e)); close(fd); return 0; } ``` Output: ``` 3 18446744073709551615 21 Is a directory ``` Whether or not that's a standard behaviour, i don't know. It was a good 5 or 6 years ago that i had a piece of software simply open/fopen and read/fread a directory handle - perhaps that was specific to those C libs. No man pages i've found make any mention of it being an error to open/fopen a directory. [IEEE fopen() lists only one error case][ieee] which sounds relevant: [ieee]: https://pubs.opengroup.org/onlinepubs/9699919799/ ``` [EISDIR] [CX] [Option Start] The named file is a directory and mode requires write access. ``` strongly implying that opening a directory *is* acceptable in read-only mode. Edit: in any case, the opening is succeeding, both with open() and fopen(), but the reading, both with read() and fread(), on my current system. [This StackOverflow answer](https://stackoverflow.com/a/24981747) says that the ability to read() a directory entry existed historically but was eventually removed. Edit: IBM's z/OS docs [say that fread() returns 0 when used to read a directory][zos], which differs from how Linux does it. Insofar as i can determine, this capability lives in the realm of implementation-defined behaviour. [zos]: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxbd00/fread.htm /shrugs shoulders