home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / tell.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  110 lines

  1. /***
  2. *tell.c - find file position
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       contains _tell() - find file position
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <io.h>
  16.  
  17. /***
  18. *long _tell(filedes) - find file position
  19. *
  20. *Purpose:
  21. *       Gets the current position of the file pointer (no adjustment
  22. *       for buffering).
  23. *
  24. *Entry:
  25. *       int filedes - file handle of file
  26. *
  27. *Exit:
  28. *       returns file position or -1L (sets errno) if bad file descriptor or
  29. *       pipe
  30. *
  31. *Exceptions:
  32. *
  33. *******************************************************************************/
  34.  
  35. long __cdecl _tell (
  36.         int filedes
  37.         )
  38. {
  39.         return(_lseek(filedes,0L,1));
  40. }
  41.  
  42.  
  43. #else  /* _MAC */
  44.  
  45.  
  46. #include <cruntime.h>
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include <io.h>
  50. #include <internal.h>
  51. #include <stddef.h>
  52. #include <stdlib.h>
  53. #include <memory.h>
  54. #include <msdos.h>
  55. #include <macos\files.h>
  56. #include <macos\errors.h>
  57.  
  58. /***
  59. *long _tell(fh) - find file position
  60. *
  61. *Purpose:
  62. *       Gets the current position of the file pointer (no adjustment
  63. *       for buffering).
  64. *
  65. *Entry:
  66. *       int fh - file handle of file
  67. *
  68. *Exit:
  69. *       returns file position or -1L (sets errno) if bad file descriptor or
  70. *       pipe
  71. *
  72. *Exceptions:
  73. *
  74. *******************************************************************************/
  75.  
  76. long __cdecl _tell (
  77.         int fh
  78.         )
  79. {
  80.         ParamBlockRec parm;
  81.         OSErr osErr;
  82.  
  83.         /* validate handle */
  84.         if ( ((unsigned)fh >= (unsigned)_nfile) ||
  85.              !(_osfile[fh] & FOPEN) ||
  86.              (_osfile[fh] & FDEV) )
  87.         {
  88.                 /* out of range -- return error */
  89.                 errno = EBADF;
  90.                 _macerrno = 0;
  91.                 return -1;
  92.         }
  93.  
  94.         memset(&parm, 0, sizeof(ParamBlockRec));
  95.         parm.ioParam.ioRefNum = _osfhnd[fh];
  96.         osErr = PBGetFPosSync(&parm);
  97.         switch (osErr)
  98.         {
  99.                 case noErr:
  100.                         return parm.ioParam.ioPosOffset;
  101.  
  102.                 default:
  103.                         errno = EIO;
  104.                         return -1;
  105.         }
  106. }
  107.  
  108.  
  109. #endif  /* _MAC */
  110.