home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / unz50p1 / AMIGA / stat.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  3KB  |  127 lines

  1. /* stat.c -- for Lattice 4.01 */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/exec.h>
  5. #include <libraries/dos.h>
  6. #include <libraries/dosextens.h>
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9.  
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12.  
  13. /* I can't find the defines for DirEntryType or EntryType... */
  14. #define DOSDIR  (2L)
  15. #define DOSFILE (-3L)   /* actually, < 0 */
  16.  
  17. #ifndef SUCCESS
  18. #define SUCCESS (-1)
  19. #define FAILURE (0)
  20. #endif
  21.  
  22. extern int stat(char *file,struct stat *buf);
  23.  
  24. stat(file,buf)
  25. char *file;
  26. struct stat *buf;
  27. {
  28.  
  29.         struct FileInfoBlock *inf;
  30.         struct FileLock *lock;
  31.         long ftime;
  32.  
  33.         if( (lock = (struct FileLock *)Lock(file,SHARED_LOCK))==0 )
  34.                 /* file not found */
  35.                 return(-1);
  36.  
  37.         if( !(inf = (struct FileInfoBlock *)AllocMem(
  38.                 (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
  39.         {
  40.                 UnLock((BPTR)lock);
  41.                 return(-1);
  42.         }
  43.  
  44.         if( Examine((BPTR)lock,inf)==FAILURE )
  45.         {
  46.                 FreeMem((char *)inf,(long)sizeof(*inf));
  47.                 UnLock((BPTR)lock);
  48.                 return(-1);
  49.         }
  50.  
  51.         /* fill in buf */
  52.  
  53.         buf->st_dev                =
  54.         buf->st_nlink        =
  55.         buf->st_uid                =
  56.         buf->st_gid                =
  57.         buf->st_rdev        = 0;
  58.         
  59.         buf->st_ino                = inf->fib_DiskKey;
  60.         buf->st_blocks        = inf->fib_NumBlocks;
  61.         buf->st_size        = inf->fib_Size;
  62.         buf->st_blksize        = 512;
  63.  
  64.         /* now the date.  AmigaDOG has weird datestamps---
  65.          *      ds_Days is the number of days since 1-1-1978;
  66.          *      however, as Unix wants date since 1-1-1970...
  67.          */
  68.  
  69.         ftime =
  70.                 (inf->fib_Date.ds_Days * 86400 )                +
  71.                 (inf->fib_Date.ds_Minute * 60 )                 +
  72.                 (inf->fib_Date.ds_Tick / TICKS_PER_SECOND )     +
  73.                 (86400 * 8 * 365 )                              +
  74.                 (86400 * 2 );  /* two leap years, I think */
  75.  
  76. /*  ftime += timezone;  */
  77.  
  78.         buf->st_ctime =
  79.         buf->st_atime =
  80.         buf->st_mtime =
  81.         buf->st_mtime = ftime;
  82.  
  83.         switch( inf->fib_DirEntryType )
  84.         {
  85.         case DOSDIR:
  86.                 buf->st_mode = S_IFDIR;
  87.                 break;
  88.  
  89.         case DOSFILE:
  90.                 buf->st_mode = S_IFREG;
  91.                 break;
  92.  
  93.         default:
  94.                 buf->st_mode = S_IFDIR | S_IFREG;
  95.                 /* an impossible combination?? */
  96.         }
  97.  
  98.         /* lastly, throw in the protection bits */
  99.  
  100.         if((inf->fib_Protection & FIBF_READ) == 0)
  101.                 buf->st_mode |= S_IREAD;
  102.  
  103.         if((inf->fib_Protection & FIBF_WRITE) == 0)
  104.                 buf->st_mode |= S_IWRITE;
  105.  
  106.         if((inf->fib_Protection & FIBF_EXECUTE) == 0)
  107.                 buf->st_mode |= S_IEXECUTE;
  108.  
  109.         if((inf->fib_Protection & FIBF_DELETE) == 0)
  110.                 buf->st_mode |= S_IDELETE;
  111.  
  112.         if((inf->fib_Protection & (long)FIBF_ARCHIVE))
  113.                 buf->st_mode |= S_IARCHIVE;
  114.  
  115.         if((inf->fib_Protection & (long)FIBF_PURE))
  116.                 buf->st_mode |= S_IPURE;
  117.  
  118.         if((inf->fib_Protection & (long)FIBF_SCRIPT))
  119.                 buf->st_mode |= S_ISCRIPT;
  120.  
  121.         FreeMem((char *)inf, (long)sizeof(*inf));
  122.         UnLock((BPTR)lock);
  123.  
  124.         return(0);
  125.  
  126. }
  127.