Improve portability for shift and rotate macros.
[mLib] / lock.c
diff --git a/lock.c b/lock.c
index 48cc813..445e43d 100644 (file)
--- a/lock.c
+++ b/lock.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: lock.c,v 1.1 1999/05/15 10:33:53 mdw Exp $
+ * $Id: lock.c,v 1.3 1999/06/06 01:23:00 mdw Exp $
  *
  * Simplified POSIX locking interface
  *
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: lock.c,v $
+ * Revision 1.3  1999/06/06 01:23:00  mdw
+ * Fix signal handling.
+ *
+ * Revision 1.2  1999/05/26 20:53:40  mdw
+ * Fixes for stupid bugs.
+ *
  * Revision 1.1  1999/05/15 10:33:53  mdw
  * Add simplified locking code.
  *
@@ -85,14 +91,14 @@ static void lock_alarm(int sig) { ; }
 int lock_file(int fd, unsigned how)
 {
   struct flock fk;
-  void (*alrm)(int);
+  struct sigaction sa, oldsa;
   int e;
 
   /* --- Fill in the easy bits --- */
 
   fk.l_whence = SEEK_SET;
   fk.l_start = 0;
-  fk.l_whence = 0;
+  fk.l_len = 0;
 
   /* --- Unlocking is really easy --- */
 
@@ -101,7 +107,7 @@ int lock_file(int fd, unsigned how)
     return (fcntl(fd, F_SETLK, &fk));
   }
 
-  /* --- Set an alarm handler --- */
+  /* --- Decide how to do the locking --- */
 
   if (how == LOCK_EXCL)
     fk.l_type = F_WRLCK;
@@ -112,13 +118,26 @@ int lock_file(int fd, unsigned how)
     return (-1);
   }
 
-  alrm = signal(SIGALRM, lock_alarm);
+  /* --- Set up the alarm --- */
+
+  sa.sa_handler = lock_alarm;
+  sa.sa_flags = 0;
+  sigemptyset(&sa.sa_mask);
+  if (sigaction(SIGALRM, &sa, &oldsa))
+    return (-1);
+
+  /* --- Do it --- */
+
   alarm(LOCK_TIMEOUT);
   if ((e = fcntl(fd, F_SETLKW, &fk)) != 0) {
     if (errno == EINTR)
       errno = EAGAIN;
   }
-  signal(SIGALRM, alrm);
+
+  /* --- Remove the alarm handling --- */
+
+  alarm(0);
+  sigaction(SIGALRM, &oldsa, 0);
   return (e);
 }