home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / FLDOLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  2.8 KB  |  97 lines

  1. /**
  2. *
  3. * Name        FLDOLOCK -- Lock or unlock a section of an open file,
  4. *                wait intelligently if busy.
  5. *
  6. * Synopsis    ercode = fldolock (handle, offset, length,
  7. *             seconds, option);
  8. *
  9. *        int ercode      DOS function error code.
  10. *        int handle      File handle of open file.
  11. *        unsigned long offset
  12. *                  Offset (in bytes) of beginning of
  13. *                  region to lock/unlock.
  14. *        unsigned long length
  15. *                  Length (in bytes) of region to
  16. *                  lock/unlock.
  17. *        int seconds      Number of seconds to wait
  18. *                  if lock call fails due to a
  19. *                  previous lock.
  20. *        int option      FL_LOCK or FL_UNLOCK.
  21. *
  22. * Description    This function locks or unlocks a section of an open file
  23. *        associated with the specified handle.  If ticks is not 0,
  24. *        FLDOLOCK retries the locking/unlocking operation
  25. *        constantly until either (1) it succeeds, or  (2) it
  26. *        runs out of time.
  27. *
  28. *        DOS 3.10 or later is required.    Earlier versions of DOS
  29. *        return a value of 1 indicating that the function number
  30. *        is unknown.
  31. *
  32. *        The designated region of the file may extend beyond the
  33. *        end of the file.
  34. *
  35. *        The recommended technique for using locks is as follows:
  36. *
  37. *            1) Lock the region using FLDOLOCK, using a
  38. *               reasonable number of ticks.  If the lock
  39. *               attempt fails because the region was already
  40. *               locked (ercode = 0x21), either keep trying or
  41. *               report an error.
  42. *            2) After locking the region, read or write the data.
  43. *            3) Unlock the region using FLDOLOCK.
  44. *
  45. *        Do not close the file or allow the program to terminate
  46. *        without removing all locks.
  47. *
  48. * Returns    ercode          DOS function return code
  49. *
  50. * Version    6.00 (C)Copyright Blaise Computing Inc. 1987, 1989
  51. *
  52. **/
  53.  
  54. #include <dos.h>
  55. #include <errno.h>
  56. #include <io.h>
  57. #include <time.h>
  58.  
  59. #include <bfiles.h>
  60.  
  61. #define ALREADY_LOCKED 0x21
  62.  
  63. int fldolock (handle, offset, length, seconds, option)
  64. int          handle, option, seconds;
  65. unsigned long offset, length;
  66. {
  67.     int   result;
  68.     long  start_second, now_second;
  69.  
  70.         /* DOS 2.x does not know about file locking.        */
  71.     if (utdosmajor < 3)
  72.     return (1);
  73.  
  74.     time (&start_second);
  75.  
  76.     do
  77.     {        /* (Note:  DOS 3.00 does not support function 0x5c  */
  78.         /* but at least it reports a valid error code &     */
  79.         /* extended error information.)             */
  80.  
  81.         /* If there was an error, and it was not a "Lock    */
  82.         /* Violation" error, return immediately -- If there */
  83.         /* wasn't an error, return.                         */
  84.     if ((result = fllock (handle, option, offset, length)) == 0)
  85.         return (0);
  86.     else
  87.         if (result != ALREADY_LOCKED)
  88.         return ((int) result);
  89.  
  90.     time (&now_second);
  91.     } while ((now_second - start_second) < (long) seconds);
  92.  
  93.         /* If we got an error on our last call (and we got  */
  94.         /* to here so it must be error 0x21), return it.    */
  95.     return (result);
  96. }
  97.