home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / FSTAT.C < prev    next >
Text File  |  1991-07-22  |  4KB  |  133 lines

  1. /*PLF Mon  05-08-1989  10:12:15 */
  2. /* This code from the MSC 5.10 Runtime Source had a bug in it. It was
  3.  * incorrectly classifing a network file as a device. I have fixed
  4.  * the bug. You will need the include files from the MSC 5.10 Runtime
  5.  * Library Source disks to compile it.
  6.  * I changed the line with *PLF in the comment. (apx line 111)
  7.  */
  8. #define DEVICE 0x0001
  9.  
  10. /***
  11. *fstat.c - OS/2 return file status info
  12. *
  13. *    Copyright (c) 1985-1988, Microsoft Corporation.  All rights reserved.
  14. *
  15. *Purpose:
  16. *    defines fstat() - return file status info
  17. *
  18. *******************************************************************************/
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <errno.h>
  23. #include "e:\msc_src\include\register.h"
  24. #include "e:\msc_src\include\msdos.h"
  25. #include <dos.h>
  26. #include "e:\msc_src\include\dostypes.h"
  27. #include <io.h>
  28. #include "e:\msc_src\include\internal.h"
  29. #include <stddef.h>
  30. #include <doscalls.h>
  31.  
  32. #define IO_DEVNBR    0x3f
  33.  
  34.  
  35. /***
  36. *int fstat(fildes, buf) - fills supplied buffer with status info
  37. *
  38. *Purpose:
  39. *    Fills the supplied buffer with status information on the
  40. *    file represented by the specified file designator.
  41. *    WARNING: the dev/rdev fields are zero for files.  This is
  42. *    incompatible with DOS 3 version of this routine.
  43. *
  44. *Entry:
  45. *    int filedes -    file descriptor
  46. *    struct stat *buf - buffer to store result in
  47. *
  48. *Exit:
  49. *    fills in buffer pointed to by buf
  50. *    returns 0 if successful
  51. *    returns -1 and sets errno if unsuccessful
  52. *
  53. *Exceptions:
  54. *
  55. *******************************************************************************/
  56.  
  57. fstat(fildes, buf)
  58. REG2 int fildes;
  59. REG1 struct stat *buf;
  60.  
  61. {
  62.     long cpos;
  63.     int isdev;    /* 0 for a file, 1 for a device */
  64.     int descrip;    /* device descriptor word */
  65.     struct FileStatus fs ;
  66.  
  67.     /* Check the validity of the fildes by doing a get-device-info call.
  68.      * This also gives us other information we may need later
  69.      */
  70.  
  71.     if (DOSQHANDTYPE(fildes, (unsigned far *)&isdev,
  72.     (unsigned far *)&descrip))
  73.     {
  74.         errno = EBADF;
  75.         return(-1); /* error from DOS call - bad file designator */
  76.     }
  77.  
  78.     /* set the common fields */
  79.  
  80.     buf->st_ino = buf->st_uid = buf->st_gid = buf->st_mode = 0;
  81.     buf->st_nlink = 1;
  82.     buf->st_mode |= (_osfile[fildes] & FRDONLY)
  83.         ? (S_IREAD + (S_IREAD >> 3) + (S_IREAD >> 6))
  84.             : ((S_IREAD|S_IWRITE) +
  85.                 ((S_IREAD|S_IWRITE) >> 3)
  86.                 + ((S_IREAD|S_IWRITE) >> 6));
  87.  
  88.     /* set file date fields - NOTE for code below, it should be
  89.      * remembered that calls to QFILEINFO cannot fail since the file
  90.      * handle is known to be good since we got by the QHANDTYPE call.
  91.      */
  92.  
  93.     (void) DOSQFILEINFO(fildes, 1, (char far *) & fs, sizeof(fs));
  94.  
  95.     buf->st_mtime = XTIME(fs.write_date, fs.write_time);
  96.  
  97.     if (fs.access_date || fs.access_time)
  98.         buf->st_atime = XTIME(fs.access_date, fs.access_time);
  99.     else
  100.         buf->st_atime = buf->st_mtime ;
  101.  
  102.     if (fs.create_date || fs.create_time)
  103.         buf->st_ctime = XTIME(fs.create_date, fs.create_time);
  104.     else
  105.         buf->st_ctime = buf->st_mtime ;
  106.  
  107.     buf->st_mtime = XTIME(fs.write_date, fs.write_time);
  108.  
  109.     /* check for device or file */
  110.  
  111.     if (isdev & DEVICE) {    /*PLF Mon  05-08-1989  10:14:27 */
  112.         /* file designator refers to a device - set file size to 0 */
  113.  
  114.         buf->st_size = 0;
  115.         buf->st_mode |= S_IFCHR;
  116.         buf->st_rdev = buf->st_dev = fildes;
  117.     }
  118.     else {
  119.         /* file designator refers to a file - set actual file size */
  120.  
  121.         cpos = lseek(fildes, 0L, 1);
  122.         buf->st_size = lseek(fildes, 0L, 2);
  123.         lseek(fildes, cpos, 0);
  124.  
  125.         buf->st_mode |= S_IFREG;
  126.         buf->st_rdev = buf->st_dev = 0; /* This is a BUG! */
  127.     }
  128.  
  129.     return(0);
  130. }
  131.  
  132. 
  133.