[Fstat/STAT/lstat System Call]
Function Description:
Obtain file-related information.
Usage:
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <unistd. h>
Int Stat (const char * path, struct stat * BUF );
Int fstat (INT filedes, struct stat * BUF );
Int lstat (const char * path, struct stat * BUF );
Parameters:
Path: file path name.
Filedes: The description of a file.
Buf: pointer of the following struct
Struct stat {
Dev_t st_dev;/* identifier of the device where the file is located */
Ino_t st_ino;/* file node number */
Mode_t st_mode;/* file protection mode */
Nlink_t st_nlink;/* Number of hard connections */
Uid_t st_uid;/* file user ID */
Gid_t st_gid;/* file user group ID */
Dev_t st_rdev;/* indicates the device ID of the special device file in the file */
Off_t st_size;/* Total size, in bytes */
Blksize_t st_blksize;/* block size of the file system */
Blkcnt_t st_blocks;/* number of blocks allocated to the file. The unit is 512 bytes */
Time_t st_atime;/* last access time */
Time_t st_mtime;/* last modification time */
Time_t st_ctime;/* last state change time */
};
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.
Files And Directories
Stat, fstat, and lstat Functions
# I nclude <sys/STAT. h>
Int Stat (const char * restrict pathname, struct stat * restrict BUF );
Int fstat (INT fields, struct stat * BUF );
Int lstat (const char * restrict pathname, struct stat * restrict BUF );
Return Value: if the request succeeds, 0 is returned. If the request fails,-1 is returned.
Once the pathname is given, the stat function returns the information structure related to the named file. The fstat function obtains information about the opened file on the descriptor fields.
The lstat function is similar to stat. But when the named file is a symbolic link, lstat returns information about the symbolic link, instead of referencing the file by the symbolic link.
. The second parameter Buf is a pointer that points to a structure that we must provide. These functions are filled in the structure pointed by the Buf. The actual definition of this structure may follow the implementation
Different.
Struct stat {
Mode_t st_mode; // file type and permission information
Ino_t st_ino; // I node ID
Dev_t st_dev; // device number (File System)
Dev_t st_rdev; // device number for special files
Nlink_t st_nlink; // Number of symbolic links
Uid_t st_uid; // user ID
Gid_t st_gid; // group ID
Off_t st_size; // size in bytes, for regular files
Time_t st_st_atime; // time of the last access
Time_t st_mtime; // The last time the file content was modified
Time_t st_ctime; // time when the last file structure was changed
Blksize_t st_blksize; // best I/O BLOCK SIZE
Blkcnt_t st_blocks; // 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 st_mode members in the stat structure.
S_isreg (); s_isdir (); s_ischr (); s_isblk (); s_isfifo (); s_islnk (); s_issock ()
Example:
# I nclude <iostream>
Int main (INT argc, char * argv [])
{
Int I;
Struct stat Buf;
Char * PTR;
For (I = 1; I <argc; I ++)
{
If (lstat (argv [I], & BUF) <0)
{
Perror ("error cause :");
Continue;
}
If (s_isreg (BUF. st_mode ))
PTR = "normal file ";
If (s_isdir (BUF. st_mode ))
PTR = "directory ";
// ...... And so on...
Cout <"parameter:" <argv [I] <"indicates a" <PTR <Endl;
}
Exit (0 );
}