The man page for stat() lists the prototype and how to use it.
Code:
// I use Solaris 8 - may be different
#include <sys/types.h>
#include <sys/stat.h>
int stat ( const char *path, struct stat *buf );
// directories leading to file in path must be searchable.
mode_t st_mode; /* File mode (see mknod(2)) */
ino_t st_ino; /* Inode number */
dev_t st_dev; /* ID of device containing */
/* a directory entry for this file */
dev_t st_rdev; /* ID of device */
/* This entry is defined only for */
/* char special or block special files */
nlink_t st_nlink; /* Number of links */
uid_t st_uid; /* User ID of the file's owner */
gid_t st_gid; /* Group ID of the file's group */
off_t st_size; /* File size in bytes */
time_t st_atime; /* Time of last access */
time_t st_mtime; /* Time of last data modification */
time_t st_ctime; /* Time of last file status change */
/* Times measured in seconds since */
/* 00:00:00 UTC, Jan. 1, 1970 */
long st_blksize; /* Preferred I/O block size */
blkcnt_t st_blocks; /* Number of 512 byte blocks allocated*/
Here is an example of how the stat function can be used:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main ( int argc, char *argv[])
{
struct stat FileAttrib;
if (argc != 2)
printf("Usage: <executable> <file>\n");
else
{
if (stat(argv[(argc - 1)], &FileAttrib) < 0)
printf("File Error Message = %s\n", strerror(errno));
else
{
tm *pTm = gmtime(&FileAttrib.st_atime);
printf("File last accessed on: %.2i/%.2i/%.2i at %.2i:%.2i:%.2i GMT\n",
(pTm->tm_mon + 1),
pTm->tm_mday,
(pTm->tm_year % 100),
pTm->tm_hour,
pTm->tm_min,
pTm->tm_sec);
}
}
return 0;
}
David