home *** CD-ROM | disk | FTP | other *** search
/ Amiga Computing 59 / ac059.adf / SupportFiles / advfiles.c / advfiles.c
C/C++ Source or Header  |  1993-02-05  |  2KB  |  78 lines

  1. #include <libraries/dos.h>
  2. #include <libraries/dosextens.h>
  3.  
  4. long DosBase=0;                     /*Dos library base*/
  5. struct FileHandle *inputfilehandle; /*filehandle pointer*/
  6. struct FileLock *lock;              /*lock pointer*/
  7. long success=0;                     /*success or failure flag*/
  8. char *filename="limerick";          /*text filenames  used by 'files' program*/
  9. char *filename2="limerick2";
  10.  
  11. /*prototypes*/ 
  12. void cleanup();
  13. int openfile(),lockfile();
  14.  
  15. main()
  16. {
  17.   DosBase= OpenLibrary(DOSNAME,0);      /*open the Dos library, if possible*/
  18.   if (DosBase == 0) 
  19.     {
  20.        cleanup ("Unable to open the Dos Library");
  21.        exit(1);                  /*exit if there is no library*/
  22.     }
  23.  
  24.   success=openfile();            /*open the text file*/
  25.   if (success !=  0)             /*clean up and exit is unsuccessful*/
  26.     {
  27.        cleanup("Error opening file"); 
  28.        exit(1);
  29.     };    
  30.   printf("Process 1 \n");        /*run 'files' program, first text file open*/
  31.   Execute("files",0,0);
  32.   Close(inputfilehandle);
  33.   printf("\n \nProcess 2 \n");   /*run 'files' program,text files closed*/ 
  34.   Execute("files",0,0);
  35.   success = lockfile();          /*obtain lock for second file*/
  36.   if (success !=  0)             /*clean up and exit if unsuccessful*/
  37.     {
  38.        cleanup("Error locking file"); 
  39.        exit(1);
  40.     };    
  41.   printf("\n \nProcess 3 \n"); /*run 'files' program,second text file locked*/
  42.   Execute("files",0,0);
  43.   UnLock(lock);
  44.   printf("\n \nProcess 4 \n"); /*run 'files' program,text files unlocked*/
  45.   Execute("files",0,0);
  46.   cleanup("advfiles ok\n");    /*close libraries, free buffer*/
  47.   return(0);
  48. }
  49.  
  50. void cleanup(errormsg)         /*tidy up any loose ends*/        
  51. {    
  52.   printf(errormsg);            /*display error message*/
  53.   if (DosBase !=0) CloseLibrary(DosBase); /*close library*/
  54.   return;
  55. }
  56.  
  57. int openfile()                  /*open file if possible*/
  58. {
  59.   inputfilehandle=(struct FileHandle *) Open(filename,MODE_READWRITE);
  60.   if (inputfilehandle == NULL) 
  61.      {
  62.        printf("Cannot open file %s \n",filename);
  63.        return(1);
  64.      }
  65.   return(0);
  66. }
  67.  
  68. int lockfile()                /*lock file if possible*/
  69.    lock = (struct FileLock *) Lock(filename2,EXCLUSIVE_LOCK);
  70.    if (lock ==NULL)
  71.       {
  72.         printf("Cannot obtain a lock for file %s \n",filename2);
  73.         return(1);
  74.       }
  75.    return(0);
  76.