]> code.delx.au - gnu-emacs/blobdiff - src/filelock.c
* Makefile.in (doxemacs, dotemacs): Explicitly pass along the CC
[gnu-emacs] / src / filelock.c
index 81e72432c069ca9b0db6ea6017d93e87a8b8a0b5..738cb382b982afa83418d0f73dbd7c2f6143f5e1 100644 (file)
@@ -1,10 +1,10 @@
-/* Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
+/* Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -20,20 +20,28 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include "config.h"
+
+#ifdef VMS
+#include "vms-pwd.h"
+#else
 #include <pwd.h>
+#endif
+
 #include <errno.h>
 #include <sys/file.h>
 #ifdef USG
 #include <fcntl.h>
 #endif /* USG */
 
-#undef NULL
 #include "lisp.h"
 #include "paths.h"
 #include "buffer.h"
 
 extern int errno;
 
+extern char *egetenv ();
+extern char *strcpy ();
+
 #ifdef CLASH_DETECTION
   
 /* If system does not have symbolic links, it does not have lstat.
@@ -43,6 +51,44 @@ extern int errno;
 #define lstat stat
 #endif
 
+
+/* The name of the directory in which we keep lock files, with a '/'
+   appended.  */  
+char *lock_path;
+
+/* The name of the file in the lock directory which is used to
+   arbitrate access to the entire directory.  */
+#define SUPERLOCK_NAME "!!!SuperLock!!!"
+
+/* The path to the superlock file.  This is SUPERLOCK_NAME appended to
+   lock_path.  */
+char *superlock_path;
+
+/* Set LOCK to the name of the lock file for the filename FILE.
+   char *LOCK; Lisp_Object FILE;  */
+#define MAKE_LOCK_PATH(lock, file) \
+  (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_path) + 1), \
+   fill_in_lock_file_name (lock, (file)))
+
+fill_in_lock_file_name (lockfile, fn)
+     register char *lockfile;
+     register Lisp_Object fn;
+{
+  register char *p;
+
+  strcpy (lockfile, lock_path);
+
+  p = lockfile + strlen (lockfile);
+
+  strcpy (p, XSTRING (fn)->data);
+
+  for (; *p; p++)
+    {
+      if (*p == '/')
+       *p = '!';
+    }
+}
+
 static Lisp_Object
 lock_file_owner_name (lfname)
      char *lfname;
@@ -85,16 +131,15 @@ lock_file (fn)
   register Lisp_Object attack;
   register char *lfname;
 
-  /* Create the name of the lock-file for file fn */
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_PATH (lfname, fn);
 
-  /* See if this file is visited and has changed on disk since it was visited.  */
+  /* See if this file is visited and has changed on disk since it was
+     visited.  */
   {
     register Lisp_Object subject_buf = Fget_file_buffer (fn);
-    if (!NULL (subject_buf)
-       && NULL (Fverify_visited_file_modtime (subject_buf))
-       && !NULL (Ffile_exists_p (fn)))
+    if (!NILP (subject_buf)
+       && NILP (Fverify_visited_file_modtime (subject_buf))
+       && !NILP (Ffile_exists_p (fn)))
       call1 (intern ("ask-user-about-supersession-threat"), fn);
   }
 
@@ -106,36 +151,17 @@ lock_file (fn)
   /* Else consider breaking the lock */
   attack = call2 (intern ("ask-user-about-lock"), fn,
                  lock_file_owner_name (lfname));
-  if (!NULL (attack))
+  if (!NILP (attack))
     /* User says take the lock */
     {
       lock_superlock (lfname);
       lock_file_1 (lfname, O_WRONLY) ;
-      unlink (PATH_SUPERLOCK);
+      unlink (superlock_path);
       return;
     }
   /* User says ignore the lock */
 }
 
-fill_in_lock_file_name (lockfile, fn)
-     register char *lockfile;
-     register Lisp_Object fn;
-{
-  register char *p;
-
-  strcpy (lockfile, PATH_LOCK);
-
-  p = lockfile + strlen (lockfile);
-
-  strcpy (p, XSTRING (fn)->data);
-
-  for (; *p; p++)
-    {
-      if (*p == '/')
-       *p = '!';
-    }
-}
-
 /* Lock the lock file named LFNAME.
    If MODE is O_WRONLY, we do so even if it is already locked.
    If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
@@ -231,15 +257,14 @@ unlock_file (fn)
 {
   register char *lfname;
 
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_PATH (lfname, fn);
 
   lock_superlock (lfname);
 
   if (current_lock_owner_1 (lfname) == getpid ())
     unlink (lfname);
 
-  unlink (PATH_SUPERLOCK);
+  unlink (superlock_path);
 }
 
 lock_superlock (lfname)
@@ -247,7 +272,7 @@ lock_superlock (lfname)
 {
   register int i, fd;
 
-  for (i = -20; i < 0 && (fd = open (PATH_SUPERLOCK,
+  for (i = -20; i < 0 && (fd = open (superlock_path,
                                     O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
        i++)
     {
@@ -258,7 +283,7 @@ lock_superlock (lfname)
   if (fd >= 0)
     {
 #ifdef USG
-      chmod (PATH_SUPERLOCK, 0666);
+      chmod (superlock_path, 0666);
 #else
       fchmod (fd, 0666);
 #endif
@@ -292,12 +317,12 @@ or else nothing is done if current buffer isn't visiting a file.")
   (fn)
      Lisp_Object fn;
 {
-  if (NULL (fn))
+  if (NILP (fn))
     fn = current_buffer->filename;
   else
     CHECK_STRING (fn, 0);
   if (current_buffer->save_modified < MODIFF
-      && !NULL (fn))
+      && !NILP (fn))
     lock_file (fn);
   return Qnil;    
 }
@@ -336,9 +361,7 @@ t if it is locked by you, else a string of the name of the locker.")
 
   fn = Fexpand_file_name (fn, Qnil);
 
-  /* Create the name of the lock-file for file filename */
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_PATH (lfname, fn);
 
   owner = current_lock_owner (lfname);
   if (owner <= 0)
@@ -349,6 +372,29 @@ t if it is locked by you, else a string of the name of the locker.")
   return (lock_file_owner_name (lfname));
 }
 
+\f
+/* Initialization functions.  */
+
+init_filelock ()
+{
+  lock_path = egetenv ("EMACSLOCKDIR");
+  if (! lock_path)
+    lock_path = PATH_LOCK;
+
+  /* Make sure it ends with a slash.  */
+  if (lock_path[strlen (lock_path) - 1] != '/')
+    {
+      lock_path = strcpy ((char *) xmalloc (strlen (lock_path) + 2),
+                         lock_path);
+      strcat (lock_path, "/");
+    }
+
+  superlock_path = (char *) xmalloc ((strlen (lock_path)
+                                     + sizeof (SUPERLOCK_NAME)));
+  strcpy (superlock_path, lock_path);
+  strcat (superlock_path, SUPERLOCK_NAME);
+}
+
 syms_of_filelock ()
 {
   defsubr (&Sunlock_buffer);