2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)quit.c 8.2 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.bin/mail/quit.c,v 1.2.6.3 2003/01/06 05:46:03 mikeh Exp $
35 * $DragonFly: src/usr.bin/mail/quit.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
42 static void edstop(void);
43 static int writeback(FILE *res);
46 * Rcv -- receive mail rationally.
48 * Termination processing.
58 * If we are sourcing, then return 1 so execute() can handle it.
59 * Otherwise, return -1 to abort command loop.
67 * Save all of the undetermined messages at the top of "mbox"
68 * Save all untouched messages back in the system mailbox.
69 * Remove the system mailbox, if none saved there.
74 int mcount, p, modify, autohold, anystat, holdbit, nohold;
75 FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
79 char *mbox, tempname[PATHSIZE];
82 * If we are read only, we can't do anything,
83 * so just return quickly.
88 * If editing (not reading system mail box), then do the work
97 * See if there any messages to save in mbox. If no, we
98 * can save copying mbox to /tmp and back.
100 * Check also to see if any files need to be preserved.
101 * Delete all untouched messages to keep them out of mbox.
102 * If all the messages are to be preserved, just exit with
106 fbuf = Fopen(mailname, "r");
109 flock(fileno(fbuf), LOCK_EX);
111 if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
112 printf("New mail has arrived.\n");
113 snprintf(tempname, sizeof(tempname), "%s/mail.RqXXXXXXXXXX",
115 if ((fd = mkstemp(tempname)) == -1 ||
116 (rbuf = Fdopen(fd, "w")) == NULL)
119 fseeko(fbuf, mailsize, SEEK_SET);
120 while ((c = getc(fbuf)) != EOF)
123 p = minfo.st_size - mailsize;
132 if ((rbuf = Fopen(tempname, "r")) == NULL)
138 * Adjust the message flags in each message.
142 autohold = value("hold") != NULL;
143 holdbit = autohold ? MPRESERVE : MBOX;
144 nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
145 if (value("keepsave") != NULL)
147 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
148 if (mp->m_flag & MNEW) {
150 mp->m_flag |= MSTATUS;
152 if (mp->m_flag & MSTATUS)
154 if ((mp->m_flag & MTOUCH) == 0)
155 mp->m_flag |= MPRESERVE;
156 if ((mp->m_flag & nohold) == 0)
157 mp->m_flag |= holdbit;
161 if ((readstat = Fopen(Tflag, "w")) == NULL)
164 for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
165 if (mp->m_flag & MBOX)
167 if (mp->m_flag & MPRESERVE)
169 if (mp->m_flag & MODIFY)
171 if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
174 if ((id = hfield("article-id", mp)) != NULL)
175 fprintf(readstat, "%s\n", id);
180 if (p == msgCount && !modify && !anystat) {
181 printf("Held %d message%s in %s\n",
182 p, p == 1 ? "" : "s", mailname);
196 * Create another temporary file and copy user's mbox file
197 * darin. If there is no mbox, copy nothing.
198 * If he has specified "append" don't copy his mailbox,
199 * just copy saveable entries at the end.
204 if (value("append") == NULL) {
205 snprintf(tempname, sizeof(tempname), "%s/mail.RmXXXXXXXXXX",
207 if ((fd = mkstemp(tempname)) == -1 ||
208 (obuf = Fdopen(fd, "w")) == NULL) {
209 warn("%s", tempname);
213 if ((ibuf = Fopen(tempname, "r")) == NULL) {
214 warn("%s", tempname);
221 if ((abuf = Fopen(mbox, "r")) != NULL) {
222 while ((c = getc(abuf)) != EOF)
227 warnx("%s", tempname);
234 close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600));
235 if ((obuf = Fopen(mbox, "r+")) == NULL) {
242 if (value("append") != NULL) {
243 if ((obuf = Fopen(mbox, "a")) == NULL) {
248 fchmod(fileno(obuf), 0600);
250 for (mp = &message[0]; mp < &message[msgCount]; mp++)
251 if (mp->m_flag & MBOX)
252 if (sendmessage(mp, obuf, saveignore, NULL) < 0) {
261 * Copy the user's old mbox contents back
262 * to the end of the stuff we just saved.
263 * If we are appending, this is unnecessary.
266 if (value("append") == NULL) {
287 printf("Saved 1 message in mbox\n");
289 printf("Saved %d messages in mbox\n", mcount);
292 * Now we are ready to copy back preserved files to
293 * the system mailbox, if any were requested.
303 * Finally, remove his /var/mail file.
304 * If new mail has arrived, copy it back.
309 abuf = Fopen(mailname, "r+");
312 while ((c = getc(rbuf)) != EOF)
326 printf("Thou hast new mail.\n");
332 * Preserve all the appropriate messages back in the system
333 * mailbox, and print a nice message indicated how many were
334 * saved. On any error, just return -1. Else return 0.
335 * Incorporate the any new mail that we found.
345 if ((obuf = Fopen(mailname, "r+")) == NULL) {
346 warn("%s", mailname);
351 while ((c = getc(res)) != EOF)
354 for (mp = &message[0]; mp < &message[msgCount]; mp++)
355 if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
357 if (sendmessage(mp, obuf, NULL, NULL) < 0) {
358 warnx("%s", mailname);
365 while ((c = getc(res)) != EOF)
371 warn("%s", mailname);
380 printf("Held 1 message in %s\n", mailname);
382 printf("Held %d messages in %s\n", p, mailname);
387 * Terminate an editing session by attempting to write out the user's
388 * file from the temporary. Save any new stuff appended to the file.
395 FILE *obuf, *ibuf, *readstat;
397 char tempname[PATHSIZE];
403 if ((readstat = Fopen(Tflag, "w")) == NULL)
406 for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
407 if (mp->m_flag & MNEW) {
409 mp->m_flag |= MSTATUS;
411 if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
413 if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
416 if ((id = hfield("article-id", mp)) != NULL)
417 fprintf(readstat, "%s\n", id);
422 if (!gotcha || Tflag != NULL)
425 if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
428 snprintf(tempname, sizeof(tempname), "%s/mbox.XXXXXXXXXX",
430 if ((fd = mkstemp(tempname)) == -1 ||
431 (obuf = Fdopen(fd, "w")) == NULL) {
432 warn("%s", tempname);
436 if ((ibuf = Fopen(mailname, "r")) == NULL) {
437 warn("%s", mailname);
443 fseeko(ibuf, mailsize, SEEK_SET);
444 while ((c = getc(ibuf)) != EOF)
448 if ((ibuf = Fopen(tempname, "r")) == NULL) {
449 warn("%s", tempname);
456 printf("\"%s\" ", mailname);
458 if ((obuf = Fopen(mailname, "r+")) == NULL) {
459 warn("%s", mailname);
465 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
466 if ((mp->m_flag & MDELETED) != 0)
469 if (sendmessage(mp, obuf, NULL, NULL) < 0) {
470 warnx("%s", mailname);
475 gotcha = (c == 0 && ibuf == NULL);
477 while ((c = getc(ibuf)) != EOF)
483 warn("%s", mailname);
492 printf("complete\n");