home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_01 / 2n01060a < prev    next >
Text File  |  1990-09-16  |  1KB  |  45 lines

  1. #include   <io.h>
  2. #include   <errno.h>
  3. #include   <dos.h>
  4. #include   "redirector.h"
  5.  
  6. /*******************************************************************
  7. *     lock_read() - read shared data from a network file
  8. *
  9. *     Parameters:
  10. *          handle (in) - file handle to read from
  11. *          buffer (in) - buffer to place data in
  12. *          length (in) - number of bytes to read
  13. *
  14. *     Returns:
  15. *          Return code is identical to read()
  16. *
  17. *     Notes:
  18. *          1.   If the desired record is locked, this routine will
  19. *               retry at 1 second intervals for RETRY attempts.
  20. *
  21. *     History:
  22. *          Original code by William H. Roetzheim, 1989
  23. **********************************************************************/
  24.  
  25. #define    RETRY      5
  26.  
  27. int lock_read(int fh, char *buffer, unsigned int length)
  28. {
  29.                 int        timeout = RETRY;
  30.       unsigned  int        count = EACCES; /* DOS error code */
  31.  
  32.       while ((lock(fh, lseek(fh, 0, SEEK_CUR), length) != 0) && (timeout > 0))
  33.       {
  34.            timeout--;
  35.            sleep(1);  /* wait one second */
  36.       }
  37.  
  38.       if (timeout > 0)/* record is successfully locked */
  39.       {
  40.            count = read(fh, buffer, length);
  41.       }
  42.  
  43.       return count;
  44. }
  45.