Using `read` system call on a directory

后端 未结 2 533
猫巷女王i
猫巷女王i 2021-01-19 22:36

I was looking at an example in K&R 2 (8.6 Example - Listing Directories). It is a stripped down version of Linux command ls or Windows\' dir. T

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 23:17

    In fact Linux dosn't allow read for directories. See man page and search for errno EISDIR. You will find

    The read() and pread() functions shall fail if ...

    The fildes argument refers to a directory and the implementation does not allow the directory to be read using read() or pread(). The readdir() function should be used instead.

    . Other UNIXes allow it nevertheless.

提交回复
热议问题