annotate lib-src/movemail.c @ 438:84b14dcb0985 r21-2-27

Import from CVS: tag r21-2-27
author cvs
date Mon, 13 Aug 2007 11:32:25 +0200
parents 3ecd8885ac67
children 8de8e3f6228a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* movemail foo bar -- move file foo to file bar,
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
2 locking file foo.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
5 This file is part of XEmacs.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
8 under the terms of the GNU General Public License as published by the
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
10 later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
15 for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
20 Boston, MA 02111-1307, USA.
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
21
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
22 Please mail bugs and suggestions to the XEmacs maintainer.
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
23 */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
25 /* Important notice:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
26 *
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
27 * You *must* coordinate the locking method used by movemail with that
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
28 * used by your mail delivery agent, as well as that of the other mail
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
29 * user agents on your system. movemail allows you to do this at run
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
30 * time via the -m flag. Moreover, it uses a default determined by
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
31 * the MAIL_LOCK_DOT, MAIL_LOCK_LOCKF, MAIL_LOCK_FLOCK,
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
32 * MAIL_LOCK_LOCKING, and MAIL_LOCK_MMDF preprocessor settings.
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
33 */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
35 /*
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
36 * Mike Sperber <sperber@informatik.uni-tuebingen.de> reorganized
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
37 * everything that has to with locking in December 1999.
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
38 */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 * movemail will accept input filename arguments of the form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 * "po:username". This will cause movemail to open a connection to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 * a pop server running on $MAILHOST (environment variable). Movemail
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 * must be setuid to root in order to work with POP.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 * New module: popmail.c
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 * Modified routines:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 * after POP code.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 * New routines in movemail.c:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 * get_errmsg - return pointer to system error message
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 * Move all of the POP code into a separate file, "pop.c".
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 * Use strerror instead of get_errmsg.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 #define NO_SHORTNAMES /* Tell config not to load remap.h */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #define DONT_ENCAPSULATE
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
65 #include <config.h>
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 #include <sys/types.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 #include <sys/stat.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 #include <errno.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 #include "../src/sysfile.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 #include "../src/syswait.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 #ifndef WINDOWSNT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 #include "../src/systime.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 #include <stdlib.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #include "getopt.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #include "pop.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #include "../src/regex.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 extern char *optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 extern int optind, opterr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 #ifndef HAVE_STRERROR
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 char * strerror (int errnum);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 #endif /* HAVE_STRERROR */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 #ifdef MSDOS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 #undef access
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 #endif /* MSDOS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 #ifndef DIRECTORY_SEP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 #define DIRECTORY_SEP '/'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 #ifndef IS_DIRECTORY_SEP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 #ifdef WINDOWSNT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 #undef access
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 #undef unlink
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 #define fork() 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 #define sys_wait(var) (*(var) = 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 though the locking call succeeds (and indeed blocks local access from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 other NT programs). If you have direct file access using an NFS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 client or something other than Samba, the locking call might work
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 properly - make sure it does before you enable this! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 #define DISABLE_DIRECT_ACCESS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 #include <io.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 #endif /* WINDOWSNT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
115 #if defined (HAVE_UNISTD_H)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 #endif /* unistd.h */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 #ifndef F_OK
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 #define F_OK 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 #define X_OK 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 #define W_OK 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 #define R_OK 4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 #endif /* No F_OK */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
125 #if defined (HAVE_FCNTL_H)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 #endif /* fcntl.h */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
129 #ifdef HAVE_LOCKING
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 #include <sys/locking.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
133 #ifdef HAVE_MMDF
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 extern int lk_open (), lk_close ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 /* Cancel substitutions made by config.h for Emacs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 #undef open
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 #undef read
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 #undef write
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 #undef close
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 static void fatal (char *, char*);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 static void error (char *, char *, char *);
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
145 static void usage(int);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 static void pfatal_with_name (char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 static void pfatal_and_delete (char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 static char *concat (char *, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 static long *xmalloc (unsigned int);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 static int popmail (char *, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 static int pop_retr (popserver server, int msgno,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 int (*action)(char *, FILE *), void *arg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 static int mbx_write (char *, FILE *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 static int mbx_delimit_begin (FILE *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 static int mbx_delimit_end (FILE *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 static struct re_pattern_buffer* compile_regex (char* regexp_pattern);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 static int pop_search_top (popserver server, int msgno, int lines,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 struct re_pattern_buffer* regexp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 int verbose=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 int reverse=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 int keep_messages=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 struct re_pattern_buffer* regexp_pattern=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 int match_lines=10;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 #define VERBOSE(x) if (verbose) { printf x; fflush(stdout); }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 struct option longopts[] =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 { "inbox", required_argument, NULL, 'i' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 { "outfile", required_argument, NULL, 'o' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 { "password", required_argument, NULL, 'p' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 { "reverse-pop-order", no_argument, NULL, 'x' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 { "keep-messages", no_argument, NULL, 'k' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 { "regex", required_argument, NULL, 'r' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 { "match-lines", required_argument, NULL, 'l' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 #endif
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
183 { "lock-method", required_argument, NULL, 'm' },
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
184 { "help", no_argument, NULL, 'h' },
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 { "verbose", no_argument, NULL, 'v' },
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 { 0 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
189 #define DOTLOCKING 0
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
190 #define FLOCKING 1
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
191 #define LOCKFING 2
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
192 #define MMDF 3
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
193 #define LOCKING 4
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
194
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
195 #if defined(MAIL_LOCK_FLOCK) && defined(HAVE_FLOCK)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
196 #define DEFAULT_LOCKING FLOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
197 #elif defined(MAIL_LOCK_LOCKF) && defined(HAVE_LOCKF)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
198 #define DEFAULT_LOCKING LOCKFING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
199 #elif defined(MAIL_LOCK_MMDF) && defined(HAVE_MMDF)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
200 #define DEFAULT_LOCKING MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
201 #elif defined(MAIL_LOCK_LOCKING) && defined(HAVE_LOCKING)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
202 #define DEFAULT_LOCKING LOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
203 #else
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
204 #define DEFAULT_LOCKING DOTLOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
205 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
206
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
207 static void lock_dot(char *);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
208 static void unlock_dot(char *);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
209 static int parse_lock_method(char *);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
210 static char *unparse_lock_method(int);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
211
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 main (int argc, char *argv[])
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 char *inname=0, *outname=0, *poppass=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 #ifndef DISABLE_DIRECT_ACCESS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 int indesc, outdesc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 int nread;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 int status;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
222 int lock_method = DEFAULT_LOCKING;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
223
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
224 char *maybe_lock_env;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
226 maybe_lock_env = getenv("EMACSLOCKMETHOD");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
227 if (maybe_lock_env)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
228 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
229 printf("maybe-lock_env: %s\n", maybe_lock_env);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
230 lock_method = parse_lock_method(maybe_lock_env);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
231 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
233 for (;;)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 #ifdef MAIL_USE_POP
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
236 char* optstring = "i:o:m:p:l:r:xvhk";
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 #else
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
238 char* optstring = "i:o:m:vh";
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 int opt = getopt_long (argc, argv, optstring, longopts, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 if (opt == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 switch (opt)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 case 0:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 case 1: /* one of the standard arguments seen */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 if (!inname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 inname = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 else if (!outname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 outname = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 poppass = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 case 'i': /* infile */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 inname = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 case 'o': /* outfile */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 outname = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 case 'p': /* pop password */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 poppass = optarg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 case 'k': keep_messages=1; break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 case 'x': reverse = 1; break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 case 'l': /* lines to match */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 match_lines = atoi (optarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 case 'r': /* regular expression */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 regexp_pattern = compile_regex (optarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 #endif
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
279
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
280 case 'm':
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
281 lock_method = parse_lock_method(optarg);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
282 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
283 case 'h':
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
284 usage(lock_method);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
285 exit(0);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
286 case 'v':
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
287 verbose = 1;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
288 break;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 while (optind < argc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 if (!inname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 inname = argv[optind];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 else if (!outname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 outname = argv[optind];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 poppass = argv[optind];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 optind++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 if (!inname || !outname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 {
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
305 usage(lock_method);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
309 #ifdef HAVE_MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
310 if (lock_method == MMDF)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
311 mmdf_init (argv[0]);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 if (*outname == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 fatal ("Destination file name is empty", 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
317 VERBOSE(("checking access to output file\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 /* Check access to output file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 pfatal_with_name (outname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 /* Also check that outname's directory is writable to the real uid. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 char *buf = (char *) xmalloc (strlen (outname) + 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 char *cp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 strcpy (buf, outname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 cp = buf + strlen (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 while (cp > buf && !IS_DIRECTORY_SEP (cp[-1]))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 *--cp = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 if (cp == buf)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 *cp++ = '.';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 if (access (buf, W_OK) != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 pfatal_with_name (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 free (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 if (!strncmp (inname, "po:", 3))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 int retcode = popmail (inname + 3, outname, poppass);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 exit (retcode);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 #ifndef WINDOWSNT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 setuid (getuid ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 #endif /* MAIL_USE_POP */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 #ifndef DISABLE_DIRECT_ACCESS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 /* Check access to input file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 if (access (inname, R_OK | W_OK) != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 pfatal_with_name (inname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
355
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
356 if (fork () == 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
357 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
358 setuid (getuid ());
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
359
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
360 VERBOSE(("opening input file\n"));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
361
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
362 switch (lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
363 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
364 case DOTLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
365 indesc = open (inname, O_RDONLY);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
366 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
367 #ifdef HAVE_LOCKF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
368 case LOCKFING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
369 indesc = open (inname, O_RDWR);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
370 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
371 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
372 #ifdef HAVE_FLOCK
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
373 case FLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
374 indesc = open (inname, O_RDWR);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
375 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
376 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
377 #ifdef HAVE_LOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
378 case LOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
379 indesc = open (inname, O_RDWR);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
380 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
381 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
382 #ifdef HAVE_MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
383 case MMDF:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
384 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
385 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
386 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
387 default: abort();
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
388 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
389
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
390 if (indesc < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
391 pfatal_with_name (inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
392
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
393 #ifdef HAVE_UMASK
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
394 /* In case movemail is setuid to root, make sure the user can
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
395 read the output file. */
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
396 umask (umask (0) & 0333);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
397 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
398
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
399 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
400 if (outdesc < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
401 pfatal_with_name (outname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
402
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
403 VERBOSE(("locking input file\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
405 switch (lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
406 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
407 #ifdef HAVE_LOCKF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
408 case LOCKFING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
409 if (lockf (indesc, F_LOCK, 0) < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
410 pfatal_with_name (inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
411 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
412 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
413 #ifdef HAVE_FLOCK
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
414 case FLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
415 if (flock (indesc, LOCK_EX) < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
416 pfatal_with_name (inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
417 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
418 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
419 #ifdef HAVE_LOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
420 case LOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
421 if (locking (indesc, LK_RLCK, -1L) < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
422 pfatal_with_name (inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
423 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
424 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
425 case DOTLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
426 lock_dot(inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
427 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
428 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
429
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
430 VERBOSE(("copying input file to output file\n"));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
431
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
432 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
433 char buf[1024];
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
434
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
435 while (1)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
436 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
437 nread = read (indesc, buf, sizeof buf);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
438 if (nread != write (outdesc, buf, nread))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
439 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
440 int saved_errno = errno;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
441 unlink (outname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
442 errno = saved_errno;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
443 pfatal_with_name (outname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
444 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
445 if (nread < sizeof buf)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
446 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
447 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
448 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
449
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
450 #ifdef HAVE_FSYNC
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
451 if (fsync (outdesc) < 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
452 pfatal_and_delete (outname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
453 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
454
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
455 /* Check to make sure no errors before we zap the inbox. */
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
456 if (close (outdesc) != 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
457 pfatal_and_delete (outname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
458
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
459 VERBOSE(("deleting or truncating input file\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
461 switch (lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
462 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
463 case LOCKFING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
464 case FLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
465 case LOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
466 #ifdef HAVE_FTRUNCATE
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
467 ftruncate (indesc, 0L);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
468 #else
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
469 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
470 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
471 close (indesc);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
472 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
473 #ifdef HAVE_MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
474 case MMDF:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
475 lk_close (indesc, 0, 0, 0);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
476 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
477 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
478 case DOTLOCKING:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
479 creat (inname, 0600);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
480 break;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
481 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
482
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
483 exit (0);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
484 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
486 wait (&status);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
487 if (!WIFEXITED (status))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
488 exit (1);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
489 else if (WEXITSTATUS (status) != 0)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
490 exit (WEXITSTATUS (status));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
491
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
492 if (lock_method == DOTLOCKING)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
493 unlock_dot(inname);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
494
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
495 #endif /* not DISABLE_DIRECT_ACCESS */
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
496
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
497 return 0;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
498 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
499
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
500 static void
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
501 usage(int lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
502 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
503 printf ("Usage: movemail [-rvxkh] [-l lines ] [-m method ] [-i] inbox [-o] destfile [[-p] POP-password]\n");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
504 printf("where method is one of: dot");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
505 #ifdef HAVE_LOCKF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
506 printf(", lockf");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
507 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
508 #ifdef HAVE_FLOCK
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
509 printf(", flock");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
510 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
511 #ifdef HAVE_MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
512 printf(", mmdf");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
513 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
514 #ifdef HAVE_LOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
515 printf(", locking");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
516 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
517 printf("\nDefault is: %s\n", unparse_lock_method(lock_method));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
518
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
519 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
521 static char *
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
522 unparse_lock_method(int lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
523 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
524 switch (lock_method)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
525 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
526 case DOTLOCKING: return "dot";
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
527 case FLOCKING: return "flock";
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
528 case LOCKFING: return "lockf";
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
529 case LOCKING: return "locking";
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
530 case MMDF: return "mmdf";
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
531 default: abort();return 0;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
532 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
533 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
535 static int
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
536 parse_lock_method(char *method_name)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
537 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
538 if (!strcmp("dot", method_name) || !strcmp("file", method_name))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
539 return DOTLOCKING;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
540 #ifdef HAVE_LOCKF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
541 else if (!strcmp("lockf", method_name))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
542 return LOCKFING;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
543 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
544 #ifdef HAVE_FLOCK
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
545 else if (!strcmp("flock", method_name))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
546 return FLOCKING;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
547 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
548 #ifdef HAVE_MMDF
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
549 else if (!strcmp("mmdf", method_name))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
550 return MMDF;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
551 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
552 #ifdef HAVE_LOCKING
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
553 else if (!strcmp("locking", method_name))
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
554 return LOCKING;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
555 #endif
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
556 else
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
557 fatal("invalid lock method: %s", method_name);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
558 return 0; /* unreached */
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
559 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
560
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
561 static char *
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
562 dot_filename(char *filename)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
563 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
564 return concat (filename, ".lock", "");
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
565 }
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
566
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
567 static char *dotlock_filename = NULL;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
568
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
569 static void
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
570 lock_dot(char *filename)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
571 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
572 struct stat st;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
573 long now;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
574 int tem;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
575 char *lockname, *p;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
576 char *tempname;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
577 int desc;
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
578
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
579 dotlock_filename = (char *) xmalloc(strlen(filename) + 1);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
580
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
581 /* Use a lock file named after our first argument with .lock appended:
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
582 If it exists, the mail file is locked. */
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
583
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
584 lockname = dot_filename(filename);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
585 tempname = (char *) xmalloc (strlen (filename) + strlen ("EXXXXXX") + 1);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
586 strcpy (tempname, filename);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 p = tempname + strlen (tempname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 p--;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 *p = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 strcpy (p, "EXXXXXX");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 mktemp (tempname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 unlink (tempname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
595 for (;;)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 /* Create the lock file, but not under the lock file name. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 /* Give up if cannot do that. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 if (desc < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 char *message = (char *) xmalloc (strlen (tempname) + 50);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 sprintf (message, "%s--see source file lib-src/movemail.c",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 tempname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 pfatal_with_name (message);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 close (desc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 tem = link (tempname, lockname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 unlink (tempname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 if (tem >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 sleep (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 /* If lock file is five minutes old, unlock it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 Five minutes should be good enough to cope with crashes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 and wedgitude, and long enough to avoid being fooled
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 by time differences between machines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 if (stat (lockname, &st) >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 now = time (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 if (st.st_ctime < now - 300)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 unlink (lockname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 }
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
626 strcpy(dotlock_filename, filename);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
627 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
629 static void
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
630 unlock_dot(char *filename)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
631 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
632 unlink(dot_filename(filename));
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
633 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
635 static void
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
636 maybe_unlock_dot(void)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
637 {
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
638 if (dotlock_filename)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
639 unlock_dot(dotlock_filename);
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
640 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 /* Print error message and exit. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 fatal (char *s1, char *s2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 {
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
647 maybe_unlock_dot();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 error (s1, s2, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 error (char *s1, char *s2, char *s3)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 fprintf (stderr, "movemail: ");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 fprintf (stderr, s1, s2, s3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 fprintf (stderr, "\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 pfatal_with_name (char *name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 char *s = concat ("", strerror (errno), " for %s");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 fatal (s, name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 pfatal_and_delete (char *name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 char *s = concat ("", strerror (errno), " for %s");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 unlink (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 fatal (s, name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 static char *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 concat (char *s1, char *s2, char *s3)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 strcpy (result, s1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 strcpy (result + len1, s2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 strcpy (result + len1 + len2, s3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 *(result + len1 + len2 + len3) = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 /* Like malloc but get fatal error if memory is exhausted. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 static long *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 xmalloc (unsigned int size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 long *result = (long *) malloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 if (!result)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 fatal ("virtual memory exhausted", 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 }
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
703
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 /* This is the guts of the interface to the Post Office Protocol. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706 #ifdef MAIL_USE_POP
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 #ifndef WINDOWSNT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709 #include <sys/socket.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 #include <netinet/in.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 #include <netdb.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 #undef _WINSOCKAPI_
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 #include <winsock.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 #include <pwd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 #define POP_ERROR (-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 #define POP_RETRIEVED (0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 #define POP_DONE (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 char *progname;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 FILE *sfi;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 FILE *sfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 char ibuffer[BUFSIZ];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 char obuffer[BUFSIZ];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 char Errmsg[80];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 popmail (char *user, char *outfile, char *password)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 int nmsgs, nbytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 register int i, idx;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735 int mbfi;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 short* retrieved_list;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 FILE *mbf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 popserver server;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
740 VERBOSE(("opening server\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 server = pop_open (0, user, password, POP_NO_GETPASS);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 if (! server)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 error (pop_error, NULL, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
748 VERBOSE(("stat'ing messages\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 if (pop_stat (server, &nmsgs, &nbytes))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 error (pop_error, NULL, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 if (!nmsgs)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 VERBOSE(("closing server\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 return (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 /* build a retrieved table */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 retrieved_list = (short*) xmalloc (sizeof (short) * (nmsgs+1));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 memset (retrieved_list, 0, sizeof (short) * (nmsgs+1));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 if (mbfi < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 error ("Error in open: %s, %s", strerror (errno), outfile);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 #if !defined(__CYGWIN32__) && !defined(WINDOWSNT)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 fchown (mbfi, getuid (), -1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 if ((mbf = fdopen (mbfi, "wb")) == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 error ("Error in fdopen: %s", strerror (errno), NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 close (mbfi);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782 unlink (outfile);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 for (idx = 0; idx < nmsgs; idx++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 i = reverse ? nmsgs - idx : idx + 1;
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
789 VERBOSE(("checking message %d \n", i));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 if (!regexp_pattern
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 pop_search_top (server, i, match_lines, regexp_pattern) == POP_RETRIEVED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 {
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
795 VERBOSE(("retrieving message %d \n", i));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 mbx_delimit_begin (mbf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 if (pop_retr (server, i, mbx_write, mbf) != POP_RETRIEVED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 error (Errmsg, NULL, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800 close (mbfi);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 retrieved_list[i]=1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 mbx_delimit_end (mbf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 fflush (mbf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 if (ferror (mbf))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 error ("Error in fflush: %s", strerror (errno), NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 close (mbfi);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 /* On AFS, a call to write only modifies the file in the local
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 * workstation's AFS cache. The changes are not written to the server
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 * until a call to fsync or close is made. Users with AFS home
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 * directories have lost mail when over quota because these checks were
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 * not made in previous versions of movemail. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
824 #ifdef HAVE_FSYNC
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 if (fsync (mbfi) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 error ("Error in fsync: %s", strerror (errno), NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 if (close (mbfi) == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834 error ("Error in close: %s", strerror (errno), NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 if (!keep_messages)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 for (i = 1; i <= nmsgs; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 if (retrieved_list[i] == 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 {
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
844 VERBOSE(("deleting message %d \n", i));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 if (pop_delete (server, i))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 error (pop_error, NULL, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 VERBOSE(("closing server \n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 if (pop_quit (server))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 error (pop_error, NULL, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 return (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 return (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866 pop_retr (popserver server, int msgno, int (*action)(char *, FILE *), void *arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 char *line;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869 int ret;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 if (pop_retrieve_first (server, msgno, &line))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 strncpy (Errmsg, pop_error, sizeof (Errmsg));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 Errmsg[sizeof (Errmsg)-1] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 while (! (ret = pop_retrieve_next (server, &line)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 if (! line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 if ((*action)(line, arg) != POP_RETRIEVED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 strcpy (Errmsg, strerror (errno));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 if (ret)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 strncpy (Errmsg, pop_error, sizeof (Errmsg));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 Errmsg[sizeof (Errmsg)-1] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 return (POP_RETRIEVED);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 /* search the top lines of each message looking for a match */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 pop_search_top (popserver server, int msgno, int lines, struct re_pattern_buffer* regexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 char *line;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 int ret;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 int match = POP_DONE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 if (pop_top_first (server, msgno, lines, &line))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911 strncpy (Errmsg, pop_error, sizeof (Errmsg));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 Errmsg[sizeof (Errmsg)-1] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 while (! (ret = pop_top_next (server, &line)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918 if (! line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 /* VERBOSE (("checking %s\n", line));*/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 if (match != POP_RETRIEVED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 if ((ret = re_match (regexp, line, strlen (line), 0, 0)) == -2 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 strcpy (Errmsg, "error in regular expression");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 pop_close (server);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930 else if (ret >=0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 match = POP_RETRIEVED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937 if (ret)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 strncpy (Errmsg, pop_error, sizeof (Errmsg));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940 Errmsg[sizeof (Errmsg)-1] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 return match;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
946
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
947 /* Do this as a macro instead of using strcmp to save on execution time. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 #define IS_FROM_LINE(a) ((a[0] == 'F') \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 && (a[1] == 'r') \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 && (a[2] == 'o') \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 && (a[3] == 'm') \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952 && (a[4] == ' '))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
954 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 mbx_write (char *line, FILE *mbf)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 if (IS_FROM_LINE (line))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
959 if (fputc ('>', mbf) == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
960 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962 if (fputs (line, mbf) == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 if (fputc (0x0a, mbf) == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 return (POP_RETRIEVED);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
967 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 mbx_delimit_begin (FILE *mbf)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974 return (POP_RETRIEVED);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978 mbx_delimit_end (FILE *mbf)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 if (putc ('\037', mbf) == EOF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981 return (POP_ERROR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982 return (POP_RETRIEVED);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
984
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
986 expression, into a real regular expression by compiling it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 static struct re_pattern_buffer*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988 compile_regex (char* pattern)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990 char *err;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
991 struct re_pattern_buffer *patbuf=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993 patbuf = (struct re_pattern_buffer*) xmalloc (sizeof (struct re_pattern_buffer));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994 patbuf->translate = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
995 patbuf->fastmap = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
996 patbuf->buffer = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
997 patbuf->allocated = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
999 err = (char*) re_compile_pattern (pattern, strlen (pattern), patbuf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000 if (err != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002 error ("%s while compiling pattern", err, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1004 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1005
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1006 return patbuf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1011 #endif /* MAIL_USE_POP */
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
1012
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 #ifndef HAVE_STRERROR
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014 char *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 strerror (int errnum)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1017 extern char *sys_errlist[];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1018 extern int sys_nerr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1019
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 if (errnum >= 0 && errnum < sys_nerr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 return sys_errlist[errnum];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022 return (char *) "Unknown error";
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025 #endif /* ! HAVE_STRERROR */