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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)quit.c 8.2 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.bin/mail/quit.c,v 1.2.6.3 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/quit.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
38 static void edstop(void);
39 static int writeback(FILE *res);
42 * Rcv -- receive mail rationally.
44 * Termination processing.
54 * If we are sourcing, then return 1 so execute() can handle it.
55 * Otherwise, return -1 to abort command loop.
63 * Save all of the undetermined messages at the top of "mbox"
64 * Save all untouched messages back in the system mailbox.
65 * Remove the system mailbox, if none saved there.
70 int mcount, p, modify, autohold, anystat, holdbit, nohold;
71 FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
75 char *mbox, tempname[PATHSIZE];
78 * If we are read only, we can't do anything,
79 * so just return quickly.
84 * If editing (not reading system mail box), then do the work
93 * See if there any messages to save in mbox. If no, we
94 * can save copying mbox to /tmp and back.
96 * Check also to see if any files need to be preserved.
97 * Delete all untouched messages to keep them out of mbox.
98 * If all the messages are to be preserved, just exit with
102 fbuf = Fopen(mailname, "r");
105 flock(fileno(fbuf), LOCK_EX);
107 if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
108 printf("New mail has arrived.\n");
109 snprintf(tempname, sizeof(tempname), "%s/mail.RqXXXXXXXXXX",
111 if ((fd = mkstemp(tempname)) == -1 ||
112 (rbuf = Fdopen(fd, "w")) == NULL)
115 fseeko(fbuf, mailsize, SEEK_SET);
116 while ((c = getc(fbuf)) != EOF)
119 p = minfo.st_size - mailsize;
128 if ((rbuf = Fopen(tempname, "r")) == NULL)
134 * Adjust the message flags in each message.
138 autohold = value("hold") != NULL;
139 holdbit = autohold ? MPRESERVE : MBOX;
140 nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
141 if (value("keepsave") != NULL)
143 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
144 if (mp->m_flag & MNEW) {
146 mp->m_flag |= MSTATUS;
148 if (mp->m_flag & MSTATUS)
150 if ((mp->m_flag & MTOUCH) == 0)
151 mp->m_flag |= MPRESERVE;
152 if ((mp->m_flag & nohold) == 0)
153 mp->m_flag |= holdbit;
157 if ((readstat = Fopen(Tflag, "w")) == NULL)
160 for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
161 if (mp->m_flag & MBOX)
163 if (mp->m_flag & MPRESERVE)
165 if (mp->m_flag & MODIFY)
167 if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
170 if ((id = hfield("article-id", mp)) != NULL)
171 fprintf(readstat, "%s\n", id);
176 if (p == msgCount && !modify && !anystat) {
177 printf("Held %d message%s in %s\n",
178 p, p == 1 ? "" : "s", mailname);
192 * Create another temporary file and copy user's mbox file
193 * darin. If there is no mbox, copy nothing.
194 * If he has specified "append" don't copy his mailbox,
195 * just copy saveable entries at the end.
200 if (value("append") == NULL) {
201 snprintf(tempname, sizeof(tempname), "%s/mail.RmXXXXXXXXXX",
203 if ((fd = mkstemp(tempname)) == -1 ||
204 (obuf = Fdopen(fd, "w")) == NULL) {
205 warn("%s", tempname);
209 if ((ibuf = Fopen(tempname, "r")) == NULL) {
210 warn("%s", tempname);
217 if ((abuf = Fopen(mbox, "r")) != NULL) {
218 while ((c = getc(abuf)) != EOF)
223 warnx("%s", tempname);
230 close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600));
231 if ((obuf = Fopen(mbox, "r+")) == NULL) {
238 if (value("append") != NULL) {
239 if ((obuf = Fopen(mbox, "a")) == NULL) {
244 fchmod(fileno(obuf), 0600);
246 for (mp = &message[0]; mp < &message[msgCount]; mp++)
247 if (mp->m_flag & MBOX)
248 if (sendmessage(mp, obuf, saveignore, NULL) < 0) {
257 * Copy the user's old mbox contents back
258 * to the end of the stuff we just saved.
259 * If we are appending, this is unnecessary.
262 if (value("append") == NULL) {
283 printf("Saved 1 message in mbox\n");
285 printf("Saved %d messages in mbox\n", mcount);
288 * Now we are ready to copy back preserved files to
289 * the system mailbox, if any were requested.
299 * Finally, remove his /var/mail file.
300 * If new mail has arrived, copy it back.
305 abuf = Fopen(mailname, "r+");
308 while ((c = getc(rbuf)) != EOF)
322 printf("Thou hast new mail.\n");
328 * Preserve all the appropriate messages back in the system
329 * mailbox, and print a nice message indicated how many were
330 * saved. On any error, just return -1. Else return 0.
331 * Incorporate the any new mail that we found.
341 if ((obuf = Fopen(mailname, "r+")) == NULL) {
342 warn("%s", mailname);
347 while ((c = getc(res)) != EOF)
350 for (mp = &message[0]; mp < &message[msgCount]; mp++)
351 if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
353 if (sendmessage(mp, obuf, NULL, NULL) < 0) {
354 warnx("%s", mailname);
361 while ((c = getc(res)) != EOF)
367 warn("%s", mailname);
376 printf("Held 1 message in %s\n", mailname);
378 printf("Held %d messages in %s\n", p, mailname);
383 * Terminate an editing session by attempting to write out the user's
384 * file from the temporary. Save any new stuff appended to the file.
391 FILE *obuf, *ibuf, *readstat;
393 char tempname[PATHSIZE];
399 if ((readstat = Fopen(Tflag, "w")) == NULL)
402 for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
403 if (mp->m_flag & MNEW) {
405 mp->m_flag |= MSTATUS;
407 if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
409 if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
412 if ((id = hfield("article-id", mp)) != NULL)
413 fprintf(readstat, "%s\n", id);
418 if (!gotcha || Tflag != NULL)
421 if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
424 snprintf(tempname, sizeof(tempname), "%s/mbox.XXXXXXXXXX",
426 if ((fd = mkstemp(tempname)) == -1 ||
427 (obuf = Fdopen(fd, "w")) == NULL) {
428 warn("%s", tempname);
432 if ((ibuf = Fopen(mailname, "r")) == NULL) {
433 warn("%s", mailname);
439 fseeko(ibuf, mailsize, SEEK_SET);
440 while ((c = getc(ibuf)) != EOF)
444 if ((ibuf = Fopen(tempname, "r")) == NULL) {
445 warn("%s", tempname);
452 printf("\"%s\" ", mailname);
454 if ((obuf = Fopen(mailname, "r+")) == NULL) {
455 warn("%s", mailname);
461 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
462 if ((mp->m_flag & MDELETED) != 0)
465 if (sendmessage(mp, obuf, NULL, NULL) < 0) {
466 warnx("%s", mailname);
471 gotcha = (c == 0 && ibuf == NULL);
473 while ((c = getc(ibuf)) != EOF)
479 warn("%s", mailname);
488 printf("complete\n");