Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <sys/stat.h>
int stat(const char *__restrict__ pathname, struct stat *__restrict__ info);
Gets status information about a specified file and places it in the area of memory pointed to by the info argument. The process does not need permissions on the file itself, but must have search permission on all directory components of the pathname. If the named file is a symbolic link, stat() resolves the symbolic link. It also returns information about the resulting file.
The information is returned as shown in the following stat structure table, as defined in the sys/stat.h header file.
Value | Description |
---|---|
mode_t st_mode | A bit string indicating the permissions and privileges of the Symbols are defined in the sys/stat.h header file to refer to bits in a mode_t value; these symbols are listed in chmod() — Change the mode of a file or directory. |
ino_t st_ino | The serial number of the file. |
dev_t st_dev | The numeric ID of the device containing the file. |
nlink_t st_nlink | The number of links to the file. |
uid_t st_uid | The numeric user ID (UID) of the file's owner. |
gid_t st_gid | The numeric group ID (GID) of the file's group. |
off_t st_size | For regular files, the file's size in bytes. For other kinds of files, the value of this field is unspecified. |
time_t st_atime | The most recent time the file was accessed. |
time_t st_ctime | The most recent time the status of the file was changed. |
time_t st_mtime | The most recent time the contents of the file were changed. |
Values for time_t_ are given in terms of seconds since epoch.
stat() updates the time-related fields before putting information in the stat structure.
If stat() successfully determines this information, it stores it in the area indicated by the info argument. The size of the buffer determines how much information is stored; data that exceeds the size of the buffer is truncated.
Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.
If successful, stat() returns 0.
⁄* CELEBS33
This example gets status information about a file.
*⁄
#define _POSIX_SOURCE
#include <sys⁄types.h>
#include <sys⁄stat.h>
#undef _POSIX_SOURCE
#include <stdio.h>
#include <time.h>
main() {
struct stat info;
if (stat("⁄", &info) != 0)
perror("stat() error");
else {
puts("stat() returned the following information about root f⁄s:");
printf(" inode: %d\n", (int) info.st_ino);
printf(" dev id: %d\n", (int) info.st_dev);
printf(" mode: %08x\n", info.st_mode);
printf(" links: %d\n", info.st_nlink);
printf(" uid: %d\n", (int) info.st_uid);
printf(" gid: %d\n", (int) info.st_gid);
printf("created: %s", ctime(&info.st_createtime));
}
}
stat() returned the following information about root f/s:
inode: 0
dev id: 1
mode: 010001ed
links: 11
uid: 0
gid: 500
created: Fri Jun 16 10:07:55 2001