home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / disk / cdrom / mkisofs / unix / stat.c < prev    next >
C/C++ Source or Header  |  1994-11-18  |  2KB  |  107 lines

  1. /* stat.c: */
  2.  
  3. #include <clib/dos_protos.h>
  4. #include <clib/exec_protos.h>
  5. #include <exec/memory.h>
  6. #include <errno.h>
  7. #include "unixlib.h"
  8.  
  9. #ifdef LATTICE
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #endif
  13.  
  14. static time_t cvt_date (struct DateStamp *p_date)
  15. {
  16.   unsigned long t = p_date->ds_Tick / TICKS_PER_SECOND +
  17.                   p_date->ds_Minute * 60 +
  18.                 p_date->ds_Days * 60 * 60 * 24;
  19.  
  20.   t += (8 * 365 + 2) * 24 * 60 * 60;
  21.   return (time_t) t;
  22. }
  23.  
  24. int stat (const char *p_name, struct stat *p_stat)
  25. {
  26.   BPTR lock;
  27.   struct FileInfoBlock *fib;
  28.   
  29.   lock = Lock ((UBYTE *) p_name, ACCESS_READ);
  30.   if (!lock) {
  31.     errno = ENOENT;
  32.     return -1;
  33.   }
  34.  
  35.   fib = AllocMem ((ULONG) sizeof (*fib), 0);
  36.   if (!fib) {
  37.     UnLock (lock);
  38.     return -1;
  39.   }
  40.  
  41.   if (!Examine (lock, fib)) {
  42.     UnLock (lock);
  43.     FreeMem (fib, sizeof (*fib));
  44.     return -1;  
  45.   }
  46.  
  47.   p_stat->st_dev = 0;
  48.   p_stat->st_ino = 0;
  49.   p_stat->st_mode = 0;
  50.   if (!(fib->fib_Protection & FIBF_READ))
  51.     p_stat->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
  52.   if (!(fib->fib_Protection & FIBF_WRITE))
  53.     p_stat->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  54.   if (!(fib->fib_Protection & FIBF_EXECUTE))
  55.     p_stat->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  56.  
  57.   if (fib->fib_DirEntryType < 0)
  58.     p_stat->st_mode |= S_IFREG;
  59.   else {
  60.     p_stat->st_mode |= S_IFDIR;
  61.     p_stat->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  62.   }
  63.   p_stat->st_nlink = 0;
  64.   p_stat->st_uid = 1;
  65.   p_stat->st_gid = 1;
  66.   p_stat->st_rdev = 0;
  67.   p_stat->st_size = fib->fib_Size;
  68.   p_stat->st_atime = cvt_date (&(fib->fib_Date));
  69.   p_stat->st_mtime = cvt_date (&(fib->fib_Date));
  70.   p_stat->st_ctime = cvt_date (&(fib->fib_Date));
  71.  
  72.  
  73.   FreeMem (fib, sizeof (*fib));
  74.   
  75.   UnLock (lock);
  76.   
  77.   return 0;
  78. }
  79.  
  80. int lstat (const char *p_name, struct stat *p_stat)
  81. {
  82.   return stat (p_name, p_stat);
  83. }
  84.  
  85. #ifdef LATTICE
  86. int access (const char *p_path, int p_modus)
  87. #else
  88. int access (char *p_path, int p_modus)
  89. #endif
  90. {
  91.   struct stat sbuf;
  92.  
  93.   if (stat (p_path, &sbuf) < 0)
  94.     return -1;
  95.  
  96.   if ((p_modus & R_OK) && !(sbuf.st_mode & S_IRUSR))
  97.     return -1;
  98.  
  99.   if ((p_modus & W_OK) && !(sbuf.st_mode & S_IWUSR))
  100.     return -1;
  101.  
  102.   if ((p_modus & X_OK) && !(sbuf.st_mode & S_IXUSR))
  103.     return -1;
  104.  
  105.   return 0;
  106. }
  107.