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