home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / fstat.c < prev    next >
Text File  |  1992-03-06  |  2KB  |  102 lines

  1. #define INCL_DOSFILEMGR
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <time.h>
  7. #include <errno.h>
  8.  
  9. ULONG Dos32QueryFileInfo() asm ("Dos32QueryFileInfo");
  10. ULONG Dos32QueryHType() asm ("Dos32QueryHType");
  11.  
  12. int fstat (int fd, struct stat *buf)
  13. {
  14.    FILESTATUS fs;
  15.    ULONG rc;
  16.    struct tm tim;
  17.    ULONG HandType;
  18.    ULONG FlagWord;
  19.  
  20.    rc = Dos32QueryFileInfo (fd, 1, (PBYTE)&fs, sizeof (FILESTATUS));
  21.  
  22.    if (rc)
  23.    {
  24.       if (rc == ERROR_ACCESS_DENIED)
  25.       {
  26.          errno = EACCES;
  27.          return (-1);
  28.       }
  29.  
  30.       if (rc == ERROR_INVALID_HANDLE)
  31.       {
  32.          errno = EBADF;
  33.          return (-1);
  34.       }
  35.  
  36.       errno = EIO;
  37.       return (-1);
  38.    }
  39.  
  40.    tim.tm_sec  = fs.ftimeCreation.twosecs;
  41.    tim.tm_min  = fs.ftimeCreation.minutes;
  42.    tim.tm_hour = fs.ftimeCreation.hours;
  43.    tim.tm_mday = fs.fdateCreation.day;
  44.    tim.tm_mon  = fs.fdateCreation.month;
  45.    tim.tm_year = fs.fdateCreation.year;
  46.  
  47.    buf -> st_ctime = mktime (&tim);
  48.  
  49.    tim.tm_sec  = fs.ftimeLastAccess.twosecs;
  50.    tim.tm_min  = fs.ftimeLastAccess.minutes;
  51.    tim.tm_hour = fs.ftimeLastAccess.hours;
  52.    tim.tm_mday = fs.fdateLastAccess.day;
  53.    tim.tm_mon  = fs.fdateLastAccess.month;
  54.    tim.tm_year = fs.fdateLastAccess.year;
  55.  
  56.    buf -> st_atime = mktime (&tim);
  57.  
  58.    tim.tm_sec  = fs.ftimeLastWrite.twosecs;
  59.    tim.tm_min  = fs.ftimeLastWrite.minutes;
  60.    tim.tm_hour = fs.ftimeLastWrite.hours;
  61.    tim.tm_mday = fs.fdateLastWrite.day;
  62.    tim.tm_mon  = fs.fdateLastWrite.month;
  63.    tim.tm_year = fs.fdateLastWrite.year;
  64.  
  65.    buf -> st_mtime = mktime (&tim);
  66.  
  67.    buf -> st_size = fs.cbFile;
  68.    buf -> st_blksize = 4096;
  69.  
  70.    buf -> st_ino = 0;
  71.    buf -> st_nlink = 1;
  72.    buf -> st_uid = 0;
  73.    buf -> st_gid = 0;
  74.  
  75.    rc = Dos32QueryHType (fd, &HandType, &FlagWord);
  76.  
  77.    if (rc)
  78.    {
  79.       errno = EBADF;
  80.       return (-1);
  81.    }
  82.  
  83.    switch (HandType & 0xff)
  84.    {
  85.       case 0:
  86.          buf -> st_mode = S_IFREG;
  87.          break;
  88.  
  89.       case 1:
  90.          buf -> st_mode = S_IFCHR;
  91.          break;
  92.  
  93.       case 2:
  94.          buf -> st_mode = S_IFIFO;
  95.          break;
  96.  
  97.    }
  98.  
  99.    return (0);
  100. }
  101.  
  102.