在C中确定文件类型

Jor*_*dan 3 c file-type file

我正在使用Linux机器上的C程序,该程序显示作为程序参数的文件的文件类型.程序需要确定文件是否是以下任何一个:目录,设备,(常规)文件,链接,套接字或fifo.我不确定如何确定文件类型.

到目前为止,这是我的代码(不多):

int
main(int argc, char **argv)
{
    if( argc == 1 )     /* default: current directory */
        puts("Directory");
    else
        while( --argc > 0 )
            determine_ftype(*++argv);

    return  0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

oua*_*uah 11

使用POSIX stat函数并读取函数返回st_mode的结构字段struct stat.

stat 功能:

http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html

结构struct stat类型:

http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

对于glibc的,你也可以阅读部分14.9.3 Testing the Type of a File的的glibc手册:

http://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html