#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
struct STAT sb;
struct TM *ptr;
if (argc! = 2)
{
fprintf (stderr, "Usage:%s <pathname>\n", argv[0]);
Exit (Exit_failure);
}
if (stat (argv[1], &sb) = =-1)
{
Perror ("stat");
Exit (exit_success);
}
printf ("File type:");
Switch (Sb.st_mode & s_ifmt) {
Case s_ifblk:printf ("Block device\n"); Break
Case s_ifchr:printf ("character device\n"); Break
Case s_ifdir:printf ("directory\n"); Break
Case s_ififo:printf ("fifo/pipe\n"); Break
Case s_iflnk:printf ("symlink\n"); Break
Case s_ifreg:printf ("regular file\n"); Break
Case s_ifsock:printf ("socket\n"); Break
default:printf ("unknown?\n"); Break
}
printf ("I-node Number:%ld\n", (long) Sb.st_ino);
printf ("Mode:%lo (octal) \ n", (unsigned long) sb.st_mode);
printf ("Link count:%ld\n", (long) sb.st_nlink);
printf ("Ownership:uid=%ld gid=%ld\n",
(long) Sb.st_uid, (long) sb.st_gid);
printf ("Preferred I/O block size:%ld bytes\n", (long) sb.st_blksize);
printf ("File Size:%lld bytes\n", (Long Long) sb.st_size);
printf ("Blocks Allocated:%lld\n", (Long Long) sb.st_blocks);
printf ("Last Status change:%s", CTime (&sb.st_ctime));
printf ("Last File access:%s", CTime (&sb.st_atime));
printf ("Last File Modification:%s", CTime (&sb.st_mtime));
Exit (exit_success);
}
All information about the file can be obtained through this structure.
There is no file time created for Linux files
Linux file contains three times
Atime Last access time
CTime Last Change attribute time
Mtime Last Modified Time
The following is a detailed information about struct STAT {
dev_t St_dev; Device number of the file
ino_t St_ino; Node
mode_t St_mode; Type of file and permissions to access
nlink_t St_nlink; The number of hard connections to the file, the file value that was just created is 1
uid_t St_uid; User ID
gid_t St_gid; Group ID
dev_t St_rdev; (device type) If this file is a device file, its device number
off_t st_size; Number of File bytes (file size)
unsigned long st_blksize; Block Size (file system I/o buffer size)
unsigned long st_blocks; Number of blocks
time_t St_atime; Last Access time
time_t St_mtime; Last modification time
time_t St_ctime; Last change time (refers to attribute)
};
Man 2 stat
Linux struct STAT file structure information