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 >
C/C++ Source or Header  |  1992-08-16  |  4KB  |  196 lines

  1. /*
  2.    lock.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Note: Turbo C has lock and unlock functions which call MSDOS locking
  5.          facilities.
  6.  
  7.    Derived from bm.
  8.  
  9.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  10.    Permission granted for non-commercial copying and use, provided
  11.    this notice is retained.
  12.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  13.    Permission granted for non-commercial copying and use, provided
  14.    this notice is retained.
  15.  
  16.    911218 : Added this header.
  17.    920601 : Added test programs - define LOCK_PROG, UNLOCK_PROG to use them.
  18. */
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <sys/stat.h>
  22. #include <io.h>
  23. #include <conio.h>
  24. #include <ctype.h>
  25. #include <time.h>
  26. #include "lock.h"
  27. #include "misc.h"
  28.  
  29. #undef LOCKDEBUG
  30.  
  31. int
  32. islocked(char *name)
  33. {
  34.    char s[256];
  35.  
  36.    sprintf(s, "%s.lck", name);
  37.  
  38.    return access(s, 0) == 0;
  39. }
  40.  
  41. /*
  42.  *  Make lock file for file specified in name (no extension) by
  43.  *  opening <name>.lck in a 'atomic' fashion.
  44.  *
  45.  *  returns 0 for success, -1 otherwise
  46.  */
  47. int
  48. bm_lock(char *name)
  49. {
  50.    int fd;
  51.    char s[256];
  52.  
  53.    sprintf(s, "%s.lck", name);
  54. #ifdef LOCKDEBUG
  55.    printf("bm_lock: locking %s%s\n", name, EXT);
  56. #endif
  57.  
  58.    /* Try to create the lock file in an atomic operation */
  59.    if((fd = open(s, O_WRONLY|O_EXCL|O_CREAT, 0600)) == -1)
  60.       return -1;
  61.    return close(fd);
  62. }
  63.  
  64. /*
  65.  *  returns 0 for success, -1 otherwise
  66.  */
  67. int
  68. bm_unlock(char *name)
  69. {
  70.    char s[256];
  71.  
  72.    sprintf(s, "%s.lck", name);
  73. #ifdef LOCKDEBUG
  74.    printf("bm_unlock: unlocking %s%s\n", name, EXT);
  75. #endif
  76.    return unlink(s);
  77. }
  78.  
  79. static int
  80. fileinfo(char *name)
  81. {
  82.    struct stat sbuf;
  83.    time_t t = time(NULL);
  84.    char s[256];
  85.  
  86.    sprintf(s, "%s.lck", name);
  87.  
  88.    if (stat(s, &sbuf) != 0)
  89.       return -1;               /* The lockfile may have been removed by now */
  90.  
  91.    fprintf(stderr, "Mail file is locked (%s).\n", s);
  92.    fprintf(stderr, "Lock file was made at: %s", ctime(&sbuf.st_atime));
  93.    fprintf(stderr, "Current clock time is: %s", ctime(&t));
  94.  
  95.    return 0;
  96. }
  97.  
  98. /*
  99.  *  returns 0 for success, -1 otherwise
  100.  */
  101. int
  102. lockit(char *name)
  103. {
  104.    if (bm_lock(name)) {
  105.       (void) fileinfo(name);
  106.       do
  107.          {
  108.          printf("Abort or Retry ? [R]:");
  109.          switch ( tolower(getch()) ) {
  110.             case 'a':
  111.                return -1;
  112.             case 'u':      /* hidden option: unlock/ignore old lock */
  113.                return 0;
  114.             case 'r':
  115.             default:
  116.                continue;
  117.             }
  118.          }
  119.       while (bm_lock(name));
  120.       }
  121.    return 0;
  122. }
  123.  
  124. FILE *
  125. fopenlocked(char *dir, char *base, char *ext, char *mode)
  126. {
  127.    char lock[256], file[256];
  128.    FILE *fp;
  129.  
  130.    sprintf(lock, "%s/%s", dir, base);
  131.    if (bm_lock(lock) == -1) {
  132.       fprintf(stderr, "open: can't lock %s\n", lock);
  133.       return NULL;
  134.       }
  135.    sprintf(file, "%s%s", lock, ext);
  136.    if ((fp = fopen(file, mode)) == NULL) {
  137.       if (mode[0] != 'r')
  138.          fprintf(stderr, "open: can't open %s\n", file);
  139.       bm_unlock(lock);
  140.       return NULL;
  141.       }
  142.    return fp;
  143. }
  144.  
  145. int
  146. fcloselocked(FILE *fp, char *dir, char *base)
  147. {
  148.    char lock[256];
  149.    int ret = fclose(fp);
  150.  
  151.    sprintf(lock, "%s/%s", dir, base);
  152.    if (bm_unlock(lock) == -1) {
  153.       fprintf(stderr, "close: can't unlock %s\n", lock);
  154.       return -1;
  155.       }
  156.    return ret;
  157. }
  158.  
  159. #ifdef LOCK_PROG
  160. /*
  161.    Syntax:
  162.             lock <file>
  163.  
  164.             eg lock /usr/spool/mail/ei9gl
  165.                will try to create a lock file /usr/spool/mail/ei9gl.lck
  166. */
  167. int
  168. main(int argc, char *argv[])
  169. {
  170.    if (argc != 2) {
  171.       fprintf(stderr, "Usage: lock <mailbox>\n");
  172.       return -1;
  173.       }
  174.    return bm_lock(argv[1]);
  175. }
  176. #endif
  177.  
  178. #ifdef UNLOCK_PROG
  179. /*
  180.    Syntax:
  181.             unlock <file>
  182.  
  183.             eg unlock /usr/spool/mail/ei9gl
  184.                will try to remove the lock file /usr/spool/mail/ei9gl.lck
  185. */
  186. int
  187. main(int argc, char *argv[])
  188. {
  189.    if (argc != 2) {
  190.       fprintf(stderr, "Usage: unlock <mailbox>\n");
  191.       return -1;
  192.       }
  193.    return bm_unlock(argv[1]);
  194. }
  195. #endif
  196.