lstat - get symbolic link status
#include <sys/stat.h>
int lstat(const char *restrict path, struct stat *restrict buf);
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.
For symbolic links, the st_mode member shall contain meaningful information when used with the file type macros, and the st_size member shall contain the length of the pathname contained in the symbolic link. File mode bits and the contents of the remaining members of the stat structure are unspecified. The value returned in the st_size member is the length of the contents of the symbolic link, and does not count any trailing null.
Upon successful completion, lstat() shall return 0. Otherwise, it shall return -1 and set errno to indicate the error.
The lstat() function shall fail if:
- [EACCES]
- A component of the path prefix denies search permission.
- [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 pathname exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
- [ENOTDIR]
- A component of the path prefix is not a directory.
- [ENOENT]
- A component of path does not name an existing file or path is an empty string.
- [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 lstat() function may fail if:
- [ELOOP]
- More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
- [ENAMETOOLONG]
- As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname string exceeded {PATH_MAX}.
- [EOVERFLOW]
- One of the members is too large to store into the structure pointed to by the buf argument.
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 filename 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);
None.
The lstat() function is not required to update the time-related fields if the named file is not a symbolic link. While the st_uid, st_gid, st_atime, st_mtime, and st_ctime members of the stat structure may apply to a symbolic link, they are not required to do so. No functions in IEEE Std 1003.1-2001 are required to maintain any of these time fields.
None.
fstat(), readlink(), stat() , symlink(), the Base Definitions volume of IEEE Std 1003.1-2001, <sys/stat.h>
First released in Issue 4, Version 2.
Moved from X/OPEN UNIX extension to BASE.
Large File Summit extensions are added.
The following changes were made to align with the IEEE P1003.1a draft standard:
This function is now mandatory.
The [ELOOP] optional error condition is added.
The restrict keyword is added to the lstat() prototype for alignment with the ISO/IEC 9899:1999 standard.