home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / LIB / stat.lzh / STAT / teststat.c < prev   
C/C++ Source or Header  |  1991-08-21  |  2KB  |  121 lines

  1. /*
  2.     OS/9 stat/fstat test program
  3.     by Richard W.M. Jones
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <modes.h>
  8. #include <time.h>
  9.  
  10. #include "stat.h"
  11.  
  12. extern int errno;
  13.  
  14. main(argc,argv)
  15. int argc;
  16. char *argv[];
  17. {
  18.     register int i;
  19.  
  20.     for(i=0;i<3;++i)
  21.         do_fstat(i);
  22.     for(i=1;i<argc;++i)
  23.         do_stat(argv[i]);
  24. }
  25.  
  26. do_stat(pathname)
  27. register char *pathname;
  28. {
  29.     struct stat statbuf;
  30.  
  31.     printf("---------- stat on pathname %s\n",pathname);
  32.     if(stat(pathname,&statbuf)<0)
  33.     {
  34.         printf("stat failed on filename %s\n",pathname);
  35.         exit(errno);
  36.     }
  37.     else
  38.         printstat(&statbuf);
  39. }
  40.  
  41. do_fstat(path)
  42. register int path;
  43. {
  44.     struct stat statbuf;
  45.  
  46.     printf("---------- fstat on path %d\n",path);
  47.     if(fstat(path,&statbuf)<0)
  48.     {
  49.         printf("fstat failed on path %d\n",path);
  50.         exit(errno);
  51.     }
  52.     else
  53.         printstat(&statbuf);
  54. }
  55.  
  56. char *caltime(n)
  57. time_t n;
  58. {
  59.     char *s;
  60.  
  61.     if(n!=-1)
  62.     {
  63.         s=ctime(&n);
  64.         s[strlen(s)-1]=0;
  65.         return(s);
  66.     }
  67.     else
  68.         return("-");
  69. }
  70.  
  71. printstat(statbuf)
  72. register struct stat *statbuf;
  73. {
  74.     register  char *timestring;
  75.     printf("device no.: %d   inode no.: %d\n",statbuf->st_dev,
  76.         statbuf->st_ino);
  77.     printf("protection bits: %04X ",statbuf->st_mode);
  78.     decode_prots(statbuf->st_mode);
  79.     printf("gid: %d   uid: %d   size: %d   links: %d\n",statbuf->st_gid,
  80.         statbuf->st_uid, statbuf->st_size, statbuf->st_nlink);
  81.     timestring=caltime(statbuf->st_atime);
  82.     printf("last access: %d (%s)\n",statbuf->st_atime,timestring);
  83.     timestring=caltime(statbuf->st_mtime);
  84.     printf("last modify: %d (%s)\n",statbuf->st_mtime,timestring);
  85.     timestring=caltime(statbuf->st_ctime);
  86.     printf("creation   : %d (%s)\n",statbuf->st_ctime,timestring);
  87. }
  88.  
  89. decode_prots(protbits)
  90. register int protbits;
  91. {
  92.     printf("(");
  93.     if(protbits & S_IFIFO)
  94.         printf("pipe ");
  95.     if(protbits & S_IFCHR)
  96.         printf("chr-special ");
  97.     if(protbits & S_IFBLK)
  98.         printf("blk-special ");
  99.     if(protbits & S_IFREG)
  100.         printf("reg-file ");
  101.     if(protbits & S_IFSOCK)
  102.         printf("socket ");
  103.     if(protbits & S_IFDIR)
  104.         printf("directory ");
  105.     if(protbits & S_IFNOSHAR)
  106.         printf("non-shar ");
  107.     if(protbits & S_IROTH)
  108.         printf("PR ");
  109.     if(protbits & S_IWOTH)
  110.         printf("PW ");
  111.     if(protbits & S_IXOTH)
  112.         printf("PX ");
  113.     if(protbits & S_IREAD)
  114.         printf("R ");
  115.     if(protbits & S_IWRITE)
  116.         printf("W ");
  117.     if(protbits & S_IEXEC)
  118.         printf("X ");
  119.     printf(")\n");
  120. }
  121.