C language: stat, fstat, and lstat Functions

Source: Internet
Author: User
Tags lstat

The functions of these three functions are consistent. They are used to obtain file-related information, but applied to different file objects. If the pathname parameter is provided in the function, the stat function returns the information structure related to this naming file. The fstat function obtains information about the file opened on the descriptor fields, the lstat function is similar to the stat function. However, when a named file is a symbolic link, lstat returns information about the symbolic link, rather than the information about the file referenced by the symbolic link. The second parameter Buf is a pointer pointing to a structure used to save the file description information. The function fills in the structure content. The actual definition of this structure may vary with the implementation.


# Include

Int Stat (const char * path, struct stat * BUF );
Int fstat (INT filedes, struct stat * BUF );
Int lstat (const char * path, struct stat * BUF );

Path: file path name.
Filedes: The description of a file.
Buf: pointer of the following struct

Struct stat {
Mode_t st_mode; // (file protection mode) file type and permission information
Ino_t st_ino; // file node number
Dev_t st_dev; // File System ID of the device where the file is located: Device number (File System)
Dev_t st_rdev; // device ID of the special device file in the file for device number for special files
Nlink_t st_nlink; // Number of symbolic links
Uid_t st_uid; // user ID of the file
Gid_t st_gid; // file user group ID
Off_t st_size; // The total size, in bytes, for regular files
Time_t st_st_atime; // The last access time of the file content
Time_t st_mtime; // The last modification time of the file content
Time_t st_ctime; // time when the last state of the file structure changes
Blksize_t st_blksize; // optimal I/O block size of the file system best I/O BLOCK SIZE
Blkcnt_t st_blocks; // number of blocks allocated to the file. The value of 512 bytes is 1 unit number of disk blocks allocated.

File Type:
Common files, directory files, block special files, character special files, sockets, FIFO, symbolic links.
The file type information is included in the st_mode Member of the stat structure. You can use the following macros to determine the file type. These macros are members of st_mode In the stat structure.
S_isreg ();

S_isdir ();

S_isblk ();

S_ischr ();

S_issock ();

S_isfifo ();

S_islnk ();

Return description:
0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
Ebadf: invalid file description
Efault: The address space is inaccessible.
Eloop: too many symbolic connections are encountered during path traversal.
Enametoolong: the file path name is too long.
Enoent: some components of the path name do not exist, or the path name is an empty string.
Enomem: insufficient memory
Enotdir: some components of the path name are not directories.

# Include
Int main (INT argc, char * argv [])
Int I;
Struct stat Buf;
Char * PTR;

For (I = 1; I
If (lstat (argv [I], & BUF) <0)
Perror ("error cause :");

If (s_isreg (BUF. st_mode ))
PTR = "normal file ";
If (s_isdir (BUF. st_mode ))
PTR = "directory ";

//...... And so on...

Cout <"parameter:" <"indicates a" <
Exit (0 );

Related Article

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.