home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
LOCK.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-16
|
4KB
|
196 lines
/*
lock.c : Copyright Paul Healy, EI9GL, 1992.
Note: Turbo C has lock and unlock functions which call MSDOS locking
facilities.
Derived from bm.
Copyright 1986 Bdale Garbee, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
911218 : Added this header.
920601 : Added test programs - define LOCK_PROG, UNLOCK_PROG to use them.
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <conio.h>
#include <ctype.h>
#include <time.h>
#include "lock.h"
#include "misc.h"
#undef LOCKDEBUG
int
islocked(char *name)
{
char s[256];
sprintf(s, "%s.lck", name);
return access(s, 0) == 0;
}
/*
* Make lock file for file specified in name (no extension) by
* opening <name>.lck in a 'atomic' fashion.
*
* returns 0 for success, -1 otherwise
*/
int
bm_lock(char *name)
{
int fd;
char s[256];
sprintf(s, "%s.lck", name);
#ifdef LOCKDEBUG
printf("bm_lock: locking %s%s\n", name, EXT);
#endif
/* Try to create the lock file in an atomic operation */
if((fd = open(s, O_WRONLY|O_EXCL|O_CREAT, 0600)) == -1)
return -1;
return close(fd);
}
/*
* returns 0 for success, -1 otherwise
*/
int
bm_unlock(char *name)
{
char s[256];
sprintf(s, "%s.lck", name);
#ifdef LOCKDEBUG
printf("bm_unlock: unlocking %s%s\n", name, EXT);
#endif
return unlink(s);
}
static int
fileinfo(char *name)
{
struct stat sbuf;
time_t t = time(NULL);
char s[256];
sprintf(s, "%s.lck", name);
if (stat(s, &sbuf) != 0)
return -1; /* The lockfile may have been removed by now */
fprintf(stderr, "Mail file is locked (%s).\n", s);
fprintf(stderr, "Lock file was made at: %s", ctime(&sbuf.st_atime));
fprintf(stderr, "Current clock time is: %s", ctime(&t));
return 0;
}
/*
* returns 0 for success, -1 otherwise
*/
int
lockit(char *name)
{
if (bm_lock(name)) {
(void) fileinfo(name);
do
{
printf("Abort or Retry ? [R]:");
switch ( tolower(getch()) ) {
case 'a':
return -1;
case 'u': /* hidden option: unlock/ignore old lock */
return 0;
case 'r':
default:
continue;
}
}
while (bm_lock(name));
}
return 0;
}
FILE *
fopenlocked(char *dir, char *base, char *ext, char *mode)
{
char lock[256], file[256];
FILE *fp;
sprintf(lock, "%s/%s", dir, base);
if (bm_lock(lock) == -1) {
fprintf(stderr, "open: can't lock %s\n", lock);
return NULL;
}
sprintf(file, "%s%s", lock, ext);
if ((fp = fopen(file, mode)) == NULL) {
if (mode[0] != 'r')
fprintf(stderr, "open: can't open %s\n", file);
bm_unlock(lock);
return NULL;
}
return fp;
}
int
fcloselocked(FILE *fp, char *dir, char *base)
{
char lock[256];
int ret = fclose(fp);
sprintf(lock, "%s/%s", dir, base);
if (bm_unlock(lock) == -1) {
fprintf(stderr, "close: can't unlock %s\n", lock);
return -1;
}
return ret;
}
#ifdef LOCK_PROG
/*
Syntax:
lock <file>
eg lock /usr/spool/mail/ei9gl
will try to create a lock file /usr/spool/mail/ei9gl.lck
*/
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: lock <mailbox>\n");
return -1;
}
return bm_lock(argv[1]);
}
#endif
#ifdef UNLOCK_PROG
/*
Syntax:
unlock <file>
eg unlock /usr/spool/mail/ei9gl
will try to remove the lock file /usr/spool/mail/ei9gl.lck
*/
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: unlock <mailbox>\n");
return -1;
}
return bm_unlock(argv[1]);
}
#endif