File Types

/usr/include/bits/stat.h

/* Encoding of the file mode.*/

/*取位宏,000 001111 000 000 000 000*/

#define __S_IFMT        0170000 /* These bits determine file type.*/

/*7种文件类型宏*/

/* File types.*/

#define __S_IFDIR        0040000 /* Directory.  */

#define __S_IFCHR       0020000 /* Character device.  */

#define __S_IFBLK        0060000 /* Block device.  */

#define __S_IFREG        0100000 /* Regular file.  */

#define __S_IFIFO        0010000 /* FIFO.  */

#define __S_IFLNK        0120000 /* Symbolic link.  */

#define __S_IFSOCK      0140000 /* Socket.  */

 

16 bits.其设计对应于inode里保存的文件mode项。

Fifo                       000 000001 000 000 000 000          1

Character device     000 000010 000 000 000 000          2

Directory                000 000100 000 000 000 000          4

Block device           000 000110 000 000 000 000           6

Regular file             000 001000 000 000 000 000           8

Symbolic link           000 001010 000 000 000 000          10

Socket                  000 001100 000 000 000 000           12

 

----------------------------------------------------------------------------------------

/usr/include/sys/stat.h

#include <bits/stat.h>    /*红体即来自该文件*/

/*7种文件类型的测试宏*/

/* Test macros for file types.  */

#define __S_ISTYPE(mode, mask)  (((mode) & __S_IFMT) == (mask))

#define S_ISDIR(mode)    __S_ISTYPE((mode), __S_IFDIR)

#define S_ISCHR(mode)    __S_ISTYPE((mode), __S_IFCHR)

#define S_ISBLK(mode)    __S_ISTYPE((mode), __S_IFBLK)

#define S_ISREG(mode)    __S_ISTYPE((mode), __S_IFREG)

#ifdef __S_IFIFO

# define S_ISFIFO(mode)  __S_ISTYPE((mode), __S_IFIFO)

#endif

#ifdef __S_IFLNK

# define S_ISLNK(mode)   __S_ISTYPE((mode), __S_IFLNK)

#endif

 

#if defined __USE_BSD && !defined __S_IFLNK

# define S_ISLNK(mode)  0

#endif

 

#if (defined __USE_BSD || defined __USE_UNIX98) /

    && defined __S_IFSOCK

# define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)

#endif

 

所有7个测试宏加上对应的文件类型宏都归于宏__S_ISTYPE(mode, mask)

该宏的操作是将mode和取位宏__S_IFMT进行与操作,以取出mode的第12位到第15位,再与对应的文件类型宏mask进行比较,结果是个bool值。

----------------------------------------------------------------------------------------

Use test macros to test a file’s type, for example:

File “dir” is a directory. First we get its information via any of the stat functions.

S_ISDIR(buf.st_mode)    /*struct stat buf;*/

 

__S_ISTYPE((buf.st_mode), __S_IFDIR)   /*加入对应的文件类型宏*/

 

((buf.st_mode) & 000 001111 000 000 000 000) == (000 000100 000 000 000 000)

/*加入取位宏*/

 

与运算,屏蔽其他位,主要摘出buf.st_mode的第12位到第15位,和对应的文件类型值进行比较。

 

mode_t is unsigned 32 bits

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值