home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OS9000 / APPS / rcs.lzh / rcs1 / stat.c < prev    next >
Text File  |  1996-04-22  |  5KB  |  206 lines

  1. static char RCSid[]="$Id: stat.c_v 1.3 96/04/23 02:08:22 hiro Exp $";
  2. /************************************************************
  3. * (C) Copyright 1995
  4. * ARK Systems USA
  5. * All rights reserved.
  6. *
  7. * stat() and fstat() standard libraries
  8. * $Date: 96/04/23 02:08:22 $
  9. * $Revision: 1.3 $
  10. * $Log:    stat.c_v $
  11.  * Revision 1.3  96/04/23  02:08:22  hiro
  12.  * Ported back to OS-9.
  13.  * 
  14.  * Revision 1.2  96/04/07  00:06:12  hiro
  15.  * Ported to OS-9000.
  16.  * 
  17. ************************************************************/
  18.  
  19. #include    <modes.h>
  20. #include    <rbf.h>
  21. #ifdef _OS9000
  22. #include    <sys/common.h>
  23. #include    <sys/unix.h>
  24. #define        fildes    fd_stats
  25. #define        fd_att    fd_flag
  26. #define        fd_link    fd_links
  27. #else
  28. #include    <direct.h>
  29. #define    bzero(b, s)    memset((b),0,(s))
  30. #endif
  31. #include    <stdio.h>
  32. #include    <errno.h>
  33. #include    <sgstat.h>
  34. #include    <sg_codes.h>
  35. #include    "stat.h"
  36.  
  37. #ifndef DT_SCF
  38. #include    <io.h>
  39. #endif
  40.  
  41. #ifndef S_IFREG
  42. #define S_IFREG    0
  43. #define S_IFCHR    0
  44. #define S_IFIFO    0
  45. #endif
  46.  
  47. /*
  48.  * Unix stat()
  49.  */
  50. int    stat( pathname, sbuf)
  51. char    *pathname;
  52. struct    stat    *sbuf;
  53. {
  54. int    path, err;
  55.  
  56.     if ((path = open(pathname,S_IREAD)) == -1) return -1;
  57.     err = fstat(path, sbuf);
  58.     close(path);
  59.     return err;
  60. }
  61.  
  62. /*
  63.  * Unix fstat()
  64.  */
  65. int    fstat( path, sbuf)
  66. int     path;
  67. struct    stat    *sbuf;
  68. {
  69. struct    fildes    fd;
  70. struct    sgbuf    opt;
  71. #ifdef _OS9000
  72. int err;
  73. u_int32 in;
  74. process_id pid;
  75. u_int16 priority, age, group, user;
  76. int32 schedule;
  77. owner_id oid;
  78. #endif
  79.  
  80.     bzero(sbuf, sizeof(*sbuf));
  81.     if( _gs_opt( path, &opt, sizeof(opt) ) < 0 )
  82.         return(-1);
  83. #ifdef _OS9000
  84.     if ((errno = _os_gs_fd(path, sizeof(fd), &fd)) != 0)
  85.         return -1;
  86.     sbuf->st_gid    = fd.fd_group;
  87.     sbuf->st_uid    = fd.fd_owner;
  88.     sbuf->st_ctime    = fd.fd_ctime;
  89.     sbuf->st_atime    = fd.fd_atime;
  90.     sbuf->st_mtime    = fd.fd_mtime;
  91.     /* Word around OS-90000's stupid protection against _os_gs_fdaddr */
  92.     _os_id(&pid, &priority, &age, &schedule, &group, &user);
  93.     oid.grp_usr.grp    = 0;
  94.     oid.grp_usr.usr    = 0;
  95.     _os_setuid(oid);
  96.     _os_gs_fdaddr(path, &in);
  97.     oid.grp_usr.grp    = group;
  98.     oid.grp_usr.usr    = user;
  99.     _os_setuid(oid);
  100.     sbuf->st_size    = fd.fd_size;
  101.     sbuf->st_ino    = in;
  102.     sbuf->st_dev    = opt.sg_drive;
  103.     sbuf->st_rdev    = 0;
  104.     if (fd.fd_att & S_IREAD)    sbuf->st_mode |= S_IRUSR;
  105.     if (fd.fd_att & S_IWRITE)    sbuf->st_mode |= S_IWUSR;
  106.     if (fd.fd_att & S_IEXEC)    sbuf->st_mode |= S_IXUSR;
  107.     if (fd.fd_att & S_IGREAD)    sbuf->st_mode |= S_IRGRP;
  108.     if (fd.fd_att & S_IGWRITE)    sbuf->st_mode |= S_IWGRP;
  109.     if (fd.fd_att & S_IGEXEC)    sbuf->st_mode |= S_IXGRP;
  110. #else
  111.     if (_gs_gfd(path, &fd, sizeof(fd)) < 0)
  112.         return -1;
  113.     sbuf->st_gid = (u_char) fd.fd_own[0];
  114.     sbuf->st_uid = (u_char) fd.fd_own[1];
  115.     {
  116.     struct    tm tbuf;
  117.         tbuf.tm_sec        = 0;
  118.         tbuf.tm_min        = fd.fd_date[4];
  119.         tbuf.tm_hour    = fd.fd_date[3];
  120.         tbuf.tm_mday    = fd.fd_date[2];
  121.         tbuf.tm_mon        = fd.fd_date[1]-1;
  122.         tbuf.tm_year    = fd.fd_date[0];
  123.         sbuf->st_mtime    = sbuf->st_atime = mktime(&tbuf);
  124.         tbuf.tm_sec        = tbuf.tm_min = tbuf.tm_hour= 0;
  125.         tbuf.tm_mday    = (int)fd.fd_dcr[2];
  126.         tbuf.tm_mon        = (int)fd.fd_dcr[1]-1;
  127.         tbuf.tm_year    = (int)fd.fd_dcr[0];
  128.         sbuf->st_ctime    = mktime(&tbuf);
  129.     }
  130.     memcpy( &sbuf->st_size, fd.fd_fsize, sizeof(time_t));
  131.     sbuf->st_dev    = opt.sg_dvt - _getsys(D_DevTbl, sizeof(void*))
  132.                         + opt.sg_drive;
  133.     sbuf->st_ino    = opt.sg_fdpsn;
  134.     sbuf->st_rdev    = 0;
  135.     if (fd.fd_att & S_IREAD)    sbuf->st_mode |= S_IRUSR;
  136.     if (fd.fd_att & S_IWRITE)    sbuf->st_mode |= S_IWUSR;
  137.     if (fd.fd_att & S_IEXEC)    sbuf->st_mode |= S_IXUSR;
  138. #endif
  139.     if (fd.fd_att & S_IOREAD)    sbuf->st_mode |= S_IROTH;
  140.     if (fd.fd_att & S_IOWRITE)    sbuf->st_mode |= S_IWOTH;
  141.     if (fd.fd_att & S_IOEXEC)    sbuf->st_mode |= S_IXOTH;
  142.     sbuf->st_mode    |= filetype(path, &fd);
  143.     sbuf->st_nlink    = (short)fd.fd_link;
  144.     return(0);
  145. }
  146.  
  147. static filetype(path, fd)
  148. int        path;
  149. struct    fildes    *fd;
  150. {
  151. struct    sgbuf    s;
  152.  
  153.     if (fd == NULL )
  154.     {
  155.         _gs_opt(path, &s);
  156.         switch(s.sg_class)
  157.         {
  158.         case DT_SCF:
  159.             return(S_IFCHR);
  160.         case DT_PIPE:
  161.             return(S_IFIFO);
  162.         }
  163.     }
  164.     return (fd->fd_att & S_IFDIR) ? S_IFDIR : S_IFREG;
  165. }
  166.  
  167. #define TRUE 1
  168. #define FALSE 0
  169.  
  170. /*
  171.  * _cmpfile() compares 2 files and returns 1 if they are considered
  172.  * identical or 0 otherwise
  173.  */
  174. int _cmpfile(a, b)
  175. char *a, *b;
  176. {
  177. struct    fildes dp0, dp1;
  178. struct    sgbuf   iopt,oopt;
  179. int    path0, path1;
  180.         
  181.     if ((path0 = open(a, S_IREAD)) == -1) return -1;
  182.     if ((path1 = open(b, S_IREAD)) == -1)
  183.     {
  184.         close(path0);
  185.         return(-1);
  186.     }
  187.  
  188.     _gs_opt(path0, &iopt, sizeof(iopt));
  189.     _gs_opt(path1, &oopt, sizeof(oopt));
  190.      close(path0);     close(path1);
  191.  
  192.     if (iopt.sg_class == DT_RBF &&
  193.         oopt.sg_class == DT_RBF &&
  194.         iopt.sg_drive == oopt.sg_drive
  195. #ifndef _OS9000
  196.     &&    iopt.sg_dvt   == oopt.sg_dvt   &&
  197.         iopt.sg_fdpsn == oopt.sg_fdpsn
  198. #endif
  199.         )
  200.         return TRUE;
  201.     else
  202.         return FALSE;
  203. }
  204.