xxxxxxxxxx
Because tests of the above form are common, additional macros are defined by POSIX to allow the test of the file type in st_mode to be written more concisely:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The preceding code snippet could thus be rewritten as:
stat(pathname, &sb);
if (S_ISREG(sb.st_mode)) {
/* Handle regular file */
}
xxxxxxxxxx
#include <stdio.h>
#include <cygwin/stat.h>
#include <sys/stat.h>
int main()
{
struct stat sb;
char *file_path = "./";
if( stat( file_path, &sb) != -1) // Check the return value of stat
{
if( S_ISREG( sb.st_mode ) != 0 )
{
printf( "%s is a file", file_path ) ;
}
else
{
printf( "%s is not a file", file_path ) ;
}
}
}
/*
Capturing the type of file and performing Decision Tree actions
Because tests of the above form are common, additional macros are defined by POSIX to allow the test of the file type in st_mode to be written more concisely:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The preceding code snippet could thus be rewritten as:
stat(pathname, &sb);
if (S_ISREG(sb.st_mode)) {
/* Handle regular file */
}
# sourced from https://stackoverflow.com/questions/40163270/what-is-s-isreg-and-what-does-it-do
*/