Improvements to vc-bzr conffile handling and pull/merge support.
[bpt/emacs.git] / lib-src / movemail.c
index 7333034..682aa10 100644 (file)
@@ -1,7 +1,8 @@
 /* movemail foo bar -- move file foo to file bar,
    locking file foo the way /bin/mail respects.
-   Copyright (C) 1986, 1992, 1993, 1994, 1996, 1999, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
+
+Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2011
+  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -63,12 +64,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <time.h>
 
 #include <getopt.h>
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #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"
@@ -140,25 +142,23 @@ static char *mail_spool_name ();
 #endif
 #endif
 
+#ifndef HAVE_STRERROR
 char *strerror (int);
-#ifdef HAVE_INDEX
-extern char *index (const char *, int);
-#endif
-#ifdef HAVE_RINDEX
-extern char *rindex (const char *, int);
 #endif
 
-void fatal (char *s1, char *s2, char *s3);
-void error (char *s1, char *s2, char *s3);
-void pfatal_with_name (char *name);
-void pfatal_and_delete (char *name);
-char *concat (char *s1, char *s2, char *s3);
-long *xmalloc (unsigned int size);
-int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order);
-int pop_retr (popserver server, int msgno, FILE *arg);
-int mbx_write (char *line, int len, FILE *mbf);
-int mbx_delimit_begin (FILE *mbf);
-int mbx_delimit_end (FILE *mbf);
+static void fatal (const char *s1, const char *s2, const char *s3) NO_RETURN;
+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 char *concat (const char *s1, const char *s2, const char *s3);
+static long *xmalloc (unsigned int size);
+#ifdef MAIL_USE_POP
+static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order);
+static int pop_retr (popserver server, int msgno, FILE *arg);
+static int mbx_write (char *line, int len, FILE *mbf);
+static int mbx_delimit_begin (FILE *mbf);
+static int mbx_delimit_end (FILE *mbf);
+#endif
 
 /* Nonzero means this is name of a lock file to delete on fatal error.  */
 char *delete_lockname;
@@ -168,7 +168,7 @@ main (int argc, char **argv)
 {
   char *inname, *outname;
   int indesc, outdesc;
-  int nread;
+  ssize_t nread;
   int status;
   int c, preserve_mail = 0;
 
@@ -355,7 +355,7 @@ main (int argc, char **argv)
       time_t touched_lock, now;
 #endif
 
-      if (setuid (getuid ()) < 0 || setegid (real_gid) < 0)
+      if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
        fatal ("Failed to drop privileges", 0, 0);
 
 #ifndef MAIL_USE_MMDF
@@ -382,7 +382,7 @@ main (int argc, char **argv)
       if (outdesc < 0)
        pfatal_with_name (outname);
 
-      if (setegid (priv_gid) < 0)
+      if (setregid (-1, priv_gid) < 0)
        fatal ("Failed to regain privileges", 0, 0);
 
       /* This label exists so we can retry locking
@@ -479,7 +479,7 @@ main (int argc, char **argv)
 #endif
 
       /* Prevent symlink attacks truncating other users' mailboxes */
-      if (setegid (real_gid) < 0)
+      if (setregid (-1, real_gid) < 0)
        fatal ("Failed to drop privileges", 0, 0);
 
       /* Check to make sure no errors before we zap the inbox.  */
@@ -489,7 +489,8 @@ main (int argc, char **argv)
 #ifdef MAIL_USE_SYSTEM_LOCK
       if (! preserve_mail)
        {
-         ftruncate (indesc, 0L);
+         if (ftruncate (indesc, 0L) != 0)
+           pfatal_with_name (inname);
        }
 #endif /* MAIL_USE_SYSTEM_LOCK */
 
@@ -514,7 +515,7 @@ main (int argc, char **argv)
 #endif /* not MAIL_USE_SYSTEM_LOCK */
 
       /* End of mailbox truncation */
-      if (setegid (priv_gid) < 0)
+      if (setregid (-1, priv_gid) < 0)
        fatal ("Failed to regain privileges", 0, 0);
 
 #ifdef MAIL_USE_MAILLOCK
@@ -550,14 +551,13 @@ main (int argc, char **argv)
    string-comparing the two paths, because one or both of them might
    be symbolic links pointing to some other directory. */
 static char *
-mail_spool_name (inname)
-     char *inname;
+mail_spool_name (char *inname)
 {
   struct stat stat1, stat2;
   char *indir, *fname;
   int status;
 
-  if (! (fname = rindex (inname, '/')))
+  if (! (fname = strrchr (inname, '/')))
     return NULL;
 
   fname++;
@@ -587,8 +587,8 @@ mail_spool_name (inname)
 \f
 /* Print error message and exit.  */
 
-void
-fatal (char *s1, char *s2, char *s3)
+static void
+fatal (const char *s1, const char *s2, const char *s3)
 {
   if (delete_lockname)
     unlink (delete_lockname);
@@ -599,8 +599,8 @@ fatal (char *s1, char *s2, char *s3)
 /* Print error message.  `s1' is printf control string, `s2' and `s3'
    are args for it or null. */
 
-void
-error (char *s1, char *s2, char *s3)
+static void
+error (const char *s1, const char *s2, const char *s3)
 {
   fprintf (stderr, "movemail: ");
   if (s3)
@@ -612,13 +612,13 @@ error (char *s1, char *s2, char *s3)
   fprintf (stderr, "\n");
 }
 
-void
+static void
 pfatal_with_name (char *name)
 {
   fatal ("%s for %s", strerror (errno), name);
 }
 
-void
+static void
 pfatal_and_delete (char *name)
 {
   char *s = strerror (errno);
@@ -628,10 +628,10 @@ pfatal_and_delete (char *name)
 
 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
 
-char *
-concat (char *s1, char *s2, char *s3)
+static char *
+concat (const char *s1, const char *s2, const char *s3)
 {
-  int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
+  size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
 
   strcpy (result, s1);
@@ -644,7 +644,7 @@ concat (char *s1, char *s2, char *s3)
 
 /* Like malloc but get fatal error if memory is exhausted.  */
 
-long *
+static long *
 xmalloc (unsigned int size)
 {
   long *result = (long *) malloc (size);
@@ -694,7 +694,7 @@ char Errmsg[200];           /* POP errors, at least, can exceed
  * Return a value suitable for passing to `exit'.
  */
 
-int
+static int
 popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order)
 {
   int nmsgs, nbytes;
@@ -707,7 +707,7 @@ popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse
   char *user, *hostname;
 
   user = mailbox;
-  if ((hostname = index(mailbox, ':')))
+  if ((hostname = strchr (mailbox, ':')))
     *hostname++ = '\0';
 
   server = pop_open (hostname, user, password, POP_NO_GETPASS);
@@ -820,10 +820,9 @@ popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse
   return EXIT_SUCCESS;
 }
 
-int
+static int
 pop_retr (popserver server, int msgno, FILE *arg)
 {
-  extern char *strerror (int);
   char *line;
   int ret;
 
@@ -868,7 +867,7 @@ pop_retr (popserver server, int msgno, FILE *arg)
                         && (a[3] == 'm') \
                         && (a[4] == ' '))
 
-int
+static int
 mbx_write (char *line, int len, FILE *mbf)
 {
 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
@@ -892,7 +891,7 @@ mbx_write (char *line, int len, FILE *mbf)
   return (OK);
 }
 
-int
+static int
 mbx_delimit_begin (FILE *mbf)
 {
   time_t now;
@@ -909,7 +908,7 @@ mbx_delimit_begin (FILE *mbf)
   return (OK);
 }
 
-int
+static int
 mbx_delimit_end (FILE *mbf)
 {
   if (putc ('\n', mbf) == EOF)
@@ -934,7 +933,5 @@ strerror (errnum)
 
 #endif /* ! HAVE_STRERROR */
 
-/* arch-tag: 1c323112-41fe-4fe5-8de9-494de631f73f
-   (do not change this comment) */
 
 /* movemail.c ends here */