2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)quit.c 8.2 (Berkeley) 4/28/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
45 * Rcv -- receive mail rationally.
47 * Termination processing.
57 * If we are sourcing, then return 1 so execute() can handle it.
58 * Otherwise, return -1 to abort command loop.
66 * Save all of the undetermined messages at the top of "mbox"
67 * Save all untouched messages back in the system mailbox.
68 * Remove the system mailbox, if none saved there.
73 int mcount, p, modify, autohold, anystat, holdbit, nohold;
74 FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
78 char *mbox, tempname[PATHSIZE];
81 * If we are read only, we can't do anything,
82 * so just return quickly.
87 * If editing (not reading system mail box), then do the work
96 * See if there any messages to save in mbox. If no, we
97 * can save copying mbox to /tmp and back.
99 * Check also to see if any files need to be preserved.
100 * Delete all untouched messages to keep them out of mbox.
101 * If all the messages are to be preserved, just exit with
105 fbuf = Fopen(mailname, "r");
108 (void)flock(fileno(fbuf), LOCK_EX);
110 if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
111 printf("New mail has arrived.\n");
112 (void)snprintf(tempname, sizeof(tempname),
113 "%s/mail.RqXXXXXXXXXX", tmpdir);
114 if ((fd = mkstemp(tempname)) == -1 ||
115 (rbuf = Fdopen(fd, "w")) == NULL)
118 (void)fseeko(fbuf, mailsize, SEEK_SET);
119 while ((c = getc(fbuf)) != EOF)
122 p = minfo.st_size - mailsize;
131 if ((rbuf = Fopen(tempname, "r")) == NULL)
137 * Adjust the message flags in each message.
141 autohold = value("hold") != NULL;
142 holdbit = autohold ? MPRESERVE : MBOX;
143 nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
144 if (value("keepsave") != NULL)
146 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
147 if (mp->m_flag & MNEW) {
149 mp->m_flag |= MSTATUS;
151 if (mp->m_flag & MSTATUS)
153 if ((mp->m_flag & MTOUCH) == 0)
154 mp->m_flag |= MPRESERVE;
155 if ((mp->m_flag & nohold) == 0)
156 mp->m_flag |= holdbit;
160 if ((readstat = Fopen(Tflag, "w")) == NULL)
163 for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
164 if (mp->m_flag & MBOX)
166 if (mp->m_flag & MPRESERVE)
168 if (mp->m_flag & MODIFY)
170 if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
173 if ((id = hfield("article-id", mp)) != NULL)
174 fprintf(readstat, "%s\n", id);
178 (void)Fclose(readstat);
179 if (p == msgCount && !modify && !anystat) {
180 printf("Held %d message%s in %s\n",
181 p, p == 1 ? "" : "s", mailname);
195 * Create another temporary file and copy user's mbox file
196 * darin. If there is no mbox, copy nothing.
197 * If he has specified "append" don't copy his mailbox,
198 * just copy saveable entries at the end.
203 if (value("append") == NULL) {
204 (void)snprintf(tempname, sizeof(tempname),
205 "%s/mail.RmXXXXXXXXXX", tmpdir);
206 if ((fd = mkstemp(tempname)) == -1 ||
207 (obuf = Fdopen(fd, "w")) == NULL) {
208 warn("%s", tempname);
212 if ((ibuf = Fopen(tempname, "r")) == NULL) {
213 warn("%s", tempname);
220 if ((abuf = Fopen(mbox, "r")) != NULL) {
221 while ((c = getc(abuf)) != EOF)
226 warnx("%s", tempname);
233 if ((fd = open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)) >= 0)
235 if ((obuf = Fopen(mbox, "r+")) == NULL) {
242 if (value("append") != NULL) {
243 if ((obuf = Fopen(mbox, "a")) == NULL) {
248 (void)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);
421 (void)Fclose(readstat);
422 if (!gotcha || Tflag != NULL)
425 if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
428 (void)snprintf(tempname, sizeof(tempname),
429 "%s/mbox.XXXXXXXXXX", tmpdir);
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 (void)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);
457 (void)fflush(stdout);
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");
493 (void)fflush(stdout);