《UNIX环境高级编程》笔记6--文件类型

来源:互联网 发布:手机淘宝开店一件代发 编辑:程序博客网 时间:2024/05/20 14:25

UNIX文件类型主要包含如下几种:

1.普通文件(regular file)

2.目录文件(directory file)

3.块特殊文件(block special file),这种文件提供对设备(例如硬盘)带缓冲的访问,每次访问以固定长度为单位进行。

4.字符特殊文件(character special file),这种文件提供对设备不带缓冲的访问,每次访问长度可变。

5.FIFO文件,命名管道(named pipe),用于进程间通信。

6.socket文件,用于进程间的网络通信,也可以在同一台机器上进行进程间通信。

7.符号链接文件(symbolic link),指向另外一个文件。


文件类型信息存放在stat结构体的st_mode成员中。stat结构体的声明如下:

[cpp] view plain copy
  1. struct stat {  
  2.   
  3.         mode_t     st_mode;       //文件对应的模式,文件,目录等  
  4.   
  5.         ino_t      st_ino;       //inode节点号  
  6.   
  7.         dev_t      st_dev;        //设备号码  
  8.   
  9.         dev_t      st_rdev;       //特殊设备号码  
  10.   
  11.         nlink_t    st_nlink;      //文件的连接数  
  12.   
  13.         uid_t      st_uid;        //文件所有者  
  14.   
  15.         gid_t      st_gid;        //文件所有者对应的组  
  16.   
  17.         off_t      st_size;       //普通文件,对应的文件字节数  
  18.   
  19.         time_t     st_atime;      //文件最后被访问的时间  
  20.   
  21.         time_t     st_mtime;      //文件内容最后被修改的时间  
  22.   
  23.         time_t     st_ctime;      //文件状态改变时间  
  24.   
  25.         blksize_t st_blksize;    //文件内容对应的块大小  
  26.   
  27.         blkcnt_t   st_blocks;     //文件内容对应的块数量  
  28.   
  29.       };  


可以使用下面的宏确定文件的类型,宏的参数都是结构体stat的成员st_mode.

posix.1实现允许将进程间通信(IPC)对象表示为文件,下面的宏确定可用来确定IPC对象的类型。

上述宏的参数是结构体stat。


那么如何才能获取stat结构体呢,使用下面三个函数:

[cpp] view plain copy
  1. #include <sys/stat.h>  
  2. int stat(const char* restrict pathname, struct stat* restrict buf);  
  3. int fstat(int filedes, struct stat *buf);  
  4. int lstat(const char* restrict pathname, struct stat* restrict buf );  
  5. //三个函数如果成功则返回0,出错返回-1.  

lstat类似于stat,但是当文件时一个符号链接时,lstat获取的是该符号链接的有关信息,而不是该符号链接引用的

文件的信息。

实践:

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <sys/stat.h>  
  3.   
  4. int main(int argc, char * argv[]){  
  5.         struct stat buf;  
  6.   
  7.         if(argc != 2){  
  8.                 printf("you must specify one parameter.\n");  
  9.                 return -1;  
  10.         }  
  11.   
  12.         if(stat(argv[1],&buf) < 0){  
  13.                 perror("stat");  
  14.                 return -1;  
  15.         }  
  16.   
  17.         if(S_ISREG(buf.st_mode)){  
  18.                 printf("regular.\n");  
  19.         }else if(S_ISDIR(buf.st_mode)){  
  20.                 printf("directory.\n");  
  21.         }else if(S_ISBLK(buf.st_mode)){  
  22.                 printf("block.\n");  
  23.         }else if(S_ISLNK(buf.st_mode)){  
  24.                 printf("symbolic link.\n");  
  25.         }  
  26.   
  27.         return 0;  
  28. }  
运行结果:

yan@yan-vm:~/apue$ ll /etc/init.d/acpid
lrwxrwxrwx 1 root root 21 Apr 12 20:33 /etc/init.d/acpid -> /lib/init/upstart-job*
yan@yan-vm:~/apue$ ./a.out /etc/init.d/acpid
regular.


如果将stat修改为lstat,结果如下:

yan@yan-vm:~/apue$ ll /etc/init.d/acpid
lrwxrwxrwx 1 root root 21 Apr 12 20:33 /etc/init.d/acpid -> /lib/init/upstart-job*
yan@yan-vm:~/apue$ ./a.out /etc/init.d/acpid
symbolic link.

原创粉丝点击