stat.h头文件,轻松获取文件属性(2…

来源:互联网 发布:淘宝网服装女装夏装 编辑:程序博客网 时间:2024/06/05 10:43
lstat
lstat
函数名: lstat
功  能: 获取一些文件相关的信息
用  法: int lstat(const char *path, struct stat*buf);
        参数:
path:文件路径名。
filedes:文件描述词。
buf:是以下结构体的指针
struct stat {
      dev_t     st_dev;     
       ino_t     st_ino;     
     mode_t    st_mode;   
     nlink_t   st_nlink;   
     uid_t     st_uid;     
     gid_t     st_gid;     
     dev_t     st_rdev;   
     off_t     st_size;   
     blksize_tst_blksize;
     blkcnt_tst_blocks;   
     time_t    st_atime;   
     time_t    st_mtime;   
     time_t    st_ctime;   
};
返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EBADF: 文件描述词无效
EFAULT: 地址空间不可访问
ELOOP: 遍历路径时遇到太多的符号连接
ENAMETOOLONG:文件路径名太长
ENOENT:路径名的部分组件不存在,或路径名是空字串
ENOMEM:内存不足
ENOTDIR:路径名的部分组件不是目录
程序例: lstat( szFilePath, &buf) <0
#include <sys/stat.h>
fstat/stat/lstat系统调用
int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);
int stat(const char *restrict pathname,struct stat *restrictbuf);
int fstat(int fields,struct stat *buf);
int lstat(const char *restrict pathname,struct stat *restrictbuf);
一旦给出pathname:
stat函数就返回与此命名文件有关的信息结构,
fstat函数获取已在描述符fields上打开文件的有关信息。
lstat函数类似于stat.但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是
由该符号链接引用文件的信息。
第二个参数buf是指针,它指向一个我们必须提供的结构,这些函数填写由buf指向的结构。
struct stat{
    mode_tst_mode;     //文件类型和权限信息
    ino_t   st_ino;      //i结点标识
    dev_t   st_dev;      //devicenumber (file system)
    dev_t   st_rdev;     //devicenumber for special files
    nlink_tst_nlink;    //符号链接数
    uid_t   st_uid;      //用户ID
    gid_t   st_gid;      //组ID
    off_t   st_size;     //sizein bytes,for regular files
    time_tst_st_atime; //最后一次访问的时间
    time_tst_mtime;    //文件内容最后一次被更改的时间
    time_tst_ctime;    //文件结构最后一次被更改的时间
    blksize_tst_blksize; //best I/O block size
    blkcnt_tst_blocks; //number of disk blocks allocated
   };
文件类型:
普通文件,目录文件,块特殊文件,字符特殊文件,套接字,FIFO,符号链接.
文件类型信息包含在stat结构的st_mode成员中,可以用如下的宏确定文件类型,这些宏是stat结构中的
st_mode成员.
S_ISREG();S_ISDIR();S_ISCHR();S_ISBLK();S_ISFIFO();S_ISLNK();S_ISSOCK()

struct stat buf;
        char* ptr;
  if(lstat(argv,&buf)<0)
  if (S_ISREG(buf.st_mode))
                ptr="普通文件";
        if(S_ISDIR(buf.st_mode))
                ptr="目录";

示例:
     #include<iostream>
     intmain(int argc,char* argv[])
     {
          inti;
        structstat buf;
        char* ptr;
        
        for(i=1;i<argc;i++)
         {
            if(lstat(argv
,&buf)<0)
               {
                 perror("错误原因是:");
                 continue;
               }
            if(S_ISREG(buf.st_mode))
                ptr="普通文件";
            if(S_ISDIR(buf.st_mode))
                ptr="目录";
            
            //......andso on...
            
           cout<<"参数为:"<<argv<<"的标识是一个"<         }
        exit(0);
     }



sys/stat.h - data returned by the stat() function

NAME
sys/stat.h - data returned by thestat() function
SYNOPSIS


#include <sys/stat.h>


DESCRIPTION
The <sys/stat.h> header defines thestructure of the data returned bythe functions fstat(), lstat(), and stat().
The structure stat contains at least the followingmembers:


dev_t     st_dev     IDof device containing file
ino_t     st_ino     fileserial number
mode_t    st_mode    modeof file (see below)
nlink_t   st_nlink   numberof links to the file
uid_t     st_uid     userID of file
gid_t     st_gid     groupID of file
dev_t     st_rdev    deviceID (if file is character or block special)
off_t     st_size    filesize in bytes (if file is a regular file)
time_t    st_atime   timeof last access
time_t    st_mtime   timeof last datamodification
time_t    st_ctime   timeof last status change
blksize_t st_blksize a filesystem-specific preferred I/O block sizefor
                     thisobject.  In some filesystem types, this may
                     varyfrom file to file
blkcnt_t  st_blocks  number of blocks allocated for thisobject


File serial number and device ID taken together uniquely identifythe file within the system. The blkcnt_t, blksize_t, dev_t, ino_t,mode_t, nlink_t, uid_t, gid_t, off_t and time_t types are definedas described in <sys/types.h>. Timesare given in seconds since the Epoch.

The following symbolic names for the values of st_mode are alsodefined:

File type:

S_IFMT
type of file
S_IFBLK
block special
S_IFCHR
character special
S_IFIFO
FIFO special
S_IFREG
regular
S_IFDIR
directory
S_IFLNK
symbolic link
File mode bits:

S_IRWXU
read, write, execute/search by owner
S_IRUSR
read permission, owner
S_IWUSR
write permission, owner
S_IXUSR
execute/search permission, owner
S_IRWXG
read, write, execute/search by group
S_IRGRP
read permission, group
S_IWGRP
write permission, group
S_IXGRP
execute/search permission, group
S_IRWXO
read, write, execute/search by others
S_IROTH
read permission, others
S_IWOTH
write permission, others
S_IXOTH
execute/search permission, others
S_ISUID
set-user-ID on execution
S_ISGID
set-group-ID on execution
S_ISVTX
on directories, restricted deletion flag
The bits defined by S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP,S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH, S_ISUID,S_ISGID  and S_ISVTX are unique.

S_IRWXU is the bitwise OR of S_IRUSR, S_IWUSR andS_IXUSR.

S_IRWXG is the bitwise OR of S_IRGRP, S_IWGRP andS_IXGRP.

S_IRWXO is the bitwise OR of S_IROTH, S_IWOTH andS_IXOTH.

Implementations may OR other implementation-dependent bits intoS_IRWXU, S_IRWXG and S_IRWXO, but they will not overlap any of theother bits defined in this document. The file permission bits aredefined to be those corresponding to the bitwise inclusive OR ofS_IRWXU, S_IRWXG and S_IRWXO.

The following macros will test whether a file is of the specifiedtype. The value m supplied to the macros is the value of st_modefrom a stat structure. The macro evaluates to a non-zero value ifthe test is true, 0 if the test is false.

S_ISBLK(m)
Test for a block special file.
S_ISCHR(m)
Test for a character special file.
S_ISDIR(m)
Test for a directory.
S_ISFIFO(m)
Test for a pipe or FIFO special file.
S_ISREG(m)
Test for a regular file.
S_ISLNK(m)
Test for a symbolic link.
The implementation may implement message queues, semaphores, orshared memory objects as distinct file types. The following macrostest whether a file is of the specified type. The value of the bufargument supplied to the macros is a pointer to a stat structure.The macro evaluates to a non-zero value if the specified object isimplemented as a distinct file type and the specified file type iscontained in the stat structure referenced by buf. Otherwise, themacro evaluates to zero.

S_TYPEISMQ(buf)
Test for a message queue
S_TYPEISSEM(buf)
Test for a semaphore
S_TYPEISSHM(buf)
Test for a shared memory object
The following are declared as functions and may also be defined asmacros. Function prototypes must be provided for use with an ISO Ccompiler.


int    chmod(constchar *, mode_t);
int    fchmod(int,mode_t);
int    fstat(int,struct stat *);
int    lstat(constchar *, struct stat *);
int    mkdir(constchar *, mode_t);
int    mkfifo(constchar *, mode_t);
int    mknod(constchar *, mode_t, dev_t);
int    stat(constchar *, struct stat *);
mode_t umask(mode_t);


APPLICATION USAGE
Use of the macros is recommended for determining the type of afile.
FUTURE DIRECTIONS
None.
SEE ALSO
chmod(), fchmod(), fstat(), lstat(), mkdir(), mkfifo(), mknod(),stat(), umask(), <sys/types.h>.
0 0