home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / unixlib / src / fibstat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  2.5 KB  |  91 lines

  1. #include "amiga.h"
  2. #include "fibstat.h"
  3. #include "timeconvert.h"
  4. #include <fcntl.h>
  5. #include <time.h>
  6. #include <sys/stat.h>
  7. #include <utility/tagitem.h>
  8.  
  9. char _temp_fname[FNAMESIZE];
  10.  
  11. void _lfibstat(char *name, struct FileInfoBlock *fib, struct MsgPort *task,
  12.            int isroot, struct stat *sbuf)
  13. {
  14.   long protection = fib->fib_Protection;
  15.   
  16.   sbuf->st_dev = (long)task;
  17.   sbuf->st_rdev = 0;
  18.   sbuf->st_uid = AMIGA_UID; sbuf->st_gid = AMIGA_GID;
  19.   sbuf->st_blksize = 512;
  20. #ifdef PRETEND_LINKED
  21.   /* This forces programs (tar) to consider potential hard links */
  22.   sbuf->st_nlink = 2;
  23. #else
  24.   sbuf->st_nlink = 1;
  25. #endif
  26.   sbuf->st_blocks = fib->fib_NumBlocks;
  27.   /* Give directories an arbitrary size */
  28.   if (fib->fib_Size == 0 && fib->fib_DirEntryType > 0) sbuf->st_size = 2048;
  29.   else sbuf->st_size = fib->fib_Size;
  30.   sbuf->st_ino = fib->fib_DiskKey;
  31.   sbuf->st_ctime = sbuf->st_atime = sbuf->st_mtime = _amiga2gmt(&fib->fib_Date);
  32.   
  33.   switch (fib->fib_DirEntryType)
  34.     {
  35.     case ST_SOFTLINK:
  36.       {
  37.     int len;
  38.  
  39.     if (name && (len = readlink(name, _temp_fname, FNAMESIZE - 1)) > 0)
  40.       sbuf->st_size = len;
  41.     else sbuf->st_size = 256; /* A random safish value */
  42.     sbuf->st_mode = S_IFLNK;
  43.     break;
  44.       }
  45.     case ST_PIPEFILE: sbuf->st_mode = S_IFIFO; break;
  46.       /* If Examine wasn't braindead this would be the right test */
  47.     case ST_ROOT: sbuf->st_mode = S_IFDIR; protection = 0; break;
  48.     case ST_FILE: /* Try & detect special files (eg windows) */
  49.       if (fib->fib_DiskKey == 0 && !fib->fib_FileName[0])
  50.     sbuf->st_mode = S_IFCHR;
  51.       else sbuf->st_mode = S_IFREG;
  52.       break;
  53.     default: sbuf->st_mode = fib->fib_DirEntryType > 0 ? S_IFDIR : S_IFREG; break;
  54.     }
  55.   /* Examine is braindead. You can't tell if you've examined a root directory
  56.      (for which the protection flags are invalid) or not. */
  57.   if (isroot) protection = 0;
  58.  
  59.   sbuf->st_mode |= _make_mode(protection);
  60. }
  61.  
  62. int _fibstat(char *name, struct stat *sbuf)
  63. {
  64.   int ret;
  65.   struct FileInfoBlock *fib;
  66.   BPTR lock = 0;
  67.  
  68.   if ((fib = AllocDosObjectTags(DOS_FIB, TAG_END)) &&
  69.       (lock = Lock(name, ACCESS_READ)) &&
  70.       Examine(lock, fib))
  71.     {
  72.       BPTR parent = ParentDir(lock);
  73.       int isroot = !parent;
  74.       struct FileLock *flock = BADDR(lock);
  75.       
  76.       if (parent) UnLock(parent);
  77.       _lfibstat(name, fib, flock->fl_Task, isroot, sbuf);
  78.       ret = 0;
  79.     }
  80.   else
  81.     {
  82.       ret = -1;
  83.       errno = convert_oserr(IoErr());
  84.     }
  85.   if (lock) UnLock(lock);
  86.   if (fib) FreeDosObject(DOS_FIB, fib);
  87.   return ret;
  88. }
  89.  
  90.       
  91.