#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
#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;
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];
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] != '/')
#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);
}
#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.
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.
/* 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);
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);
}
}
#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;
{
/* 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);
}
/* 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;
FILE *mbf;
char *getenv ();
int mbx_write ();
- PopServer server;
+ popserver server;
extern char *strerror ();
server = pop_open (0, user, 0, POP_NO_GETPASS);
* 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)
{
}
pop_retr (server, msgno, action, arg)
- PopServer server;
+ popserver server;
int (*action)();
{
extern char *strerror ();