]> code.delx.au - gnu-emacs/blobdiff - lib-src/movemail.c
Update FSF's address.
[gnu-emacs] / lib-src / movemail.c
index 5c5b85ce921c3960ce0610d7ae4b3260afb41f9f..0286b65af6511f599ccb707d84e5b3c33210d599 100644 (file)
@@ -58,6 +58,7 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/file.h>
+#include <stdio.h>
 #include <errno.h>
 #include <../src/syswait.h>
 #ifdef MAIL_USE_POP
@@ -105,15 +106,27 @@ extern int lk_open (), lk_close ();
 #undef write
 #undef close
 
-char *concat ();
-char *xmalloc ();
 #ifndef errno
 extern int errno;
 #endif
+char *strerror ();
+
+void fatal ();
+void error ();
+void pfatal_with_name ();
+void pfatal_and_delete ();
+char *concat ();
+long *xmalloc ();
+int popmail ();
+int pop_retr ();
+int mbx_write ();
+int mbx_delimit_begin ();
+int mbx_delimit_end ();
 
 /* Nonzero means this is name of a lock file to delete on fatal error.  */
 char *delete_lockname;
 
+int
 main (argc, argv)
      int argc;
      char **argv;
@@ -135,7 +148,10 @@ main (argc, argv)
   delete_lockname = 0;
 
   if (argc < 3)
-    fatal ("two arguments required");
+    {
+      fprintf (stderr, "Usage: movemail inbox destfile\n");
+      exit(1);
+    }
 
   inname = argv[1];
   outname = argv[2];
@@ -144,14 +160,17 @@ main (argc, argv)
   mmdf_init (argv[0]);
 #endif
 
+  if (*outname == 0)
+    fatal ("Destination file name is empty", 0);
+
   /* Check access to output file.  */
   if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
     pfatal_with_name (outname);
 
-  /* Also check that outname's directory is writeable to the real uid.  */
+  /* Also check that outname's directory is writable to the real uid.  */
   {
     char *buf = (char *) xmalloc (strlen (outname) + 1);
-    char *p, q;
+    char *p;
     strcpy (buf, outname);
     p = buf + strlen (buf);
     while (p > buf && p[-1] != '/')
@@ -166,13 +185,9 @@ main (argc, argv)
 #ifdef MAIL_USE_POP
   if (!strncmp (inname, "po:", 3))
     {
-      int status; char *user;
-
-      for (user = &inname[strlen (inname) - 1]; user >= inname; user--)
-       if (*user == ':')
-         break;
+      int status;
 
-      status = popmail (user, outname);
+      status = popmail (inname + 3, outname);
       exit (status);
     }
 
@@ -185,7 +200,7 @@ main (argc, argv)
 
 #ifndef MAIL_USE_MMDF
 #ifndef MAIL_USE_SYSTEM_LOCK
-  /* Use a lock file named /usr/spool/mail/$USER.lock:
+  /* Use a lock file named after our first argument with .lock appended:
      If it exists, the mail file is locked.  */
   /* Note: this locking mechanism is *required* by the mailer
      (on systems which use it) to prevent loss of mail.
@@ -194,7 +209,7 @@ main (argc, argv)
      WILL occasionally cause loss of mail due to timing errors!
 
      So, if creation of the lock file fails
-     due to access permission on /usr/spool/mail,
+     due to access permission on the mail spool directory,
      you simply MUST change the permission
      and/or make movemail a setgid program
      so it can create lock files properly.
@@ -225,7 +240,12 @@ main (argc, argv)
       /* Give up if cannot do that.  */
       desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
       if (desc < 0)
-       pfatal_with_name ("lock file--see source file lib-src/movemail.c");
+       {
+         char *message = (char *) xmalloc (strlen (tempname) + 50);
+         sprintf (message, "%s--see source file lib-src/movemail.c",
+                  tempname);
+         pfatal_with_name (message);
+       }
       close (desc);
 
       tem = link (tempname, lockname);
@@ -234,11 +254,14 @@ main (argc, argv)
        break;
       sleep (1);
 
-      /* If lock file is a minute old, unlock it.  */
+      /* If lock file is five minutes old, unlock it.
+        Five minutes should be good enough to cope with crashes
+        and wedgitude, and long enough to avoid being fooled
+        by time differences between machines.  */
       if (stat (lockname, &st) >= 0)
        {
          now = time (0);
-         if (st.st_ctime < now - 60)
+         if (st.st_ctime < now - 300)
            unlink (lockname);
        }
     }
@@ -351,11 +374,12 @@ main (argc, argv)
 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
   unlink (lockname);
 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
-  exit (0);
+  return 0;
 }
 \f
 /* Print error message and exit.  */
 
+void
 fatal (s1, s2)
      char *s1, *s2;
 {
@@ -367,33 +391,28 @@ fatal (s1, s2)
 
 /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
 
+void
 error (s1, s2, s3)
      char *s1, *s2, *s3;
 {
-  printf ("movemail: ");
-  printf (s1, s2, s3);
-  printf ("\n");
+  fprintf (stderr, "movemail: ");
+  fprintf (stderr, s1, s2, s3);
+  fprintf (stderr, "\n");
 }
 
+void
 pfatal_with_name (name)
      char *name;
 {
-  extern int errno;
-  extern char *strerror ();
-  char *s;
-
-  s = concat ("", strerror (errno), " for %s");
+  char *s = concat ("", strerror (errno), " for %s");
   fatal (s, name);
 }
 
+void
 pfatal_and_delete (name)
      char *name;
 {
-  extern int errno;
-  extern char *strerror ();
-  char *s;
-
-  s = concat ("", strerror (errno), " for %s");
+  char *s = concat ("", strerror (errno), " for %s");
   unlink (name);
   fatal (s, name);
 }
@@ -417,11 +436,11 @@ concat (s1, s2, s3)
 
 /* Like malloc but get fatal error if memory is exhausted.  */
 
-char *
+long *
 xmalloc (size)
      unsigned size;
 {
-  char *result = (char *) malloc (size);
+  long *result = (long *) malloc (size);
   if (!result)
     fatal ("virtual memory exhausted", 0);
   return result;
@@ -467,7 +486,7 @@ popmail (user, outfile)
   FILE *mbf;
   char *getenv ();
   int mbx_write ();
-  PopServer server;
+  popserver server;
   extern char *strerror ();
 
   server = pop_open (0, user, 0, POP_NO_GETPASS);
@@ -533,11 +552,13 @@ popmail (user, outfile)
    *      directories have lost mail when over quota because these checks were
    *      not made in previous versions of movemail. */
 
+#ifdef BSD
   if (fsync (mbfi) < 0)
     {
       error ("Error in fsync: %s", strerror (errno));
       return (1);
     }
+#endif
 
   if (close (mbfi) == -1)
     {
@@ -565,7 +586,7 @@ popmail (user, outfile)
 }
 
 pop_retr (server, msgno, action, arg)
-     PopServer server;
+     popserver server;
      int (*action)();
 {
   extern char *strerror ();