sys_stat.h(0P)
PROLOG โจ
okay so bestie, this is sys_stat.h(0P) โ the POSIX Programmer's Manual page for the <sys/stat.h> header. This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
NAME ๐
sys/stat.h โ data returned by the stat() function
SYNOPSIS ๐
#include <sys/stat.h>
DESCRIPTION ๐ฆ
okay so the <sys/stat.h> header shall define the structure of the data returned by the fstat(), lstat(), and stat() functions.
The <sys/stat.h> header shall define the stat structure, which shall include at least the following members:
dev_t st_devโ Device ID of device containing file.ino_t st_inoโ File serial number.mode_t st_modeโ Mode of file (see below).nlink_t st_nlinkโ Number of hard links to the file.uid_t st_uidโ User ID of file.gid_t st_gidโ Group ID of file.dev_t st_rdevโ Device ID (if file is character or block special).off_t st_sizeโ For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link. For a shared memory object, the length in bytes. For a typed memory object, the length in bytes. For other file types, the use of this field is unspecified.struct timespec st_atimโ Last data access timestamp.struct timespec st_mtimโ Last data modification timestamp.struct timespec st_ctimโ Last file status change timestamp.blksize_t st_blksizeโ A file system-specific preferred I/O block size for this object. In some file system types, this may vary from file to file.blkcnt_t st_blocksโ Number of blocks allocated for this object.
The st_ino and st_dev fields taken together uniquely identify the file within the system.
The <sys/stat.h> header shall define the blkcnt_t, blksize_t, dev_t, ino_t, mode_t, nlink_t, uid_t, gid_t, off_t, and time_t types as described in <sys/types.h>.
The <sys/stat.h> header shall define the timespec structure as described in <time.h>. Times shall be given in seconds since the Epoch.
Which structure members have meaningful values depends on the type of file. For further information, see the descriptions of fstat(), lstat(), and stat() in the System Interfaces volume of POSIX.1โ2017.
For compatibility with earlier versions of this standard, the st_atime macro shall be defined with the value st_atim.tv_sec. Similarly, st_ctime and st_mtime shall be defined as macros with the values st_ctim.tv_sec and st_mtim.tv_sec, respectively.
The <sys/stat.h> header shall define the following symbolic constants for the file types encoded in type mode_t. The values shall be suitable for use in #if preprocessing directives:
S_IFMTโ Type of file.S_IFBLKโ Block special.S_IFCHRโ Character special.S_IFIFOโ FIFO special.S_IFREGโ Regular.S_IFDIRโ Directory.S_IFLNKโ Symbolic link.S_IFSOCKโ Socket.
The <sys/stat.h> header shall define the following symbolic constants for the file mode bits encoded in type mode_t, with the indicated numeric values. These macros shall expand to an expression which has a type that allows them to be used, either singly or OR'ed together, as the third argument to open() without the need for a mode_t cast. The values shall be suitable for use in #if preprocessing directives.
S_IRWXUโ numeric value0700โ Read, write, execute/search by owner.S_IRUSRโ numeric value0400โ Read permission, owner.S_IWUSRโ numeric value0200โ Write permission, owner.S_IXUSRโ numeric value0100โ Execute/search permission, owner.S_IRWXGโ numeric value070โ Read, write, execute/search by group.S_IRGRPโ numeric value040โ Read permission, group.S_IWGRPโ numeric value020โ Write permission, group.S_IXGRPโ numeric value010โ Execute/search permission, group.S_IRWXOโ numeric value07โ Read, write, execute/search by others.S_IROTHโ numeric value04โ Read permission, others.S_IWOTHโ numeric value02โ Write permission, others.S_IXOTHโ numeric value01โ Execute/search permission, others.S_ISUIDโ numeric value04000โ Set-user-ID on execution.S_ISGIDโ numeric value02000โ Set-group-ID on execution.S_ISVTXโ numeric value01000โ On directories, restricted deletion flag.
The following macros shall be provided to test whether a file is of the specified type. The value m supplied to the macros is the value of st_mode from a stat structure. The macro shall evaluate to a non-zero value if the test is true; 0 if the test is false.
S_ISBLK(m)โ Test for a block special file.S_ISCHR(m)โ Test for a character special file.S_ISDIR(m)โ Test for a directory.S_ISFIFO(m)โ Test for a pipe or FIFO special file.S_ISREG(m)โ Test for a regular file.S_ISLNK(m)โ Test for a symbolic link.S_ISSOCK(m)โ Test for a socket.
The implementation may implement message queues, semaphores, or shared memory objects as distinct file types. The following macros shall be provided to test whether a file is of the specified type. The value of the buf argument supplied to the macros is a pointer to a stat structure. The macro shall evaluate to a non-zero value if the specified object is implemented as a distinct file type and the specified file type is contained in the stat structure referenced by buf. Otherwise, the macro shall evaluate to zero.
S_TYPEISMQ(buf)โ Test for a message queue.S_TYPEISSEM(buf)โ Test for a semaphore.S_TYPEISSHM(buf)โ Test for a shared memory object.
The implementation may implement typed memory objects as distinct file types, and the following macro shall test whether a file is of the specified type. The value of the buf argument supplied to the macros is a pointer to a stat structure. The macro shall evaluate to a non-zero value if the specified object is implemented as a distinct file type and the specified file type is contained in the stat structure referenced by buf. Otherwise, the macro shall evaluate to zero.
S_TYPEISTMO(buf)โ Test macro for a typed memory object.
The <sys/stat.h> header shall define the following symbolic constants as distinct integer values outside of the range [0,999999999], for use with the futimens() and utimensat() functions: UTIME_NOW UTIME_OMIT
The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.
int chmod(const char *, mode_t);
int fchmod(int, mode_t);
int fchmodat(int, const char *, mode_t, int);
int fstat(int, struct stat *);
int fstatat(int, const char *restrict, struct stat *restrict, int);
int futimens(int, const struct timespec [2]);
int lstat(const char *restrict, struct stat *restrict);
int mkdir(const char *, mode_t);
int mkdirat(int, const char *, mode_t);
int mkfifo(const char *, mode_t);
int mkfifoat(int, const char *, mode_t);
int mknod(const char *, mode_t, dev_t);
int mknodat(int, const char *, mode_t, dev_t);
int stat(const char *restrict, struct stat *restrict);
mode_t umask(mode_t);
int utimensat(int, const char *, const struct timespec [2], int);
Inclusion of the <sys/stat.h> header may make visible all symbols from the <time.h> header.
The following sections are informative.
APPLICATION USAGE ๐ธ
Use of the macros is recommended for determining the type of a file.
๐ RATIONALE ๐
ok so bestie, first thing: if you're writing a conforming C-language application and you're using functions that take arguments or return values of type mode_t, you have to include <sys/stat.h> โ that's the only way you get to use the symbolic values for that type. Like, that's the whole point. An alternative could have been to require those constants to also be defined by including <sys/types.h>, but nope, that's not how they did it. ๐
The S_ISUID and S_ISGID bits may be cleared on any write, not just on open(), like some historical implementations do. so don't be shocked, it's giving "traditionally unhinged" but it's allowed. ๐
System calls that update the time entry fields in the stat structure must be documented by the implementors. That's on them, not you. And POSIX-conforming systems should not update the time entry fields for functions listed in the System Interfaces volume of POSIX.1โ2017 unless the standard requires that they do โ the one exception being documented extensions to the standard. slay, boundaries. โจ
ok now this part is iconic: upon assignment, file timestamps are immediately converted to the resolution of the file system by truncation โ meaning the recorded time can be older than the actual time. yes, really. for example, if the file system resolution is 1 microsecond, then a conforming stat() must always return an st_mtim.tv_nsec that is a multiple of 1000. ๐ญ
Some older implementations returned higher-resolution timestamps while the inode information was cached, and then spontaneously truncated the tv_nsec fields when they were stored to and retrieved from disk. That behavior does not conform. periodt. ๐ซ
Also note that st_dev must be unique within a Local Area Network (LAN) in a ``system'' made up of multiple computers' file systems connected by a LAN. iykyk. ๐ฆ
Networked implementations of a POSIX-conforming system must guarantee that all files visible within the file tree โ including the parts of the tree that may be remotely mounted from other machines on the network โ on each individual processor are uniquely identified by the combination of the st_ino and st_dev fields. no cap, that's the vibe check. ๐ฅ
Now, the unit for the st_blocks member of the stat structure is not defined within POSIX.1โ2008. In some implementations it's 512 bytes. It may differ on a file system basis. And there is no correlation between values of the st_blocks and st_blksize, and the f_bsize (from <sys/statvfs.h>) structure members. so don't try to math them together bestie, they're not friends. ๐
Traditionally, some implementations defined the multiplier for st_blocks in <sys/param.h> as the symbol DEV_BSIZE. cute little historical nugget for you. ๐
And finally, storytime: some earlier versions of this standard did not specify values for the file mode bit macros. The expectation back then was that some implementors might choose to use a different encoding for these bits than the traditional one, and that new applications would use symbolic file modes instead of numeric. This version of the standard specifies the traditional encoding, in recognition that nearly 20 years after the first publication of this standard, numeric file modes are still in widespread use by application developers, and that all conforming implementations still use the traditional encoding. the tradition ate, honestly. ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
ok so looking ahead: no new S_IFMT symbolic names for the file type values of mode_t will be defined by POSIX.1โ2008. If new file types are required, they will only be testable through S_ISxx() or S_TYPEISxxx() macros instead. that's the plan, bestie, manifesting it. โจ
๐ธ SEE ALSO ๐ธ
<sys_statvfs.h><sys_types.h><time.h>
The System Interfaces volume of POSIX.1โ2017, chmod(), fchmod(), fstat(), fstatat(), futimens(), mkdir(), mkfifo(), mknod(), umask()
like go read those too, they're the group chat. ๐ซถ
๐ COPYRIGHT ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's a wrap on sys_stat.h(0P) bestie!! ๐ this lil beauty comes to you from IEEE/The Open Group, dated 2017 โ go forth and stat responsibly, mwah ๐โจ๐ฆ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms