]> code.delx.au - gnu-emacs/blobdiff - lib-src/movemail.c
Merge from emacs-24; up to 2012-04-25T15:23:19Z!sdl.web@gmail.com
[gnu-emacs] / lib-src / movemail.c
index 4a894c1cba1a450648b153b0d4e4a6632a095449..9d19df32814688c4dac8da033c71e98362e13fdd 100644 (file)
@@ -1,7 +1,7 @@
 /* movemail foo bar -- move file foo to file bar,
    locking file foo the way /bin/mail respects.
 
-Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2011
+Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2012
   Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -68,9 +68,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 #include "syswait.h"
 #ifdef MAIL_USE_POP
 #include "pop.h"
@@ -131,7 +129,7 @@ extern int lk_open (), lk_close ();
    files appear in. */
 #ifdef MAILDIR
 #define MAIL_USE_MAILLOCK
-static char *mail_spool_name ();
+static char *mail_spool_name (char *);
 #endif
 #endif
 
@@ -167,10 +165,10 @@ main (int argc, char **argv)
 
 #ifndef MAIL_USE_SYSTEM_LOCK
   struct stat st;
-  long now;
   int tem;
-  char *lockname, *p;
+  char *lockname;
   char *tempname;
+  size_t inname_dirlen;
   int desc;
 #endif /* not MAIL_USE_SYSTEM_LOCK */
 
@@ -185,8 +183,8 @@ main (int argc, char **argv)
 # define ARGSTR "p"
 #endif /* MAIL_USE_POP */
 
-  uid_t real_gid = getgid();
-  uid_t priv_gid = getegid();
+  uid_t real_gid = getgid ();
+  uid_t priv_gid = getegid ();
 
 #ifdef WINDOWSNT
   /* Ensure all file i/o is in binary mode. */
@@ -259,7 +257,13 @@ main (int argc, char **argv)
 #ifndef MAIL_USE_SYSTEM_LOCK
 #ifdef MAIL_USE_MAILLOCK
   spool_name = mail_spool_name (inname);
-  if (! spool_name)
+  if (spool_name)
+    {
+#ifdef lint
+      lockname = 0;
+#endif
+    }
+  else
 #endif
     {
       #ifndef DIRECTORY_SEP
@@ -293,27 +297,38 @@ main (int argc, char **argv)
         to bug-gnu-emacs@prep.ai.mit.edu so we can fix it.  */
 
       lockname = concat (inname, ".lock", "");
-      tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
-      strcpy (tempname, inname);
-      p = tempname + strlen (tempname);
-      while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
-       p--;
-      *p = 0;
-      strcpy (p, "EXXXXXX");
-      mktemp (tempname);
-      unlink (tempname);
+      for (inname_dirlen = strlen (inname);
+          inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
+          inname_dirlen--)
+       continue;
+      tempname = (char *) xmalloc (inname_dirlen + sizeof "EXXXXXX");
 
       while (1)
        {
          /* Create the lock file, but not under the lock file name.  */
          /* Give up if cannot do that.  */
-         desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
+
+         memcpy (tempname, inname, inname_dirlen);
+         strcpy (tempname + inname_dirlen, "EXXXXXX");
+#ifdef HAVE_MKSTEMP
+         desc = mkstemp (tempname);
+#else
+         mktemp (tempname);
+         if (!*tempname)
+           desc = -1;
+         else
+           {
+             unlink (tempname);
+             desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
+           }
+#endif
          if (desc < 0)
            {
-             char *message = (char *) xmalloc (strlen (tempname) + 50);
-             sprintf (message, "creating %s, which would become the lock file",
-                      tempname);
-             pfatal_with_name (message);
+             int mkstemp_errno = errno;
+             error ("error while creating what would become the lock file",
+                    0, 0);
+             errno = mkstemp_errno;
+             pfatal_with_name (tempname);
            }
          close (desc);
 
@@ -336,7 +351,7 @@ main (int argc, char **argv)
             by time differences between machines.  */
          if (stat (lockname, &st) >= 0)
            {
-             now = time (0);
+             time_t now = time (0);
              if (st.st_ctime < now - 300)
                unlink (lockname);
            }
@@ -352,7 +367,10 @@ main (int argc, char **argv)
       int lockcount = 0;
       int status = 0;
 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
-      time_t touched_lock, now;
+      time_t touched_lock;
+# ifdef lint
+      touched_lock = 0;
+# endif
 #endif
 
       if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
@@ -462,7 +480,7 @@ main (int argc, char **argv)
 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
            if (spool_name)
              {
-               now = time (0);
+               time_t now = time (0);
                if (now - touched_lock > 60)
                  {
                    touchlock ();