File and directory: stat fstat lstat Function

Source: Internet
Author: User
Tags lstat

文件和目录:stat fstat lstat函数

#include <sys/stat.h>int stat( const char *restrict pathname, struct stat *restrict buf );int fstat( int filedes, struct stat *buf );int lstat( const char *restrict pathname, struct stat *restrict buf );文件三个函数的返回值:若成功则返回0,若出错则返回-1

Once the pathname is given, the stat function returns the information structure related to this name file.

The fstat function obtains information about the opened object on the descriptor filedes.

The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, rather than the information of the file referenced by the symbolic link.

The second parameter Buf is a pointer that points to a structure that we must provide. These functions are in the structure directed by the Buf. The actual definition of this structure may vary with implementation, but its basic form is:

struct stat{    mode_t     st_mode;    /* file type & mode (permissions) */    ino_t      st_ino;     /* i-node number (serial number) */    dev_t      st_dev;     /* device number (file system) */    dev_t      st_rdev;    /* device number for special files */    nlink_t    st_nlink;   /* number of links */    uid_t      st_uid;     /* user ID of owner */    gid_t      st_gid;     /* group ID of owner */    off_t      st_size;    /* size in bytes, for regular files */    time_t     st_atime;   /* time of last access */    time_t     st_mtime;   /* time of last modification */    time_t     st_ctime;   /* time of last file status change */    blksize_t  st_blksize; /* best I/O block size */    blkcnt_t   st_blocks;  /* number of disk blocks allocated */};



The lstat parameter prints an instance of the file type:

# Include "apue. H"
Int main (INT argc, char * argv [])
{
Int I;
Struct stat Buf;
Char * PTR;
For (I = 1; I <argc; I ++)
{

Printf ("% s:", argv [I]);
If (lstat (argv [I], & BUF) <0)
{
Err_ret ("lstat error ");
Continue;

}
If (s_isreg (BUF. st_mode) PTR = "regular ";
Else if (s_isdir (BUF. st_mode) PTR = "directory ";
Else if (s_ischr (BUF. st_mode) PTR = "character special ";
Else if (s_isblk (BUF. st_mode) PTR = "Block special ";
Else if (s_isfifo (BUF. st_mode) PTR = "FIFO ";
Else if (s_islnk (BUF. st_mode) PTR = "Symbolic Link ";
Else if (s_issock (BUF. st_mode) PTR = "socket ";
Else PTR = "** unknown mode **";
Printf ("% s \ n", PTR );
}
Exit (0 );
}

 运行结果


File and directory: stat fstat lstat Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.