quit.c revision 1.23 1 1.23 christos /* $NetBSD: quit.c,v 1.23 2006/05/24 15:53:21 christos Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.19 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.5 christos #if 0
35 1.6 tls static char sccsid[] = "@(#)quit.c 8.2 (Berkeley) 4/28/95";
36 1.5 christos #else
37 1.23 christos __RCSID("$NetBSD: quit.c,v 1.23 2006/05/24 15:53:21 christos Exp $");
38 1.5 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.3 deraadt #include "extern.h"
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Rcv -- receive mail rationally.
46 1.1 cgd *
47 1.1 cgd * Termination processing.
48 1.1 cgd */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * The "quit" command.
52 1.1 cgd */
53 1.3 deraadt int
54 1.21 christos /*ARGSUSED*/
55 1.13 wiz quitcmd(void *v)
56 1.1 cgd {
57 1.1 cgd /*
58 1.1 cgd * If we are sourcing, then return 1 so execute() can handle it.
59 1.1 cgd * Otherwise, return -1 to abort command loop.
60 1.1 cgd */
61 1.1 cgd if (sourcing)
62 1.1 cgd return 1;
63 1.1 cgd return -1;
64 1.1 cgd }
65 1.1 cgd
66 1.1 cgd /*
67 1.1 cgd * Save all of the undetermined messages at the top of "mbox"
68 1.1 cgd * Save all untouched messages back in the system mailbox.
69 1.1 cgd * Remove the system mailbox, if none saved there.
70 1.1 cgd */
71 1.3 deraadt void
72 1.13 wiz quit(void)
73 1.1 cgd {
74 1.1 cgd int mcount, p, modify, autohold, anystat, holdbit, nohold;
75 1.5 christos FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
76 1.7 lukem struct message *mp;
77 1.16 wiz int c, fd;
78 1.1 cgd struct stat minfo;
79 1.20 christos const char *mbox;
80 1.15 wiz char tempname[PATHSIZE];
81 1.1 cgd
82 1.9 bad #ifdef __GNUC__
83 1.9 bad obuf = NULL; /* XXX gcc -Wuninitialized */
84 1.9 bad #endif
85 1.9 bad
86 1.1 cgd /*
87 1.1 cgd * If we are read only, we can't do anything,
88 1.1 cgd * so just return quickly.
89 1.1 cgd */
90 1.1 cgd if (readonly)
91 1.1 cgd return;
92 1.1 cgd /*
93 1.1 cgd * If editing (not reading system mail box), then do the work
94 1.1 cgd * in edstop()
95 1.1 cgd */
96 1.1 cgd if (edit) {
97 1.1 cgd edstop();
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd
101 1.1 cgd /*
102 1.1 cgd * See if there any messages to save in mbox. If no, we
103 1.1 cgd * can save copying mbox to /tmp and back.
104 1.1 cgd *
105 1.1 cgd * Check also to see if any files need to be preserved.
106 1.1 cgd * Delete all untouched messages to keep them out of mbox.
107 1.1 cgd * If all the messages are to be preserved, just exit with
108 1.1 cgd * a message.
109 1.1 cgd */
110 1.1 cgd
111 1.1 cgd fbuf = Fopen(mailname, "r");
112 1.1 cgd if (fbuf == NULL)
113 1.1 cgd goto newmail;
114 1.5 christos if (flock(fileno(fbuf), LOCK_EX) == -1) {
115 1.5 christos nolock:
116 1.16 wiz warn("Unable to lock mailbox");
117 1.21 christos (void)Fclose(fbuf);
118 1.5 christos return;
119 1.5 christos }
120 1.5 christos if (dot_lock(mailname, 1, stdout, ".") == -1)
121 1.5 christos goto nolock;
122 1.1 cgd rbuf = NULL;
123 1.1 cgd if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
124 1.21 christos (void)printf("New mail has arrived.\n");
125 1.17 wiz (void)snprintf(tempname, sizeof(tempname),
126 1.17 wiz "%s/mail.RqXXXXXXXXXX", tmpdir);
127 1.17 wiz if ((fd = mkstemp(tempname)) == -1 ||
128 1.17 wiz (rbuf = Fdopen(fd, "w")) == NULL) {
129 1.17 wiz if (fd != -1)
130 1.21 christos (void)close(fd);
131 1.1 cgd goto newmail;
132 1.17 wiz }
133 1.1 cgd #ifdef APPEND
134 1.21 christos (void)fseek(fbuf, (long)mailsize, 0);
135 1.1 cgd while ((c = getc(fbuf)) != EOF)
136 1.18 wiz (void)putc(c, rbuf);
137 1.1 cgd #else
138 1.1 cgd p = minfo.st_size - mailsize;
139 1.1 cgd while (p-- > 0) {
140 1.1 cgd c = getc(fbuf);
141 1.1 cgd if (c == EOF)
142 1.1 cgd goto newmail;
143 1.18 wiz (void)putc(c, rbuf);
144 1.1 cgd }
145 1.1 cgd #endif
146 1.18 wiz (void)fflush(rbuf);
147 1.8 bad if (ferror(rbuf)) {
148 1.17 wiz warn("%s", tempname);
149 1.21 christos (void)Fclose(rbuf);
150 1.21 christos (void)Fclose(fbuf);
151 1.8 bad dot_unlock(mailname);
152 1.8 bad return;
153 1.8 bad }
154 1.21 christos (void)Fclose(rbuf);
155 1.17 wiz if ((rbuf = Fopen(tempname, "r")) == NULL)
156 1.1 cgd goto newmail;
157 1.21 christos (void)rm(tempname);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Adjust the message flags in each message.
162 1.1 cgd */
163 1.1 cgd
164 1.1 cgd anystat = 0;
165 1.14 wiz autohold = value("hold") != NULL;
166 1.1 cgd holdbit = autohold ? MPRESERVE : MBOX;
167 1.1 cgd nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
168 1.14 wiz if (value("keepsave") != NULL)
169 1.1 cgd nohold &= ~MSAVED;
170 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++) {
171 1.1 cgd if (mp->m_flag & MNEW) {
172 1.1 cgd mp->m_flag &= ~MNEW;
173 1.1 cgd mp->m_flag |= MSTATUS;
174 1.1 cgd }
175 1.1 cgd if (mp->m_flag & MSTATUS)
176 1.1 cgd anystat++;
177 1.1 cgd if ((mp->m_flag & MTOUCH) == 0)
178 1.1 cgd mp->m_flag |= MPRESERVE;
179 1.1 cgd if ((mp->m_flag & nohold) == 0)
180 1.1 cgd mp->m_flag |= holdbit;
181 1.1 cgd }
182 1.1 cgd modify = 0;
183 1.14 wiz if (Tflag != NULL) {
184 1.1 cgd if ((readstat = Fopen(Tflag, "w")) == NULL)
185 1.14 wiz Tflag = NULL;
186 1.1 cgd }
187 1.1 cgd for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
188 1.1 cgd if (mp->m_flag & MBOX)
189 1.1 cgd c++;
190 1.1 cgd if (mp->m_flag & MPRESERVE)
191 1.1 cgd p++;
192 1.1 cgd if (mp->m_flag & MODIFY)
193 1.1 cgd modify++;
194 1.14 wiz if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
195 1.1 cgd char *id;
196 1.1 cgd
197 1.14 wiz if ((id = hfield("article-id", mp)) != NULL)
198 1.21 christos (void)fprintf(readstat, "%s\n", id);
199 1.1 cgd }
200 1.1 cgd }
201 1.14 wiz if (Tflag != NULL)
202 1.21 christos (void)Fclose(readstat);
203 1.1 cgd if (p == msgCount && !modify && !anystat) {
204 1.21 christos (void)printf("Held %d message%s in %s\n",
205 1.1 cgd p, p == 1 ? "" : "s", mailname);
206 1.21 christos (void)Fclose(fbuf);
207 1.5 christos dot_unlock(mailname);
208 1.1 cgd return;
209 1.1 cgd }
210 1.1 cgd if (c == 0) {
211 1.1 cgd if (p != 0) {
212 1.21 christos (void)writeback(rbuf);
213 1.21 christos (void)Fclose(fbuf);
214 1.5 christos dot_unlock(mailname);
215 1.1 cgd return;
216 1.1 cgd }
217 1.1 cgd goto cream;
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd /*
221 1.1 cgd * Create another temporary file and copy user's mbox file
222 1.1 cgd * darin. If there is no mbox, copy nothing.
223 1.1 cgd * If he has specified "append" don't copy his mailbox,
224 1.1 cgd * just copy saveable entries at the end.
225 1.1 cgd */
226 1.1 cgd
227 1.1 cgd mbox = expand("&");
228 1.1 cgd mcount = c;
229 1.14 wiz if (value("append") == NULL) {
230 1.15 wiz (void)snprintf(tempname, sizeof(tempname),
231 1.15 wiz "%s/mail.RmXXXXXXXXXX", tmpdir);
232 1.15 wiz if ((fd = mkstemp(tempname)) == -1 ||
233 1.15 wiz (obuf = Fdopen(fd, "w")) == NULL) {
234 1.15 wiz warn("%s", tempname);
235 1.16 wiz if (fd != -1)
236 1.21 christos (void)close(fd);
237 1.21 christos (void)Fclose(fbuf);
238 1.5 christos dot_unlock(mailname);
239 1.1 cgd return;
240 1.1 cgd }
241 1.15 wiz if ((ibuf = Fopen(tempname, "r")) == NULL) {
242 1.15 wiz warn("%s", tempname);
243 1.21 christos (void)rm(tempname);
244 1.21 christos (void)Fclose(obuf);
245 1.21 christos (void)Fclose(fbuf);
246 1.5 christos dot_unlock(mailname);
247 1.1 cgd return;
248 1.1 cgd }
249 1.21 christos (void)rm(tempname);
250 1.1 cgd if ((abuf = Fopen(mbox, "r")) != NULL) {
251 1.1 cgd while ((c = getc(abuf)) != EOF)
252 1.18 wiz (void)putc(c, obuf);
253 1.21 christos (void)Fclose(abuf);
254 1.1 cgd }
255 1.1 cgd if (ferror(obuf)) {
256 1.15 wiz warn("%s", tempname);
257 1.21 christos (void)Fclose(ibuf);
258 1.21 christos (void)Fclose(obuf);
259 1.21 christos (void)Fclose(fbuf);
260 1.5 christos dot_unlock(mailname);
261 1.1 cgd return;
262 1.1 cgd }
263 1.21 christos (void)Fclose(obuf);
264 1.23 christos if ((fd = creat(mbox, 0600)) != -1)
265 1.22 christos (void)close(fd);
266 1.1 cgd if ((obuf = Fopen(mbox, "r+")) == NULL) {
267 1.16 wiz warn("%s", mbox);
268 1.21 christos (void)Fclose(ibuf);
269 1.21 christos (void)Fclose(fbuf);
270 1.5 christos dot_unlock(mailname);
271 1.1 cgd return;
272 1.1 cgd }
273 1.1 cgd }
274 1.5 christos else {
275 1.1 cgd if ((obuf = Fopen(mbox, "a")) == NULL) {
276 1.16 wiz warn("%s", mbox);
277 1.21 christos (void)Fclose(fbuf);
278 1.5 christos dot_unlock(mailname);
279 1.1 cgd return;
280 1.1 cgd }
281 1.21 christos (void)fchmod(fileno(obuf), 0600);
282 1.1 cgd }
283 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++)
284 1.1 cgd if (mp->m_flag & MBOX)
285 1.14 wiz if (sendmessage(mp, obuf, saveignore, NULL) < 0) {
286 1.16 wiz warn("%s", mbox);
287 1.21 christos (void)Fclose(ibuf);
288 1.21 christos (void)Fclose(obuf);
289 1.21 christos (void)Fclose(fbuf);
290 1.5 christos dot_unlock(mailname);
291 1.1 cgd return;
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd /*
295 1.1 cgd * Copy the user's old mbox contents back
296 1.1 cgd * to the end of the stuff we just saved.
297 1.1 cgd * If we are appending, this is unnecessary.
298 1.1 cgd */
299 1.1 cgd
300 1.14 wiz if (value("append") == NULL) {
301 1.1 cgd rewind(ibuf);
302 1.1 cgd c = getc(ibuf);
303 1.1 cgd while (c != EOF) {
304 1.18 wiz (void)putc(c, obuf);
305 1.1 cgd if (ferror(obuf))
306 1.1 cgd break;
307 1.1 cgd c = getc(ibuf);
308 1.1 cgd }
309 1.21 christos (void)Fclose(ibuf);
310 1.1 cgd }
311 1.21 christos (void)fflush(obuf);
312 1.8 bad if (!ferror(obuf))
313 1.8 bad trunc(obuf); /* XXX or should we truncate? */
314 1.1 cgd if (ferror(obuf)) {
315 1.16 wiz warn("%s", mbox);
316 1.21 christos (void)Fclose(obuf);
317 1.21 christos (void)Fclose(fbuf);
318 1.5 christos dot_unlock(mailname);
319 1.1 cgd return;
320 1.1 cgd }
321 1.21 christos (void)Fclose(obuf);
322 1.1 cgd if (mcount == 1)
323 1.21 christos (void)printf("Saved 1 message in mbox\n");
324 1.1 cgd else
325 1.21 christos (void)printf("Saved %d messages in mbox\n", mcount);
326 1.1 cgd
327 1.1 cgd /*
328 1.1 cgd * Now we are ready to copy back preserved files to
329 1.1 cgd * the system mailbox, if any were requested.
330 1.1 cgd */
331 1.1 cgd
332 1.1 cgd if (p != 0) {
333 1.21 christos (void)writeback(rbuf);
334 1.21 christos (void)Fclose(fbuf);
335 1.5 christos dot_unlock(mailname);
336 1.1 cgd return;
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd /*
340 1.16 wiz * Finally, remove his /var/mail file.
341 1.1 cgd * If new mail has arrived, copy it back.
342 1.1 cgd */
343 1.1 cgd
344 1.1 cgd cream:
345 1.1 cgd if (rbuf != NULL) {
346 1.1 cgd abuf = Fopen(mailname, "r+");
347 1.1 cgd if (abuf == NULL)
348 1.1 cgd goto newmail;
349 1.1 cgd while ((c = getc(rbuf)) != EOF)
350 1.18 wiz (void)putc(c, abuf);
351 1.18 wiz (void)fflush(abuf);
352 1.10 bad if (ferror(abuf)) {
353 1.16 wiz warn("%s", mailname);
354 1.21 christos (void)Fclose(abuf);
355 1.21 christos (void)Fclose(fbuf);
356 1.8 bad dot_unlock(mailname);
357 1.8 bad return;
358 1.8 bad }
359 1.21 christos (void)Fclose(rbuf);
360 1.1 cgd trunc(abuf);
361 1.21 christos (void)Fclose(abuf);
362 1.1 cgd alter(mailname);
363 1.21 christos (void)Fclose(fbuf);
364 1.5 christos dot_unlock(mailname);
365 1.1 cgd return;
366 1.1 cgd }
367 1.1 cgd demail();
368 1.21 christos (void)Fclose(fbuf);
369 1.5 christos dot_unlock(mailname);
370 1.1 cgd return;
371 1.1 cgd
372 1.1 cgd newmail:
373 1.21 christos (void)printf("Thou hast new mail.\n");
374 1.5 christos if (fbuf != NULL) {
375 1.21 christos (void)Fclose(fbuf);
376 1.5 christos dot_unlock(mailname);
377 1.5 christos }
378 1.1 cgd }
379 1.1 cgd
380 1.1 cgd /*
381 1.1 cgd * Preserve all the appropriate messages back in the system
382 1.1 cgd * mailbox, and print a nice message indicated how many were
383 1.1 cgd * saved. On any error, just return -1. Else return 0.
384 1.1 cgd * Incorporate the any new mail that we found.
385 1.1 cgd */
386 1.3 deraadt int
387 1.13 wiz writeback(FILE *res)
388 1.1 cgd {
389 1.7 lukem struct message *mp;
390 1.7 lukem int p, c;
391 1.1 cgd FILE *obuf;
392 1.1 cgd
393 1.1 cgd p = 0;
394 1.1 cgd if ((obuf = Fopen(mailname, "r+")) == NULL) {
395 1.16 wiz warn("%s", mailname);
396 1.1 cgd return(-1);
397 1.1 cgd }
398 1.1 cgd #ifndef APPEND
399 1.8 bad if (res != NULL) {
400 1.1 cgd while ((c = getc(res)) != EOF)
401 1.18 wiz (void)putc(c, obuf);
402 1.18 wiz (void)fflush(obuf);
403 1.8 bad if (ferror(obuf)) {
404 1.16 wiz warn("%s", mailname);
405 1.21 christos (void)Fclose(obuf);
406 1.8 bad return(-1);
407 1.8 bad }
408 1.8 bad }
409 1.1 cgd #endif
410 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++)
411 1.1 cgd if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
412 1.1 cgd p++;
413 1.16 wiz if (sendmessage(mp, obuf, NULL, NULL) < 0) {
414 1.16 wiz warn("%s", mailname);
415 1.21 christos (void)Fclose(obuf);
416 1.1 cgd return(-1);
417 1.1 cgd }
418 1.1 cgd }
419 1.1 cgd #ifdef APPEND
420 1.1 cgd if (res != NULL)
421 1.1 cgd while ((c = getc(res)) != EOF)
422 1.18 wiz (void)putc(c, obuf);
423 1.1 cgd #endif
424 1.21 christos (void)fflush(obuf);
425 1.8 bad if (!ferror(obuf))
426 1.10 bad trunc(obuf); /* XXX or should we truncate? */
427 1.1 cgd if (ferror(obuf)) {
428 1.16 wiz warn("%s", mailname);
429 1.21 christos (void)Fclose(obuf);
430 1.1 cgd return(-1);
431 1.1 cgd }
432 1.1 cgd if (res != NULL)
433 1.21 christos (void)Fclose(res);
434 1.21 christos (void)Fclose(obuf);
435 1.1 cgd alter(mailname);
436 1.1 cgd if (p == 1)
437 1.21 christos (void)printf("Held 1 message in %s\n", mailname);
438 1.1 cgd else
439 1.21 christos (void)printf("Held %d messages in %s\n", p, mailname);
440 1.1 cgd return(0);
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd /*
444 1.1 cgd * Terminate an editing session by attempting to write out the user's
445 1.1 cgd * file from the temporary. Save any new stuff appended to the file.
446 1.1 cgd */
447 1.3 deraadt void
448 1.13 wiz edstop(void)
449 1.1 cgd {
450 1.7 lukem int gotcha, c;
451 1.7 lukem struct message *mp;
452 1.5 christos FILE *obuf, *ibuf, *readstat = NULL;
453 1.1 cgd struct stat statb;
454 1.16 wiz char tempname[PATHSIZE];
455 1.16 wiz int fd;
456 1.1 cgd
457 1.1 cgd if (readonly)
458 1.1 cgd return;
459 1.1 cgd holdsigs();
460 1.14 wiz if (Tflag != NULL) {
461 1.1 cgd if ((readstat = Fopen(Tflag, "w")) == NULL)
462 1.14 wiz Tflag = NULL;
463 1.1 cgd }
464 1.1 cgd for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
465 1.1 cgd if (mp->m_flag & MNEW) {
466 1.1 cgd mp->m_flag &= ~MNEW;
467 1.1 cgd mp->m_flag |= MSTATUS;
468 1.1 cgd }
469 1.1 cgd if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
470 1.1 cgd gotcha++;
471 1.14 wiz if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
472 1.1 cgd char *id;
473 1.1 cgd
474 1.14 wiz if ((id = hfield("article-id", mp)) != NULL)
475 1.21 christos (void)fprintf(readstat, "%s\n", id);
476 1.1 cgd }
477 1.1 cgd }
478 1.14 wiz if (Tflag != NULL)
479 1.21 christos (void)Fclose(readstat);
480 1.14 wiz if (!gotcha || Tflag != NULL)
481 1.1 cgd goto done;
482 1.1 cgd ibuf = NULL;
483 1.1 cgd if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
484 1.16 wiz (void)snprintf(tempname, sizeof(tempname),
485 1.16 wiz "%s/mbox.XXXXXXXXXX", tmpdir);
486 1.16 wiz if ((fd = mkstemp(tempname)) == -1 ||
487 1.16 wiz (obuf = Fdopen(fd, "w")) == NULL) {
488 1.16 wiz warn("%s", tempname);
489 1.16 wiz if (fd != -1)
490 1.21 christos (void)close(fd);
491 1.1 cgd relsesigs();
492 1.1 cgd reset(0);
493 1.1 cgd }
494 1.1 cgd if ((ibuf = Fopen(mailname, "r")) == NULL) {
495 1.16 wiz warn("%s", mailname);
496 1.21 christos (void)Fclose(obuf);
497 1.21 christos (void)rm(tempname);
498 1.1 cgd relsesigs();
499 1.1 cgd reset(0);
500 1.1 cgd }
501 1.21 christos (void)fseek(ibuf, (long)mailsize, 0);
502 1.1 cgd while ((c = getc(ibuf)) != EOF)
503 1.18 wiz (void)putc(c, obuf);
504 1.18 wiz (void)fflush(obuf);
505 1.8 bad if (ferror(obuf)) {
506 1.16 wiz warn("%s", tempname);
507 1.21 christos (void)Fclose(obuf);
508 1.21 christos (void)Fclose(ibuf);
509 1.21 christos (void)rm(tempname);
510 1.8 bad relsesigs();
511 1.8 bad reset(0);
512 1.8 bad }
513 1.21 christos (void)Fclose(ibuf);
514 1.21 christos (void)Fclose(obuf);
515 1.1 cgd if ((ibuf = Fopen(tempname, "r")) == NULL) {
516 1.16 wiz warn("%s", tempname);
517 1.21 christos (void)rm(tempname);
518 1.1 cgd relsesigs();
519 1.1 cgd reset(0);
520 1.1 cgd }
521 1.21 christos (void)rm(tempname);
522 1.1 cgd }
523 1.21 christos (void)printf("\"%s\" ", mailname);
524 1.21 christos (void)fflush(stdout);
525 1.1 cgd if ((obuf = Fopen(mailname, "r+")) == NULL) {
526 1.16 wiz warn("%s", mailname);
527 1.1 cgd relsesigs();
528 1.1 cgd reset(0);
529 1.1 cgd }
530 1.1 cgd trunc(obuf);
531 1.1 cgd c = 0;
532 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++) {
533 1.1 cgd if ((mp->m_flag & MDELETED) != 0)
534 1.1 cgd continue;
535 1.1 cgd c++;
536 1.16 wiz if (sendmessage(mp, obuf, NULL, NULL) < 0) {
537 1.16 wiz warn("%s", mailname);
538 1.1 cgd relsesigs();
539 1.1 cgd reset(0);
540 1.1 cgd }
541 1.1 cgd }
542 1.1 cgd gotcha = (c == 0 && ibuf == NULL);
543 1.1 cgd if (ibuf != NULL) {
544 1.1 cgd while ((c = getc(ibuf)) != EOF)
545 1.18 wiz (void)putc(c, obuf);
546 1.21 christos (void)Fclose(ibuf);
547 1.1 cgd }
548 1.21 christos (void)fflush(obuf);
549 1.1 cgd if (ferror(obuf)) {
550 1.16 wiz warn("%s", mailname);
551 1.1 cgd relsesigs();
552 1.1 cgd reset(0);
553 1.1 cgd }
554 1.21 christos (void)Fclose(obuf);
555 1.1 cgd if (gotcha) {
556 1.21 christos (void)rm(mailname);
557 1.21 christos (void)printf("removed\n");
558 1.1 cgd } else
559 1.21 christos (void)printf("complete\n");
560 1.21 christos (void)fflush(stdout);
561 1.1 cgd
562 1.1 cgd done:
563 1.1 cgd relsesigs();
564 1.1 cgd }
565