当前位置: 移动技术网 > 科技>操作系统>Linux > Linux 基础(一)stat函数

Linux 基础(一)stat函数

2019年05月24日  | 移动技术网科技  | 我要评论

黄镇东简历,星木在哪买,罗宾·范佩西

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‐name. (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‐not 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 *)接收的路径名  ,需要注意的是,也能能处理符号链接,但处理的是符号链接本身(自身)文件。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网