S_isreg a few common macros (GO)

Source: Internet
Author: User

From Baidu Library: Http://wenku.baidu.com/view/31777dc1d5bbfd0a795673b1.htmlstat function Explanation:

Table header files: #include <sys/stat.h>
#include <unistd.h>
Define functions: Int stat (const char *file_name, struct stat *buf);
Function Description: Obtains file information by filename, and is stored in the structure stat referred to by buf
Return value: Execution succeeds returns 0, failure returns-1, error code stored in errno
Error code:
ENOENT parameter file_name The specified file does not exist
Directories in the Enotdir path exist but are not real directories
Eloop the file you want to open has too many symbolic connection problems, with a maximum of 16 symbolic connections
Efault parameter BUF is invalid pointer, pointing to memory space that cannot exist
Eaccess denied when accessing file
Enomem Core memory is low
Enametoolong parameter file_name The path name is too long

int stat (const char *file_name,struct stat *buf);
int fstat (int filedes,struct stat *buf);
The stat structure is defined in:/usr/include/sys/stat.h file
struct stat finfo;
Stat (sFileName, &finfo);
int size = Finfo. St_size;
struct STAT {
mode_t St_mode; File corresponding to the mode, file, directory, etc.
ino_t St_ino; I-node Node Number
dev_t St_dev; Device number
dev_t St_rdev; Special Equipment Number
nlink_t St_nlink; Number of connections to the file
uid_t St_uid; File owner
gid_t St_gid; The group that the file owner corresponds to
off_t st_size; Normal file, corresponding file byte number
time_t St_atime; Time the file was last visited
time_t St_mtime; time when the contents of the file were last modified
time_t St_ctime; File status (attribute) change time
blksize_t st_blksize; Block size for file contents
blkcnt_t st_blocks; Number of blocks corresponding to the file contents
};

Stat is used to determine which files are not open, and fstat is used to determine which files are open. The most common attribute we use is st_mode. By using properties we can tell whether a given file is a normal file or a directory, a connection, and so on. You can use the following macros to judge.
S_islnk (St_mode): Whether it is a connection.

Whether the S_isreg is a regular file.

Whether the S_isdir is a directory

Whether the S_ISCHR is a character device.

Whether the S_ISBLK is a block device

Whether the S_isfifo is a FIFO file.

Whether the S_issock is a socket file.

Usage:

Define a struct body
struct stat m;

The file name entered by the user (to determine if it is a directory).
Char *filename;

int A;
int n = stat (filename,&m);
A = S_isdir (M.st_mode);

If A is true, the description is the directory, otherwise it is not.

S_isreg a few common macros (GO)

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.