What is `S_ISREG()`, and what does it do?

前端 未结 3 1751
时光说笑
时光说笑 2021-02-05 08:30

I came across the macro S_ISREG() in a C program that retrieves file attributes. Unfortunately, there isn\'t any basic information about this macro online. There ar

3条回答
  •  無奈伤痛
    2021-02-05 09:07

    The POSIX standard which defines S_ISREG is in fact online.

    http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html

    Quote:

    The following macros shall be provided to test whether a file is of the specified type. The value m supplied to the macros is the value of st_mode from a stat structure. The macro shall evaluate to a non-zero value if the test is true; 0 if the test is false.

    [...]

    S_ISFIFO(m)

       Test for a pipe or FIFO special file.

    S_ISREG(m)

       Test for a regular file.

    S_ISLNK(m)

       Test for a symbolic link.

    [...]

    A typical way to use S_ISREG is to first call the stat function to fill a struct stat object with information about a file. Then the value of the st_mode member of this structure, an integer type, can be tested with this macro.

    In addition the standard, there are man pages from various systems online, as well as tutorials about programming with stat. The Wikipedia has a page on stat, with an apparently complete code example. Though it doesn't feature S_ISREG, that can easily be worked in.

提交回复
热议问题