/* 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.
#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"
char *strerror (int);
#endif
-static void fatal (const char *s1, const char *s2, const char *s3) NO_RETURN;
+static _Noreturn void fatal (const char *s1, const char *s2, const char *s3);
static void error (const char *s1, const char *s2, const char *s3);
-static void pfatal_with_name (char *name) NO_RETURN;
-static void pfatal_and_delete (char *name) NO_RETURN;
+static _Noreturn void pfatal_with_name (char *name);
+static _Noreturn void pfatal_and_delete (char *name);
static char *concat (const char *s1, const char *s2, const char *s3);
static long *xmalloc (unsigned int size);
#ifdef MAIL_USE_POP
#ifndef MAIL_USE_SYSTEM_LOCK
struct stat st;
int tem;
- char *lockname, *p;
+ char *lockname;
char *tempname;
+ size_t inname_dirlen;
int desc;
#endif /* not MAIL_USE_SYSTEM_LOCK */
# 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. */
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);