Stat, Fstat, lstat functions __ functions

Source: Internet
Author: User
Tags bitwise lstat posix

NOTE: Reprint please indicate the source

The specific information for the function is as follows:

function to get file information

Header file

<sys/types.h>

<sys/stat.h>

<unistd.h>

function form

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

int fstat (int filedes, struct stat *buf);

int Lstat (const char *path, struct stat *buf);

return value

Success

Failed

Whether to set errno

0

-1

Is

Description

You can still get file information even if you do not have read access to the file. For the stat function and the Lstat function, if you want to obtain file information in a directory, you require appropriate access to all parent directories in which the file is located, that is, to have executable permissions on the directory (x).

(1): The stat function obtains the relevant file information by pointing to a pointer to the path, and writes the file information to the second parameter of the function, which is a pointer to the stat structure body.

(2): The Lstat function is almost identical to the function of the stat function. If the path given to a file or directory is a symbolic link (a symbolic link can be understood to be similar to the concept of a shortcut in the Windows operating system), you will get information about the symbolic link instead of the symbolic link pointing to the file.

(3): The Fstat function is the same as the stat function, except that the first argument invoked is replaced by a file descriptor.

The specific definition of the stat structure is as follows:

struct STAT

{

dev_t St_dev;

ino_t St_ino;

mode_t St_mode;

nlink_t St_nlink;

uid_t St_uid;

gid_t St_gid;

dev_t St_rdev;

off_t st_size;

blksize_t st_blksize;

blkcnt_t st_blocks;

time_t St_atime;

time_t St_mtime;

time_t St_ctime;

};

Parameter meaning:

St_dev: The disk device ID number where the file is.

St_inode:inode index number

St_mode: File access permissions. Linux is programmed to follow the POSIX specification and provides several macros to determine the type to which the given path points, such as the following table:

macro Description of the check file type defined in POSIX

macro Definition Form

Description

S_isreg (M)

is a normal file

S_isdir (M)

is a directory

S_ISCHR (M)

is a character device

S_ISBLK (M)

is a block device

S_isfifo (M)

is FIFO (named pipe file, for process communication)

S_islnk (M)

is a symbolic link

S_issock (M)

is a socket

The Linux system also defines the macros shown in the following table to represent St_mode file access rights:

related macros defined in St_mode

macro Definition

value

Description

S_ifmt

0170000

Get the screen bit of the file type

S_ifsock

0140000

Socket

S_iflnk

0120000

Symbolic Links

S_ifreg

0100000

Normal file

S_ifblk

0060000

Block device

S_ifdir

0040000

Directory

S_ifchr

0020000

Character devices

S_ififo

0010000

FIFO file

S_isuid

0004000

Set the user ID bit

S_isgid

0002000

Set the group ID bit

S_isvtx

0001000

Set the sticky bit

S_irwxu

00700

Shield bit to get file owner permission

S_irusr

00400

File owner has Read permission

S_iwusr

00200

File owner has write permission

S_ixusr

00100

File owner has execute permission

S_irwxg

00070

Get the screen bits for the owner of the file with the same group member permissions

S_irgrp

00040

The file owner has read access to the same group member

S_iwgrp

00020

File owner has write permission to the same group member

S_ixgrp

00010

File owners have execute permissions on the same group members

S_irwxo

00007

Screen bit for other user rights

S_iroth

00004

Other users have Read permissions

S_iwoth

00002

Other users have Write permissions

S_ixoth

00001

Other users have Execute permissions

St_nlink: The number of hard links the file has

St_uid: The user ID number of the owner of the file, which is a good ID for which specific user can be obtained by viewing the/etc/passwd file.

St_gid: The group ID where the file owner is.

St_rdev: If the given path points to a specific device, the value will give the device identifier information.

St_size: For a regular file, the value file size information; For symbolic links, the length of the directory to which this value is linked; For device files, the value is assigned to 0.

St_blksize: The block size of the file system.

St_atime: The last access time for the file.

St_mtime: The last time the file was modified.

St_ctime: The last modification time to a file property.

Error message:

Eacces: The path given to the file does not have access rights.

EBADF: Illegal file descriptor.

Efault: Address error.

Floop: The number of symbolic links in the path to the given file is more than that.

Enametoolong: File name is too long.

Enoent: The path to the given file does not exist or the path is an empty string.

Enomem: Not enough memory.

Enotdir: The given file contains a path that is not part of the directory.

Instance:

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int main (int argc, char *argv[])

{

Defines a struct file_stat with a type of stat that is used to save the obtained file information

struct stat file_stat;

Determine if the program is executed with a parameter, if not, give a hint and end the run

if (argc!= 2)

{

printf ("usage:%s filename\n", argv[0]);

return (1);

}

Call the stat function, if an error occurs, give an error message, the program exits

if (stat (argv[1], &file_stat) = = 1)

{

Perror ("Cannot get the information of the file!\n");

return (1);

}

Use the macros defined in POSIX to determine if it is a regular file

if (S_isreg (File_stat.st_mode))

{

printf ('%s is Regular file,judged by s_isreg\n ', argv[1]);

}

Determining whether a regular file is based on the bitwise operation of St_mode and S_ifreg

if (File_stat.st_mode & S_ifreg)

{

printf ('%s is Regular file,judged by bits calculates_ifreg\n ', argv[1]);

}

To determine whether a directory is by S_isdir macros

if (S_isdir (File_stat.st_mode))

{

printf ("%s is Directory, judged by s_isdir\n", argv[1]);

}

if (File_stat.st_mode & S_ifdir)

{

printf ("%s is Directory, judged by bits calculate s_ifdir\n", argv[1]);

}

Output other file information in File_stat

printf ("Owner ID:%d, Group ID:%d\n", File_stat.st_uid,file_stat.st_gid);

printf ("Permission:%o\n", File_stat.st_mode & 0x1ff);

printf ("Last Access Time:%15s\n", CTime (&file_stat.st_atime));

printf ("Last Modification Time:%15s\n", CTime (&file_stat.st_mtime));

printf ("Last Status Change Time:%15s\n", CTime (&file_stat.st_ctime));

return (0);

}

Two methods are given in the code to determine the types of arguments given: one is a macro expression defined in the POSIX standard, and one that is obtained by bitwise operations. When obtaining access access, the program will get the St_mode value and a hexadecimal number of bitwise operations, 0X1FF conversion into octal is 777. To get permission from the file owner, you need to perform a bitwise operation with 0700. To get the permissions of the group where the file owner is located, you need to perform a bitwise operation with 0070. To get permission from another user, you need to perform a bitwise operation with 0007. To get 3 of the permissions, the need and 777 bitwise operations, that is, the use of 0x1ff in the program.

The results of the run:

[Root@localhost test]#./stat chdir

ChDir is Regular file,judged by S_isreg

ChDir is Regular file,judged by Bitscalculate S_ifreg

Owner id:0, Group id:0

permission:755

Last Access time:tue APR 30 01:48:53 2013

Last modification Time:tue APR 30 01:43:172013

Last Status change Time:tue Apr 3001:43:17 2013

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.