跳转至

文件

1 dirent 结构 d_type 类型

  • 头文件:#include <dirent.h> struct dirent 数据结构如下:
/* file location /usr/include/bits/dirent.h */
struct dirent
  {
#ifndef __USE_FILE_OFFSET64
    __ino_t d_ino;
    __off_t d_off;
#else
    __ino64_t d_ino;
    __off64_t d_off;
#endif
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];       /* We must not include limits.h! */
  };

d_type 可选类型有:

/* file location /usr/include/dirent.h */
/* File types for `d_type'.  */
enum
  {
    DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
    DT_FIFO = 1,
# define DT_FIFO    DT_FIFO
    DT_CHR = 2,
# define DT_CHR     DT_CHR
    DT_DIR = 4,
# define DT_DIR     DT_DIR
    DT_BLK = 6,
# define DT_BLK     DT_BLK
    DT_REG = 8,
# define DT_REG     DT_REG
    DT_LNK = 10,
# define DT_LNK     DT_LNK
    DT_SOCK = 12,
# define DT_SOCK    DT_SOCK
    DT_WHT = 14
# define DT_WHT     DT_WHT
  };

/* Convert between stat structure types and directory types.  */
# define IFTODT(mode)   (((mode) & 0170000) >> 12)
# define DTTOIF(dirtype)    ((dirtype) << 12)

2 stat 结构的 st_mode

  • 头文件:#include <sys/stat.h> stat 数据结构如下:
/* file location /usr/include/bits/stat.h */
struct stat
{
dev_t st_dev; /*device*/
ino_t st_ino; /*inode*/
mode_t st_mode; /*protection*/
nlink_t st_nlink; /*number of hard links */
uid_t st_uid; /*user ID of owner*/
gid_t st_gid; /*group ID of owner*/
dev_t st_rdev; /*device type */
off_t st_size; /*total size, in bytes*/
unsigned long st_blksize; /*blocksize for filesystem I/O */
unsigned long st_blocks; /*number of blocks allocated*/
time_t st_atime; /* time of lastaccess*/
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */
};
/* file location /usr/include/bits/stat.h */
/* Encoding of the file mode.  */
#define __S_IFMT    0170000 /* These bits determine file type.  */
/* 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.  */

/* file location /usr/include/fcntl.h  or /usr/include/sys/stat.h */
# define S_IFMT     __S_IFMT
# 3 define S_IFDIR  __S_IFDIR
# define S_IFCHR    __S_IFCHR
# 3 define S_IFBLK  __S_IFBLK
# define S_IFREG    __S_IFREG
# ifdef __S_IFIFO
#  define S_IFIFO   __S_IFIFO
# endif
# ifdef __S_IFLNK
#  define S_IFLNK   __S_IFLNK
# endif
# if (defined __USE_UNIX98 || defined __USE_XOPEN2K8) && defined __S_IFSOCK
#  define S_IFSOCK  __S_IFSOCK
# endif

3 open 方法 flags 参数

  • 头文件:#include <fcntl.h> open 接口如下:
/* file location /usr/include/fcntl.h */
int open( const char *pathname, int flags);
int open( const char *pathname, int flags, mode_t mode);

flags 参数可选类型有:

/* file location /usr/include/bits/fcntl-linux.h */
/* open/fcntl.  */
#define O_ACCMODE      0003
#define O_RDONLY         00
#define O_WRONLY         01
#define O_RDWR           02
#ifndef O_CREAT
# define O_CREAT       0100 /* Not fcntl.  */
#endif
#ifndef O_EXCL
# define O_EXCL        0200 /* Not fcntl.  */
#endif
#ifndef O_NOCTTY
# define O_NOCTTY      0400 /* Not fcntl.  */
#endif
#ifndef O_TRUNC
# define O_TRUNC      01000 /* Not fcntl.  */
#endif
#ifndef O_APPEND
# define O_APPEND     02000
#endif
#ifndef O_NONBLOCK
# define O_NONBLOCK   04000
# 4 #endif
#ifndef O_NDELAY
# 4 define O_NDELAY O_NONBLOCK
#endif
#ifndef O_SYNC
# define O_SYNC        04010000
#endif
#define O_FSYNC     O_SYNC
#ifndef O_ASYNC
# define O_ASYNC     020000
#endif
#ifndef __O_LARGEFILE
# define __O_LARGEFILE  0100000
#endif

4 open 和 chmod 方法 mode 参数

  • 头文件:#include <fcntl.h>#include <sys/stat.h> open 接口如下:
/* file location /usr/include/fcntl.h */
int open( const char *pathname, int flags);
int open( const char *pathname, int flags, mode_t mode);

/* file location /usr/include/stat.h */
int chmod(const char *path, mode_t mode);

mode 参数可选类型有:``

/* file location /usr/include/fcntl.h */
#define __S_IREAD   0400    /* Read by owner.  */
#define __S_IWRITE  0200    /* Write by owner.  */
#define __S_IEXEC   0100    /* Execute by owner.  */

# define S_IRUSR    __S_IREAD       /* Read by owner.  */
# define S_IWUSR    __S_IWRITE      /* Write by owner.  */
# define S_IXUSR    __S_IEXEC       /* Execute by owner.  */
/* Read, write, and execute by owner.  */
# define S_IRWXU    (__S_IREAD|__S_IWRITE|__S_IEXEC)

# define S_IRGRP    (S_IRUSR >> 3)  /* Read by group.  */
# define S_IWGRP    (S_IWUSR >> 3)  /* Write by group.  */
# define S_IXGRP    (S_IXUSR >> 3)  /* Execute by group.  */
/* Read, write, and execute by group.  */
# define S_IRWXG    (S_IRWXU >> 3)

# define S_IROTH    (S_IRGRP >> 3)  /* Read by others.  */
# define S_IWOTH    (S_IWGRP >> 3)  /* Write by others.  */
# define S_IXOTH    (S_IXGRP >> 3)  /* Execute by others.  */
/* Read, write, and execute by others.  */
# define S_IRWXO    (S_IRWXG >> 3)

5 lseek 方法 whence 参数

  • 头文件:`#include
/* file location /usr/include/unistd.h */
off_t lseek(int fildes, off_t offset, int whence);

whence 可选值如下:

/* file location /usr/include/unistd.h */
/* Values for the WHENCE argument to lseek.  */
#ifndef _STDIO_H        /* <stdio.h> has the same definitions.  */
# define SEEK_SET   0   /* Seek from beginning of file.  */
# define SEEK_CUR   1   /* Seek from current position.  */
# define SEEK_END   2   /* Seek from end of file.  */
# ifdef __USE_GNU
#  define SEEK_DATA 3   /* Seek to next data.  */
#  define SEEK_HOLE 4   /* Seek to next hole.  */
# endif
#endif