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