]> diplodocus.org Git - nmh/blobdiff - sbr/lock_file.c
Document changes to base64 encoder/decoder.
[nmh] / sbr / lock_file.c
index 8506faf818c4a05251cf9db72f44eb583abadcb9..1a562f9b987d61333f7367bb50fd1aee851cbfa7 100644 (file)
 # include <sys/time.h>
 #endif
 #include <time.h>
-#include <errno.h>
 #include <fcntl.h>
 #ifdef HAVE_FLOCK
 # include <sys/file.h>
 #endif
-#ifdef HAVE_LOCKF
-# include <unistd.h>
-#endif
-
-#include <signal.h>
 
 #if defined(HAVE_LIBLOCKFILE)
-#include <lockfile.h>
+# include <lockfile.h>
 #endif
 
 #ifdef LOCKDIR
@@ -229,8 +223,13 @@ lkclosedata(int fd, const char *name)
 int
 lkfclosedata(FILE *f, const char *name)
 {
-    int fd = fileno(f);
-    int rc = fclose(f);
+    int fd, rc;
+
+    if (f == NULL)
+       return 0;
+    
+    fd = fileno(f);
+    rc = fclose(f);
 
     if (datalocktype == DOT_LOCKING)
        lkclose_dot(fd, name);
@@ -252,8 +251,13 @@ lkclosespool(int fd, const char *name)
 int
 lkfclosespool(FILE *f, const char *name)
 {
-    int fd = fileno(f);
-    int rc = fclose(f);
+    int fd, rc;
+
+    if (f == NULL)
+       return 0;
+    
+    fd = fileno(f);
+    rc = fclose(f);
 
     if (spoollocktype == DOT_LOCKING)
        lkclose_dot(fd, name);
@@ -331,7 +335,7 @@ lkclose_dot (int fd, const char *file)
 
     lockname (file, &lkinfo, 0);       /* get name of lock file */
 #if !defined(HAVE_LIBLOCKFILE)
-    unlink (lkinfo.curlock);           /* remove lock file      */
+    (void) m_unlink (lkinfo.curlock);  /* remove lock file      */
 #else
     lockfile_remove(lkinfo.curlock);
 #endif /* HAVE_LIBLOCKFILE */
@@ -376,6 +380,7 @@ lkopen_fcntl(const char *file, int access, mode_t mode)
 }
 
 
+#ifdef HAVE_FLOCK
 /*
  * Open and lock a file, using flock locking
  */
@@ -408,7 +413,7 @@ lkopen_flock(const char *file, int access, mode_t mode)
     errno = saved_errno;
     return -1;
 }
-
+#endif /* HAVE_FLOCK */
 
 /*
  * Open and lock a file, using lockf locking
@@ -508,7 +513,7 @@ lkopen_dot (const char *file, int access, mode_t mode)
                    
                    /* check for stale lockfile, else sleep */
                    if (curtime > st.st_ctime + RSECS)
-                       unlink (lkinfo.curlock);
+                       (void) m_unlink (lkinfo.curlock);
                    else
                        sleep (5);
                }
@@ -538,17 +543,18 @@ static int
 lockit (struct lockinfo *li)
 {
     int fd;
-    char *curlock, *tmplock;
+    char *curlock, *tmpfile;
 
 #if 0
     char buffer[128];
 #endif
 
     curlock = li->curlock;
-    tmplock = li->tmplock;
 
-    if ((fd = mkstemp(tmplock)) == -1)
+    if ((tmpfile = m_mktemp(li->tmplock, &fd, NULL)) == NULL) {
+        advise(NULL, "unable to create temporary file in %s", get_temp_dir());
        return -1;
+    }
 
 #if 0
     /* write our process id into lock file */
@@ -562,8 +568,8 @@ lockit (struct lockinfo *li)
      * Now try to create the real lock file
      * by linking to the temporary file.
      */
-    fd = link(tmplock, curlock);
-    unlink(tmplock);
+    fd = link(tmpfile, curlock);
+    (void) m_unlink(tmpfile);
 
     return (fd == -1 ? -1 : 0);
 }
@@ -735,21 +741,21 @@ alrmser (int sig)
 static enum locktype
 init_locktype(const char *lockname)
 {
-    if (mh_strcasecmp(lockname, "fcntl") == 0) {
+    if (strcasecmp(lockname, "fcntl") == 0) {
        return FCNTL_LOCKING;
-    } else if (mh_strcasecmp(lockname, "lockf") == 0) {
+    } else if (strcasecmp(lockname, "lockf") == 0) {
 #ifdef HAVE_LOCKF
        return LOCKF_LOCKING;
 #else /* ! HAVE_LOCKF */
        adios(NULL, "lockf not supported on this system");
 #endif /* HAVE_LOCKF */
-    } else if (mh_strcasecmp(lockname, "flock") == 0) {
+    } else if (strcasecmp(lockname, "flock") == 0) {
 #ifdef HAVE_FLOCK
        return FLOCK_LOCKING;
 #else /* ! HAVE_FLOCK */
        adios(NULL, "flock not supported on this system");
 #endif /* HAVE_FLOCK */
-    } else if (mh_strcasecmp(lockname, "dot") == 0) {
+    } else if (strcasecmp(lockname, "dot") == 0) {
        return DOT_LOCKING;
     } else {
        adios(NULL, "Unknown lock type: \"%s\"", lockname);