/*
 * burton rosenberg
 * march 2005
 *
 */

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h> /* for all sorts of types */
#include<time.h> /* for ctime(), asctime, localtime, struct tm */
#include<pwd.h> /* for struct passwd, getpwuid */

#define ALLOC_SS
#define USAGE "usage: %s filename\n"

int main(int argc, char * argv[]) {

/*
 * note two ways of passing arrays: caller alloc's, callee alloc's.
 * there has to be an agreement on who frees!
 */

   struct stat s_s, * s_s_p ;
   struct tm * tm_p ;
   struct passwd * passwd_struct_p ;
   char * time_str ;

   if ( argc!=2 ) {
      printf(USAGE, argv[0]) ;
      exit(0) ;
   }

   s_s_p = &s_s ;
#ifdef ALLOC_SS
   s_s_p = (struct stat *) malloc(sizeof(struct stat)) ; 
#endif

   if( stat(argv[1], s_s_p ) ) {
      printf("stat failed for filename %s\n", argv[1]) ;
      exit(0) ;
   }

   tm_p = localtime( &s_s_p->st_atime ) ;
   time_str = asctime(tm_p) ;
   passwd_struct_p = getpwuid(s_s_p->st_uid) ;

   printf("inode: %d\nuser: %d, %s\na-time: %d, %s\n", 
      s_s_p->st_ino, s_s_p->st_uid, 
      passwd_struct_p->pw_name,
      s_s_p->st_atime, time_str ) ;

#ifdef ALLOC_SS
   free(s_s_p) ;
#endif

}

