Stat (system call)
![]() stat command line
For example, the
Since at least 2004, the same-named shell command FunctionsThe C POSIX library header sys/stat.h, found on POSIX and other Unix-like operating systems, declares int stat(const char* path, struct stat* buf);
int lstat(const char* path, struct stat* buf);
int fstat(int filedesc, struct stat* buf);
Each function accepts a pointer to a The The library has been extended to support large files. Functions Data structureThe metadata structure is defined in the sys/stat.h header. The following shows the base fields, but an implementation is free to include additional fields:[3] struct stat {
mode_t st_mode;
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
blksize_t st_blksize;
blkcnt_t st_blocks;
};
POSIX.1 does not require In older versions of POSIX.1 standard, the time-related fields were defined as Fields include:
Example
The following C program reports metadata about each file passed via the command-line – using stat() to query the system for the information. #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char* argv[]) {
struct stat sb;
for (int i = 1; i < argc; i++) {
if (stat(argv[i], &sb) == -1) {
perror("stat failed");
exit(EXIT_FAILURE);
}
printf("%s:\n", argv[i]);
printf("\tinode: %u\n", sb.st_ino);
printf("\tperms: %o\n", sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
printf("\tlinks: %d\n", sb.st_nlink);
printf("\tsize: %ld\n", sb.st_size);
printf("\tatime: %s", ctime(&sb.st_atim.tv_sec));
printf("\tmtime: %s", ctime(&sb.st_mtim.tv_sec));
printf("\tctime: %s", ctime(&sb.st_ctim.tv_sec));
printf("\n");
}
return 0;
}
References
External links
|