stat() example

Script started on Fri May 02 12:54:12 1997
sh-2.00$ cat statfile.c
/* statfile - get file status using stat(path, buf) system call.
   Synopsis:
      include files:  <sys/types.h>
                      <sys/stat.h>

     int stat(path, buf)
     char * path;
     struct stat * buf;

From /usr/include/sys/stat.h, we have the following description of the
stat structure, used by stat and fstat.

struct  stat
{
	dev_t	st_dev;			 ID of device containing a directory
					   entry for this file.  File serial
					   no + device ID uniquely identify 
					   the file within the system 
	ino_t	st_ino;			 File serial number 
	mode_t	st_mode;		 File mode; see #define's below 
	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 
	dev_t	st_rdev;		 ID of device 
					   This entry is defined only for 
					   character or block special files 
	off_t	st_size;		 File size in bytes 
	time_t	st_atime;		 Time of last access 
	int	st_spare1;
	time_t	st_mtime;		 Time of last data modification 
	int	st_spare2;
	time_t	st_ctime;		 Time of last file status change 
	int	st_spare3;
					 Time measured in seconds since 
					   00:00:00 GMT, Jan. 1, 1970 
	uint_t	st_blksize;		 Size of block in file 
        int    st_blocks;                # blocks allocated for file 
        uint_t  st_flags;                user defined flags for file 
        uint_t  st_gen;                  file generation number 

};

*****************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>

void report(char *, struct stat *);

void main(int argc, char * argv[])
/* Print status of files on argument list. */
{
   struct stat status_buf;
   if (argc < 2)
   {
      fprintf(stderr, "statfile file1 ...\n");
      exit(-1);
   }
   while(--argc)
   {
      if( stat(*++argv, &status_buf) < 0 )
         perror(*argv);
      else
         report(*argv, &status_buf);
   }
}

/* Possible access modes... one for each octal value. */
char * accesses[] = {"...", "..x", ".w.", ".wx", "r..", "r.x", "rw.", "rwx"};

void report( char * name, struct stat * buffer)
/* Decode and present the status information. */
{
   int i;
   struct passwd * passent;
   ushort mode = buffer -> st_mode;
   printf("\n %s :\n", name);
   printf(" Last access : %s\n", ctime( &(buffer -> st_atime)) );
   printf(" Last modification : %s\n", ctime( &(buffer -> st_mtime)));
   printf(" Last status change : %s\n", ctime( &(buffer -> st_ctime)));
   printf(" Current file size : %ld \n", buffer -> st_size);

   /* type dev_t is int*/
   printf(" Directory entry is on device %d.\n", buffer -> st_dev);

   /* type ino_t is unsigned  */
   printf(" Inode number is %lu\n", buffer -> st_ino);

   /* Identify the owner by number and by name. */
   passent = getpwuid(buffer -> st_uid);
   if(passent != NULL)
      printf("The owner of the file is #%d - %s\n",
          buffer -> st_uid, passent -> pw_name);
   else
      printf(" The owner of the file is #%d - unknown\n", buffer -> st_uid);

   printf(" Access mode 0%o: ", mode);
   for(i = 6; i >= 0; i -=3)
      printf("%s", accesses[(mode >> i) & 7]);
   printf("\n");
}


sh-2.00$ statfile pi*

 pipe3a.c :
 Last access : Wed Apr 30 08:09:06 1997

 Last modification : Sat Apr  1 07:53:41 1995

 Last status change : Wed Jan 29 10:05:55 1997

 Current file size : 10046 
 Directory entry is on device 8393735.
 Inode number is 59931
The owner of the file is #20 - rossa
 Access mode 0100640: rw.r.....

 pipe3b.c :
 Last access : Wed Apr 30 08:09:06 1997

 Last modification : Sat Apr  1 11:47:31 1995

 Last status change : Wed Jan 29 10:05:55 1997

 Current file size : 10838 
 Directory entry is on device 8393735.
 Inode number is 59932
The owner of the file is #20 - rossa
 Access mode 0100640: rw.r.....

 pipeblock.c :
 Last access : Wed Apr 30 08:09:06 1997

 Last modification : Thu Apr  6 08:20:33 1995

 Last status change : Wed Jan 29 10:05:55 1997

 Current file size : 641 
 Directory entry is on device 8393735.
 Inode number is 59935
The owner of the file is #20 - rossa
 Access mode 0100640: rw.r.....

 pipes.ex :
 Last access : Wed Apr 30 08:09:06 1997

 Last modification : Thu Apr  6 12:32:15 1995

 Last status change : Wed Jan 29 10:05:55 1997

 Current file size : 3707 
 Directory entry is on device 8393735.
 Inode number is 59937
The owner of the file is #20 - rossa
 Access mode 0100640: rw.r.....
sh-2.00$ exit
exit

script done on Fri May 02 12:54:40 1997