home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / SYS / FSTAT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  2.1 KB  |  84 lines

  1. /* sys/fstat.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <os2emx.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include "syscalls.h"
  9.  
  10. int __fstat (int handle, struct stat *buf)
  11. {
  12.   ULONG rc;
  13.   ULONG htype, hflags;
  14.   FILESTATUS3 info;
  15.  
  16.   memset (buf, 0, sizeof (*buf));
  17.   rc = DosQueryHType (handle, &htype, &hflags);
  18.   if (rc != 0)
  19.     {
  20.       _sys_set_errno (rc);
  21.       return (-1);
  22.     }
  23.   switch (htype & 0xff)
  24.     {
  25.     case 1:
  26.       buf->st_mode = S_IFCHR;
  27.       break;
  28.     case 2:
  29.       buf->st_mode = S_IFSOCK;
  30.       break;
  31.     default:
  32.       buf->st_mode = S_IFREG;
  33.       break;
  34.     }
  35.   if (buf->st_mode == S_IFREG)
  36.     {
  37.       rc = DosQueryFileInfo (handle, FIL_STANDARD, &info, sizeof (info));
  38.       if (rc != 0)
  39.         {
  40.           _sys_set_errno (rc);
  41.           return (-1);
  42.         }
  43.       buf->st_attr = info.attrFile;
  44.       buf->st_size = info.cbFile;
  45.       buf->st_reserved = 0;
  46.       buf->st_mtime = _sys_p2t (info.ftimeLastWrite, info.fdateLastWrite);
  47.       if (FTIMEZEROP (info.ftimeCreation) &&
  48.           FDATEZEROP (info.fdateCreation))
  49.         buf->st_ctime = buf->st_mtime;
  50.       else
  51.         buf->st_ctime = _sys_p2t (info.ftimeCreation, info.fdateCreation);
  52.       if (FTIMEZEROP (info.ftimeLastAccess) &&
  53.           FDATEZEROP (info.fdateLastAccess))
  54.         buf->st_atime = buf->st_mtime;
  55.       else
  56.         buf->st_atime = _sys_p2t (info.ftimeLastAccess, info.fdateLastAccess);
  57.       if (info.attrFile & 1)
  58.         buf->st_mode |= (S_IREAD >> 6) * 0111;
  59.       else
  60.         buf->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
  61.     }
  62.   else
  63.     {
  64.       buf->st_size = 0;
  65.       rc = DosQueryFHState (handle, &hflags);
  66.       if (rc != 0)
  67.         {
  68.           _sys_set_errno (rc);
  69.           return (-1);
  70.         }
  71.       if ((hflags & 7) == 0)
  72.         buf->st_mode |= (S_IREAD >> 6) * 0111;
  73.       else
  74.         buf->st_mode |= ((S_IREAD|S_IWRITE) >> 6) * 0111;
  75.     }
  76.   buf->st_uid = 0;
  77.   buf->st_gid = 0;
  78.   buf->st_ino = _sys_ino++;
  79.   buf->st_dev = 0;
  80.   buf->st_rdev = 0;
  81.   buf->st_nlink = 1;
  82.   return (0);
  83. }
  84.