File Handling · intermediate · ~10 min
Inspect file metadata with stat.
int stat(const char *path, struct stat *out) fills a struct with size, type (regular/dir/symlink), mtime, and POSIX permission bits.
Use it before opening to validate ownership or mode; combine with S_IRUSR, S_IWOTH, etc. masks.
struct stat st;
if (stat(path, &st) == 0) {
if (S_ISREG(st.st_mode)) printf("regular file\n");
if (st.st_mode & S_IWOTH) printf("WORLD-WRITABLE!\n");
}
stat (follows symlinks) with lstat (doesn't).