home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / ii-87 / exreclock2.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  4KB  |  84 lines

  1. ;/* ExRecLock2.c - Execute me to compile me with SAS/C 6.56
  2. sc DATA=NEAR NMINC STRMERGE STREQ NOSTKCHK SAVEDS IGNORE=73 ExRecLock2.c
  3. slink FROM LIB:c.o,ExRecLock2.o TO ExRecLock2 LIBRARY LIB:sc.lib,LIB:amiga.lib
  4. quit ;*/
  5.  
  6. /*
  7. (c)  Copyright 1992 Commodore-Amiga, Inc.   All rights reserved.
  8. The information contained herein is subject to change without notice,
  9. and is provided "as is" without warranty of any kind, either expressed
  10. or implied.  The entire risk as to the use of this information is
  11. assumed by the user.
  12. */
  13.  
  14. /* This is a simple example of using record locking to create an exclusive record    */
  15. /* lock on a file, and writing to that record.  The example ExRecLock1 is almost     */
  16. /* exactly the same as this example, except ExRecLock1 uses the record lock directly */
  17. /* before ExRecLock2's record.  If you try to run ExRecLock2 (or ExRecLock1) while   */
  18. /* another instance of ExRecLock2 (or ExRecLock1) is running, the second record lock */
  19. /* attempt will fail.                                                                */
  20.  
  21. #include <clib/dos_protos.h>
  22. #include <clib/alib_protos.h>
  23. #include <clib/alib_stdio_protos.h>
  24.  
  25. #ifdef LATTICE
  26. int CXBRK(void) { return(0); }                    /* Disable Lattice CTRL/C handling */
  27. void chkabort(void) { return; }
  28. #endif
  29.  
  30. #define RECORDSIZE   12
  31. #define RECORDOFFSET 12
  32.  
  33. extern struct Library *DOSBase;
  34.  
  35. UBYTE *vers = "\0$VER: ExRecLock2 37.2";
  36. UBYTE *string = "ExRecLock2\n";                           /* This string will be the */
  37.                                                           /* contents of the record. */
  38. void main(void)
  39. {
  40.     BPTR fh;
  41.  
  42.     if (DOSBase->lib_Version >= 37)   /* Record locking was introduced in Release 2, */
  43.     {                                 /* but the standard startup code will open any */
  44.                                       /* version of dos.library, so we have to ex-   */
  45.                                       /* plicitly check the version number of DOS.   */
  46.  
  47.         if (fh = Open("t:testRLock", MODE_READWRITE))     /* Open the file, creating */
  48.         {                                                 /*        it if necessary. */
  49.             if (DOSTRUE == LockRecord(fh,           /* Lock the record as exclusive, */
  50.                                       RECORDOFFSET, /* and do not wait if it is not  */
  51.                                       RECORDSIZE,   /* available immediately.        */
  52.                                       REC_EXCLUSIVE_IMMED, 0))
  53.             {
  54.                 LONG error = RECORDOFFSET;
  55.  
  56.                                      /* If the record is beyond the end of the file, */
  57.                 if (Seek(fh, 0, OFFSET_END) < RECORDOFFSET)    /* lengthen the file. */
  58.                     error = SetFileSize(fh, RECORDOFFSET, OFFSET_BEGINNING);
  59.  
  60.                 if (error == RECORDOFFSET)    /* If there was no error with the file */
  61.                 {                             /*                file size, continue. */
  62.                     if (Seek(fh, RECORDOFFSET, OFFSET_BEGINNING) < 0)
  63.                         PrintFault(IoErr(), "Seek() error");
  64.  
  65.                     if (Write(fh, string, RECORDSIZE) < 0)
  66.                         PrintFault(IoErr(), "Write() error");
  67.                     else
  68.                         PutStr("Write successful, ");
  69.                 }
  70.                 PutStr("Waiting 10 seconds...\n");
  71.                 TimeDelay(UNIT_VBLANK, 10, 0);       /* Amiga.lib function that puts */
  72.                           /* a task to sleep for a given amount of time.  This waits */
  73.                           /* 10 seconds before unlocking the record to give the user */
  74.                           /* a chance to start a second copy of this example.        */
  75.                 UnLockRecord(fh, RECORDOFFSET, RECORDSIZE);
  76.             }
  77.             else PrintFault(IoErr(), "Record Lock Failed");
  78.             Close(fh);
  79.         }
  80.         else PrintFault(IoErr(), "Open Failed");
  81.     }
  82.     else PutStr("Need dos.library V37 or greater.\n");
  83. }
  84.