home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / unixlib36d / UnixLib36d / src / unix / c / stat < prev    next >
Encoding:
Text File  |  1994-03-08  |  2.3 KB  |  133 lines

  1. static char sccs_id[] = "@(#) stat.c 1.2 " __DATE__ " HJR";
  2.  
  3. /* stat.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <errno.h>
  6. #include <time.h>
  7.  
  8. #include "fcntl.h"
  9.  
  10. #include "sys/types.h"
  11. #include "sys/dev.h"
  12. #include "sys/unix.h"
  13. #include "sys/stat.h"
  14. #include "sys/os.h"
  15.  
  16. static void __stat (register int *, register struct stat *);
  17.  
  18. int
  19. stat (char *file, register struct stat *buf)
  20. {
  21.   int r[6];
  22.   os_error *e;
  23.  
  24.   if (!buf)
  25.     {
  26.       errno = EINVAL;
  27.       return (-1);
  28.     }
  29.  
  30.   file = __uname (file, 0);
  31.  
  32.   if (e = os_file (0x05, file, r))
  33.     {
  34.       __seterr (e);
  35.       return (-1);
  36.     }
  37.   if (!r[0])
  38.     {
  39.       errno = ENOENT;
  40.       return (-1);
  41.     }
  42.  
  43.   buf->st_dev = makedev (DEV_RISCOS, 0);
  44.  
  45.   __stat (r, buf);
  46.  
  47.   return (0);
  48. }
  49.  
  50. int
  51. fstat (int fd, register struct stat *buf)
  52. {
  53.   register struct file *f;
  54.  
  55.   if (!buf)
  56.     {
  57.       errno = EINVAL;
  58.       return (-1);
  59.     }
  60.  
  61.   if (BADF (fd))
  62.     {
  63.       errno = EBADF;
  64.       return (-1);
  65.     }
  66.  
  67.   f = __u->file + fd;
  68.  
  69.   buf->st_dev = f->dev;
  70.  
  71.   __stat (f->r, buf);
  72.  
  73.   return (0);
  74. }
  75.  
  76. static void
  77. __stat (register int *r, register struct stat *buf)
  78. {
  79.   buf->st_ino = 0;
  80.  
  81.   buf->st_mode = ((r[5] & 0001) << 8) | ((r[5] & 0002) << 6) |
  82.     ((r[5] & 0020) >> 2) | ((r[5] & 0040) >> 4);
  83.  
  84.   switch (r[0])
  85.     {
  86.     case 1:
  87.       buf->st_mode |= S_IFREG;
  88.       break;
  89.     case 2:            /* Normal directory */
  90.     case 3:            /* Image directory (RISC OS 3 and above) */
  91.       buf->st_mode |= S_IFDIR | 0700;    /* FS bug */
  92.       break;
  93.     }
  94.  
  95.   switch (major (buf->st_dev))
  96.     {
  97.     case DEV_TTY:
  98.       buf->st_mode |= S_IFCHR;
  99.       break;
  100.     case DEV_PIPE:
  101.       buf->st_mode |= 0;
  102.       break;
  103.     }
  104.  
  105.   buf->st_nlink = 0;
  106.   buf->st_uid = 1;
  107.   buf->st_gid = 1;
  108.   buf->st_rdev = 0;
  109.   buf->st_size = r[4];
  110.   buf->st_blksize = 1024;
  111.  
  112.   if ((((unsigned int) r[2]) >> 20) == 0xfff)    /* date stamped file */
  113.     {
  114.       register unsigned int t1, t2, tc;
  115.  
  116.       t1 = (unsigned int) (r[3]);
  117.       t2 = (unsigned int) (r[2] & 0xff);
  118.  
  119.       tc = 0x6e996a00U;
  120.       if (t1 < tc)
  121.     t2--;
  122.       t1 -= tc;
  123.       t2 -= 0x33;        /* 00:00:00 Jan. 1 1970 = 0x336e996a00 */
  124.  
  125.       t1 = (t1 / 100) + (t2 * 42949673U);    /* 0x100000000 / 100 = 42949672.96 */
  126.       t1 -= (t2 / 25);        /* compensate for .04 error */
  127.  
  128.       buf->st_atime = buf->st_mtime = buf->st_ctime = t1;
  129.     }
  130.   else
  131.     buf->st_atime = buf->st_mtime = buf->st_ctime = 0;
  132. }
  133.