FSTATAT(3P)
๐ FSTATAT(3P) โ POSIX Programmer's Manual ๐
okay bestie, welcome to the POSIX Programmer's Manual page for fstatat, lstat, and stat. this is the โจgroup chatโจ version. every single fact is still here, i promise on my life. let's get into it ๐ซถ
๐ธ PROLOG ๐ธ
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.
ngl that last part is giving "it might not even exist on your machine" energy ๐ so check before you commit, bestie.
โจ NAME โจ
fstatat, lstat, stat โ get file status. that's it. that's the vibe. ๐
๐ฆ SYNOPSIS ๐ฆ
#include <fcntl.h>
#include <sys/stat.h>
int fstatat(int fd, const char *restrict path,
struct stat *restrict buf, int flag);
int lstat(const char *restrict path, struct stat *restrict buf);
int stat(const char *restrict path, struct stat *restrict buf);
three functions, one goal: know what's up with a file. iconic behavior. ๐
๐ DESCRIPTION ๐
The stat() function shall obtain information about the named file and write it to the area pointed to by the buf argument. The path argument points to a pathname naming a file. Read, write, or execute permission of the named file is not required. (yes, you read that right โ no permission slip needed for this field trip ๐)
An implementation that provides additional or alternate file access control mechanisms may, under implementation-defined conditions, cause stat() to fail. In particular, the system may deny the existence of the file specified by path. like, it can literally just pretend your file doesn't exist?? the audacity ๐
If the named file is a symbolic link, the stat() function shall continue pathname resolution using the contents of the symbolic link, and shall return information pertaining to the resulting file if the file exists.
The buf argument is a pointer to a stat structure, as defined in the <sys/stat.h> header, into which information is placed concerning the file.
The stat() function shall update any time-related fields (as described in the Base Definitions volume of POSIX.1โ2017, Section 4.9, File Times Update), before writing into the stat structure.
If the named file is a shared memory object, the implementation shall update in the stat structure pointed to by the buf argument the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.
If the named file is a typed memory object, the implementation shall update in the stat structure pointed to by the buf argument the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.
For all other file types defined in this volume of POSIX.1โ2017, the structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atim, st_ctim, and st_mtim shall have meaningful values and the value of the member st_nlink shall be set to the number of links to the file.
The lstat() function shall be equivalent to stat(), except when path refers to a symbolic link. In that case lstat() shall return information about the link, while stat() shall return information about the file the link references. ๐ช like looking in a mirror vs looking at what the mirror reflects. both valid, different tea.
For symbolic links, the st_mode member shall contain meaningful information when used with the file type macros. The file mode bits in st_mode are unspecified. The structure members st_ino, st_dev, st_uid, st_gid, st_atim, st_ctim, and st_mtim shall have meaningful values and the value of the st_nlink member shall be set to the number of (hard) links to the symbolic link. The value of the st_size member shall be set to the length of the pathname contained in the symbolic link not including any terminating null byte.
The fstatat() function shall be equivalent to the stat() or lstat() function, depending on the value of flag (see below), except in the case where path specifies a relative path. In this case the status shall be retrieved from a file relative to the directory associated with the file descriptor fd instead of the current working directory.
If the access mode of the open file description associated with the file descriptor is not O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the access mode is O_SEARCH, the function shall not perform the check. (skipping the vibe check when you already have the pass, lowkey smart ๐)
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
AT_SYMLINK_NOFOLLOWโ If path names a symbolic link, the status of the symbolic link is returned. like "don't follow the link, bestie, i want the link itself" ๐ฆ
If fstatat() is passed the special value AT_FDCWD in the fd parameter, the current working directory shall be used and the behavior shall be identical to a call to stat() or lstat() respectively, depending on whether or not the AT_SYMLINK_NOFOLLOW bit is set in flag.
๐ฅ RETURN VALUE ๐ฅ
Upon successful completion, these functions shall return 0. Otherwise, these functions shall return -1 and set errno to indicate the error. classic 0-or-cry situation ๐ญ
๐ ERRORS ๐
These functions shall fail if:
EACCESโ Search permission is denied for a component of the path prefix.EIOโ An error occurred while reading from the file system.ELOOPโ A loop exists in symbolic links encountered during resolution of the path argument.ENAMETOOLONGโ The length of a component of a pathname is longer than{NAME_MAX}.ENOENTโ A component of path does not name an existing file or path is an empty string.ENOTDIRโ A component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or the path argument contains at least one non-<slash>character and ends with one or more trailing<slash>characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.EOVERFLOWโ The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the structure pointed to by buf.
The fstatat() function shall fail if:
EACCESโ The access mode of the open file description associated with fd is notO_SEARCHand the permissions of the directory underlying fd do not permit directory searches.EBADFโ The path argument does not specify an absolute path and the fd argument is neitherAT_FDCWDnor a valid file descriptor open for reading or searching.ENOTDIRโ The path argument is not an absolute path and fd is a file descriptor associated with a non-directory file.
These functions may fail if:
ELOOPโ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of the path argument.ENAMETOOLONGโ The length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}.EOVERFLOWโ A value to be stored would overflow one of the members of thestatstructure.
The fstatat() function may fail if:
EINVALโ The value of the flag argument is not valid.
bestie, "shall fail" vs "may fail" is a real distinction โ one is a promise, one is a maybe. don't conflate them ๐
The following sections are informative.
๐ EXAMPLES ๐
๐ Obtaining File Status Information ๐
The following example shows how to obtain file status information for a file named /home/cnd/mod1. The structure variable buffer is defined for the stat structure.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct stat buffer;
int status;
...
status = stat("/home/cnd/mod1", &buffer);
slay, one call, all the tea โ
๐ฆ Getting Directory Information ๐ฆ
The following example fragment gets status information for each entry in a directory. The call to the stat() function stores file information in the stat structure pointed to by statbuf. The lines that follow the stat() call format the fields in the stat structure for presentation to the user of the program.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <stdint.h>
struct dirent *dp;
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
struct tm *tm;
char datestring[256];
...
/* Loop through directory entries. */
while ((dp = readdir(dir)) != NULL) {
/* Get entry's information. */
if (stat(dp->d_name, &statbuf) == -1)
continue;
/* Print out type, permissions, and number of links. */
printf("%10.10s", sperm (statbuf.st_mode));
printf("%4d", statbuf.st_nlink);
/* Print out owner's name if it is found using getpwuid(). */
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %-8.8s", pwd->pw_name);
else
printf(" %-8d", statbuf.st_uid);
/* Print out group name if it is found using getgrgid(). */
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp->gr_name);
else
printf(" %-8d", statbuf.st_gid);
/* Print size of file. */
printf(" %9jd", (intmax_t)statbuf.st_size);
tm = localtime(&statbuf.st_mtime);
/* Get localized date string. */
strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
printf(" %s %s\n", datestring, dp->d_name);
}
it's giving "ls -l but make it your own" โจ
๐ Obtaining Symbolic Link Status Information ๐
The following example shows how to obtain status information for a symbolic link named /modules/pass1. The structure variable buffer is defined for the stat structure. If the path argument specified the pathname for the file pointed to by the symbolic link (/home/cnd/mod1), the results of calling the function would be the same as those returned by a call to the stat() function.
#include <sys/stat.h>
struct stat buffer;
int status;
...
status = lstat("/modules/pass1", &buffer);
see the link, not the linked โ lstat() said "i'm different" ๐
๐ซถ APPLICATION USAGE ๐ซถ
None.
okay that's part 1 bestie, hold on, there's more coming โจ
๐ญ RATIONALE ๐ญ
Okay so bestie, the whole intent of that paragraph talking about โadditional or alternate file access control mechanismsโ โ yes, keep that exact phrase in your head โ is to allow a secure implementation where a process with a label that does not dominate the fileโs label cannot perform a stat() function. Itโs giving security model realness. And no, this is not related to read permission. A process with a label that dominates the fileโs label does not need read permission. Also, an implementation that supports write-up operations could fail fstat() function calls even though it has a valid file descriptor open for writing. Thatโs the plot twist. ๐
The purpose of the fstatat() function is to obtain the status of files in directories other than the current working directory without exposure to race conditions. Because race conditions are the villain era, fr fr. Any part of the path of a file could be changed in parallel to a call to stat(), resulting in unspecified behavior. By opening a file descriptor for the target directory and using the fstatat() function it can be guaranteed that the file for which status is returned is located relative to the desired directory. Iconic, stable, no chaos. โจ
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
Literally none. Nada. Zilch. The future is a blank canvas and this page is not painting on it. ๐
๐ SEE ALSO ๐
access()chmod()fdopendir()fstat()mknod()readlink()symlink()
The Base Definitions volume of POSIX.1โ2017, Section 4.9, File Times Update, <fcntl.h>, <sys_stat.h>, <sys_types.h>
Go read those if you want the full lore, bestie. Iykyk. ๐ฆ
๐ 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 .
Not me getting all legal on main, but the details matter and we keep every single one. ๐
This page is FSTATAT(3P), source/version IEEE/The Open Group, date 2017. Slay, you made it to the end. ๐โจ
๐ฆ 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