home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / sys / stat / filelen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-29  |  1.6 KB  |  63 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* This is file FILELEN.C */
  3. /*
  4.  * Copyright (c) 1994 Eli Zaretskii <eliz@is.elta.co.il>
  5.  *
  6.  * This software may be used freely so long as this copyright notice is
  7.  * left intact.  There is no warranty on this software.
  8.  *
  9.  */
  10.  
  11. #include <errno.h>
  12. #include <dpmi.h>
  13. #include <libc/dosio.h>
  14.  
  15. long __filelength(int);
  16.  
  17. long
  18. __filelength(int fhandle)
  19. {
  20.   __dpmi_regs    regs;
  21.   unsigned short fpos_high, fpos_low;
  22.   long           retval;
  23.  
  24.   /* Remember the current file position, so we can return there
  25.      later.  */
  26.   regs.x.ax = 0x4201;      /* set pointer from current position */
  27.   regs.x.bx = fhandle;
  28.   regs.x.cx = regs.x.dx = 0; /* move 0 bytes (i.e., stay put) */
  29.   __dpmi_int(0x21, ®s);
  30.   if (regs.x.flags & 1)
  31.     {
  32.       errno = __doserr_to_errno(regs.x.ax);
  33.       return -1L;
  34.     }
  35.   fpos_high = regs.x.dx;   /* save current position */
  36.   fpos_low  = regs.x.ax;
  37.  
  38.   regs.x.cx = regs.x.dx = 0;
  39.   regs.x.ax = 0x4202;      /* set pointer 0 bytes from the end of file */
  40.   __dpmi_int(0x21, ®s);
  41.   if (regs.x.flags & 1)
  42.     {
  43.       errno = __doserr_to_errno(regs.x.ax);
  44.       return -1L;
  45.     }
  46.  
  47.   /* The absolute byte offset returned in DX:AX is the file size. */
  48.   retval = ( (long)regs.x.dx << 16 ) + regs.x.ax;
  49.  
  50.   /* Leave things as we have found them. */
  51.   regs.x.ax = 0x4200;      /* set pointer from the beginning of file */
  52.   regs.x.cx = fpos_high;
  53.   regs.x.dx = fpos_low;
  54.   __dpmi_int(0x21, ®s);
  55.   if (regs.x.flags & 1)
  56.     {
  57.       errno = __doserr_to_errno(regs.x.ax);
  58.       return -1L;
  59.     }
  60.  
  61.   return retval;
  62. }
  63.