1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-24 01:56:26 -05:00
uemacs/lock.c

163 lines
3.2 KiB
C
Raw Normal View History

/* LOCK.C
*
* File locking command routines
*
* written by Daniel Lawrence
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#include "efunc.h"
#if FILOCK
#if BSD | SVR4
#include <sys/errno.h>
uemacs: fix sparse warnings, making file-local symbols static. ansi.c:255:6: warning: symbol 'ansihello' was not declared. Should it be static? epath.h:11:6: warning: symbol 'pathname' was not declared. Should it be static? display.c:36:7: warning: symbol 'vscreen' was not declared. Should it be static? display.c:38:7: warning: symbol 'pscreen' was not declared. Should it be static? display.c:927:5: warning: symbol 'updateline' was not declared. Should it be static? evar.h:20:6: warning: symbol 'uv' was not declared. Should it be static? evar.h:24:6: warning: symbol 'envars' was not declared. Should it be static? evar.h:128:7: warning: symbol 'funcs' was not declared. Should it be static? fileio.c:14:6: warning: symbol 'ffp' was not declared. Should it be static? fileio.c:15:5: warning: symbol 'eofflag' was not declared. Should it be static? ibmpc.c:505:6: warning: symbol 'ibmhello' was not declared. Should it be static? isearch.c:36:5: warning: symbol 'saved_get_char' was not declared. Should it be static? isearch.c:37:5: warning: symbol 'eaten_char' was not declared. Should it be static? isearch.c:41:5: warning: symbol 'cmd_buff' was not declared. Should it be static? isearch.c:42:5: warning: symbol 'cmd_offset' was not declared. Should it be static? isearch.c:43:5: warning: symbol 'cmd_reexecute' was not declared. Should it be static? line.c:21:6: warning: symbol 'ykbuf' was not declared. Should it be static? line.c:22:5: warning: symbol 'ykboff' was not declared. Should it be static? lock.c:17:6: warning: symbol 'lname' was not declared. Should it be static? lock.c:18:5: warning: symbol 'numlocks' was not declared. Should it be static? vmsvt.c:521:6: warning: symbol 'hellovms' was not declared. Should it be static? vt52.c:181:6: warning: symbol 'vt52hello' was not declared. Should it be static? Signed-off-by: Thiago Farina <tfransosi@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2010-01-29 19:17:02 -05:00
static char *lname[NLOCKS]; /* names of all locked files */
static int numlocks; /* # of current locks active */
/*
* lockchk:
* check a file for locking and add it to the list
*
* char *fname; file to check for a lock
*/
int lockchk(char *fname)
{
int i; /* loop indexes */
int status; /* return status */
/* check to see if that file is already locked here */
if (numlocks > 0)
for (i = 0; i < numlocks; ++i)
if (strcmp(fname, lname[i]) == 0)
return (TRUE);
/* if we have a full locking table, bitch and leave */
if (numlocks == NLOCKS) {
mlwrite("LOCK ERROR: Lock table full");
return (ABORT);
}
/* next, try to lock it */
status = lock(fname);
if (status == ABORT) /* file is locked, no override */
return (ABORT);
if (status == FALSE) /* locked, overriden, dont add to table */
return (TRUE);
/* we have now locked it, add it to our table */
lname[++numlocks - 1] = (char *) malloc(strlen(fname) + 1);
if (lname[numlocks - 1] == NULL) { /* malloc failure */
undolock(fname); /* free the lock */
mlwrite("Cannot lock, out of memory");
--numlocks;
return (ABORT);
}
/* everthing is cool, add it to the table */
strcpy(lname[numlocks - 1], fname);
return (TRUE);
}
/*
* lockrel:
* release all the file locks so others may edit
*/
int lockrel(void)
{
int i; /* loop index */
int status; /* status of locks */
int s; /* status of one unlock */
status = TRUE;
if (numlocks > 0)
for (i = 0; i < numlocks; ++i) {
if ((s = unlock(lname[i])) != TRUE)
status = s;
free(lname[i]);
}
numlocks = 0;
return (status);
}
/*
* lock:
* Check and lock a file from access by others
* returns TRUE = files was not locked and now is
* FALSE = file was locked and overridden
* ABORT = file was locked, abort command
*
* char *fname; file name to lock
*/
int lock(char *fname)
{
char *locker; /* lock error message */
int status; /* return status */
char msg[NSTRING]; /* message string */
/* attempt to lock the file */
locker = dolock(fname);
if (locker == NULL) /* we win */
return (TRUE);
/* file failed...abort */
if (strncmp(locker, "LOCK", 4) == 0) {
lckerror(locker);
return (ABORT);
}
/* someone else has it....override? */
strcpy(msg, "File in use by ");
strcat(msg, locker);
strcat(msg, ", override?");
status = mlyesno(msg); /* ask them */
if (status == TRUE)
return (FALSE);
else
return (ABORT);
}
/*
* unlock:
* Unlock a file
* this only warns the user if it fails
*
* char *fname; file to unlock
*/
int unlock(char *fname)
{
char *locker; /* undolock return string */
/* unclock and return */
locker = undolock(fname);
if (locker == NULL)
return (TRUE);
/* report the error and come back */
lckerror(locker);
return (FALSE);
}
/*
* report a lock error
*
* char *errstr; lock error string to print out
*/
void lckerror(char *errstr)
{
char obuf[NSTRING]; /* output buffer for error message */
strcpy(obuf, errstr);
strcat(obuf, " - ");
strcat(obuf, strerror(errno));
mlwrite(obuf);
}
#endif
#else
static void lckhello(void )
{ /* dummy function */
}
#endif