home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 198_02 / lock.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  4KB  |  165 lines

  1. /*    LOCK:    File locking command routines for MicroEMACS
  2.         written by Daniel Lawrence
  3.                                 */
  4.  
  5. #include <stdio.h>
  6. #include "estruct.h"
  7. #include "edef.h"
  8.  
  9. #if    FILOCK
  10. #if    BSD
  11. #include <sys/errno.h>
  12.  
  13. extern int sys_nerr;        /* number of system error messages defined */
  14. extern char *sys_errlist[];    /* list of message texts */
  15. extern int errno;        /* current error */
  16.  
  17. char *lname[NLOCKS];    /* names of all locked files */
  18. int numlocks;        /* # of current locks active */
  19.  
  20. /* lockchk:    check a file for locking and add it to the list */
  21.  
  22. lockchk(fname)
  23.  
  24. char *fname;    /* file to check for a lock */
  25.  
  26. {
  27.     register int i;        /* loop indexes */
  28.     register int status;    /* return status */
  29.     char *undolock();
  30.  
  31.     /* check to see if that file is already locked here */
  32.     if (numlocks > 0)
  33.         for (i=0; i < numlocks; ++i)
  34.             if (strcmp(fname, lname[i]) == 0)
  35.                 return(TRUE);
  36.  
  37.     /* if we have a full locking table, bitch and leave */
  38.     if (numlocks == NLOCKS) {
  39.         mlwrite("LOCK ERROR: Lock table full");
  40.         return(ABORT);
  41.     }
  42.  
  43.     /* next, try to lock it */
  44.     status = lock(fname);
  45.     if (status == ABORT)    /* file is locked, no override */
  46.         return(ABORT);
  47.     if (status == FALSE)    /* locked, overriden, dont add to table */
  48.         return(TRUE);
  49.  
  50.     /* we have now locked it, add it to our table */
  51.     lname[++numlocks - 1] = (char *)malloc(strlen(fname) + 1);
  52.     if (lname[numlocks - 1] == NULL) {    /* malloc failure */
  53.         undolock(fname);        /* free the lock */
  54.         mlwrite("Cannot lock, out of memory");
  55.         --numlocks;
  56.         return(ABORT);
  57.     }
  58.  
  59.     /* everthing is cool, add it to the table */
  60.     strcpy(lname[numlocks-1], fname);
  61.     return(TRUE);
  62. }
  63.  
  64. /*    lockrel:    release all the file locks so others may edit */
  65.  
  66. lockrel()
  67.  
  68. {
  69.     register int i;        /* loop index */
  70.     register int status;    /* status of locks */
  71.     register int s;        /* status of one unlock */
  72.  
  73.     status = TRUE;
  74.     if (numlocks > 0)
  75.         for (i=0; i < numlocks; ++i) {
  76.             if ((s = unlock(lname[i])) != TRUE)
  77.                 status = s;
  78.             free(lname[i]);
  79.         }
  80.     numlocks = 0;
  81.     return(status);
  82. }
  83.  
  84. /* lock:    Check and lock a file from access by others
  85.         returns    TRUE = files was not locked and now is
  86.             FALSE = file was locked and overridden
  87.             ABORT = file was locked, abort command
  88. */
  89.  
  90. lock(fname)
  91.  
  92. char *fname;    /* file name to lock */
  93.  
  94. {
  95.     register char *locker;    /* lock error message */
  96.     register int status;    /* return status */
  97.     char msg[NSTRING];    /* message string */
  98.     char *dolock();
  99.  
  100.     /* attempt to lock the file */
  101.     locker = dolock(fname);
  102.     if (locker == NULL)    /* we win */
  103.         return(TRUE);
  104.  
  105.     /* file failed...abort */
  106.     if (strncmp(locker, "LOCK", 4) == 0) {
  107.         lckerror(locker);
  108.         return(ABORT);
  109.     }
  110.  
  111.     /* someone else has it....override? */
  112.     strcpy(msg, "File in use by ");
  113.     strcat(msg, locker);
  114.     strcat(msg, ", overide?");
  115.     status = mlyesno(msg);        /* ask them */
  116.     if (status == TRUE)
  117.         return(FALSE);
  118.     else
  119.         return(ABORT);
  120. }
  121.  
  122. /*    unlock:    Unlock a file
  123.         this only warns the user if it fails
  124.                             */
  125.  
  126. unlock(fname)
  127.  
  128. char *fname;    /* file to unlock */
  129.  
  130. {
  131.     register char *locker;    /* undolock return string */
  132.     char *undolock();
  133.  
  134.     /* unclock and return */
  135.     locker = undolock(fname);
  136.     if (locker == NULL)
  137.         return(TRUE);
  138.  
  139.     /* report the error and come back */
  140.     lckerror(locker);
  141.     return(FALSE);
  142. }
  143.  
  144. lckerror(errstr)    /* report a lock error */
  145.  
  146. char *errstr;        /* lock error string to print out */
  147.  
  148. {
  149.     char obuf[NSTRING];    /* output buffer for error message */
  150.  
  151.     strcpy(obuf, errstr);
  152.     strcat(obuf, " - ");
  153.     if (errno < sys_nerr)
  154.         strcat(obuf, sys_errlist[errno]);
  155.     else
  156.         strcat(obuf, "[can not get system error message]");
  157.     mlwrite(obuf);
  158. }
  159. #endif
  160. #else
  161. lckhello()    /* dummy function */
  162. {
  163. }
  164. #endif
  165.