home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / disk / mkisofs-1.00.5.lha / mkisofs / unix / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  2.1 KB  |  105 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_nlink = 0;
  62.   p_stat->st_uid = 1;
  63.   p_stat->st_gid = 1;
  64.   p_stat->st_rdev = 0;
  65.   p_stat->st_size = fib->fib_Size;
  66.   p_stat->st_atime = cvt_date (&(fib->fib_Date));
  67.   p_stat->st_mtime = cvt_date (&(fib->fib_Date));
  68.   p_stat->st_ctime = cvt_date (&(fib->fib_Date));
  69.  
  70.  
  71.   FreeMem (fib, sizeof (*fib));
  72.   
  73.   UnLock (lock);
  74.   
  75.   return 0;
  76. }
  77.  
  78. int lstat (const char *p_name, struct stat *p_stat)
  79. {
  80.   return stat (p_name, p_stat);
  81. }
  82.  
  83. #ifdef LATTICE
  84. int access (const char *p_path, int p_modus)
  85. #else
  86. int access (char *p_path, int p_modus)
  87. #endif
  88. {
  89.   struct stat sbuf;
  90.  
  91.   if (stat (p_path, &sbuf) < 0)
  92.     return -1;
  93.  
  94.   if ((p_modus & R_OK) && !(sbuf.st_mode & S_IRUSR))
  95.     return -1;
  96.  
  97.   if ((p_modus & W_OK) && !(sbuf.st_mode & S_IWUSR))
  98.     return -1;
  99.  
  100.   if ((p_modus & X_OK) && !(sbuf.st_mode & S_IXUSR))
  101.     return -1;
  102.  
  103.   return 0;
  104. }
  105.