Linux 基础(一)stat函数

2019-05-24 06:10:14来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Header file:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

DEFINITION:

int stat(const char *pathname, struct stat *buf);

DESCRIPTION:

  The functions return information about a file, in the buffer pointed to by buf.

No permissions are required on the file itself, but—in the case of stat(), fstatat(), and
lstat()—execute (search) permission is required on all of the directories in pathname
that lead to the file.

 

stat() and fstatat() retrieve information about the file pointed to by pathname;

stat() and fstatat() retrieve information about the file pointed to by pathname; the differences for fstatat() are described below.

lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that it refers to.

fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by the file descriptor fd.

 

RETURN VALUE:

On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS:

EACCES Search permission is denied for one of the directories in the path prefix of path‐ ame. (See also path_resolution(7).)

EBADF fd is bad.

EFAULT Bad address.

ELOOP Too many symbolic links encountered while traversing the path.

ENAMETOOLONG   pathname is too long.

ENOENT       A component of pathname does not exist, or pathname is an empty string.

ENOMEM   Out of memory (i.e., kernel memory).

ENOTDIR  A component of the path prefix of pathname is not a directory.

EOVERFLOW  pathname or fd refers to a file whose size, inode number, or number of blocks can‐ ot be represented in, respectively, the types off_t, ino_t, or blkcnt_t. This
error can occur when, for example, an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes.

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
 
int main() {
    struct stat buf;
    stat("HOME/C++/test", &buf);
    printf("the file size = %d\n", buf.st_size);
}

 

stat structure:

           struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* inode number */
               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 ID (if special file) */
               off_t     st_size;        /* total size, in bytes */
               blksize_t st_blksize;     /* blocksize for filesystem I/O */
               blkcnt_t  st_blocks;      /* number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

      

          struct timespec st_atim; /* time of last access */
          struct timespec st_mtim; /* time of last modification */
          struct timespec st_ctim; /* time of last status change */

          #define st_atime st_atim.tv_sec /* Backward compatibility */
          #define st_mtime st_mtim.tv_sec
          #define st_ctime st_ctim.tv_sec};

};

 

 fstat(int fd,struct stat *)接收的已open的文件描述符

 

stat(char *filename,struct stat *)接收的路径名, 需要注意的是 能处理符号链接,但处理的是符号链接指向的文件。


lstat(char *filename,struct stat *)接收的路径名  ,需要注意的是,也能能处理符号链接,但处理的是符号链接本身(自身)文件。

 


原文链接:https://www.cnblogs.com/Thrylos/p/10915102.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:linux的vi和vim编辑器操作

下一篇:SaltStack--接口salt-api