在C中确定文件类型
我正在使用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; }
谢谢!
使用POSIX stat
函数并读取函数返回的结构struct stat
的st_mode
字段。
stat
function:
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
手册14.9.3 Testing the Type of a File
的部分:
http://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html