* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)quit.c 5.16 (Berkeley) 2/9/91";
* Rcv -- receive mail rationally.
* Termination processing.
* If we are sourcing, then return 1 so execute() can handle it.
* Otherwise, return -1 to abort command loop.
* Save all of the undetermined messages at the top of "mbox"
* Save all untouched messages back in the system mailbox.
* Remove the system mailbox, if none saved there.
int mcount
, p
, modify
, autohold
, anystat
, holdbit
, nohold
;
FILE *ibuf
, *obuf
, *fbuf
, *rbuf
, *readstat
, *abuf
;
register struct message
*mp
;
extern char tempQuit
[], tempResid
[];
* If we are read only, we can't do anything,
* so just return quickly.
* If editing (not reading system mail box), then do the work
* See if there any messages to save in mbox. If no, we
* can save copying mbox to /tmp and back.
* Check also to see if any files need to be preserved.
* Delete all untouched messages to keep them out of mbox.
* If all the messages are to be preserved, just exit with
fbuf
= Fopen(mailname
, "r");
flock(fileno(fbuf
), LOCK_EX
);
if (fstat(fileno(fbuf
), &minfo
) >= 0 && minfo
.st_size
> mailsize
) {
printf("New mail has arrived.\n");
rbuf
= Fopen(tempResid
, "w");
if (rbuf
== NULL
|| fbuf
== NULL
)
fseek(fbuf
, mailsize
, 0);
while ((c
= getc(fbuf
)) != EOF
)
p
= minfo
.st_size
- mailsize
;
if ((rbuf
= Fopen(tempResid
, "r")) == NULL
)
* Adjust the message flags in each message.
autohold
= value("hold") != NOSTR
;
holdbit
= autohold
? MPRESERVE
: MBOX
;
nohold
= MBOX
|MSAVED
|MDELETED
|MPRESERVE
;
if (value("keepsave") != NOSTR
)
for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
if (mp
->m_flag
& MSTATUS
)
if ((mp
->m_flag
& MTOUCH
) == 0)
if ((mp
->m_flag
& nohold
) == 0)
if ((readstat
= Fopen(Tflag
, "w")) == NULL
)
for (c
= 0, p
= 0, mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
if (mp
->m_flag
& MPRESERVE
)
if (Tflag
!= NOSTR
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
if ((id
= hfield("article-id", mp
)) != NOSTR
)
fprintf(readstat
, "%s\n", id
);
if (p
== msgCount
&& !modify
&& !anystat
) {
printf("Held %d message%s in %s\n",
p
, p
== 1 ? "" : "s", mailname
);
* Create another temporary file and copy user's mbox file
* darin. If there is no mbox, copy nothing.
* If he has specified "append" don't copy his mailbox,
* just copy saveable entries at the end.
if (value("append") == NOSTR
) {
if ((obuf
= Fopen(tempQuit
, "w")) == NULL
) {
if ((ibuf
= Fopen(tempQuit
, "r")) == NULL
) {
if ((abuf
= Fopen(mbox
, "r")) != NULL
) {
while ((c
= getc(abuf
)) != EOF
)
close(creat(mbox
, 0600));
if ((obuf
= Fopen(mbox
, "r+")) == NULL
) {
if (value("append") != NOSTR
) {
if ((obuf
= Fopen(mbox
, "a")) == NULL
) {
fchmod(fileno(obuf
), 0600);
for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
if (send(mp
, obuf
, saveignore
, NOSTR
) < 0) {
* Copy the user's old mbox contents back
* to the end of the stuff we just saved.
* If we are appending, this is unnecessary.
if (value("append") == NOSTR
) {
printf("Saved 1 message in mbox\n");
printf("Saved %d messages in mbox\n", mcount
);
* Now we are ready to copy back preserved files to
* the system mailbox, if any were requested.
* Finally, remove his /usr/mail file.
* If new mail has arrived, copy it back.
abuf
= Fopen(mailname
, "r+");
while ((c
= getc(rbuf
)) != EOF
)
printf("Thou hast new mail.\n");
* Preserve all the appropriate messages back in the system
* mailbox, and print a nice message indicated how many were
* saved. On any error, just return -1. Else return 0.
* Incorporate the any new mail that we found.
register struct message
*mp
;
if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
while ((c
= getc(res
)) != EOF
)
for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
if ((mp
->m_flag
&MPRESERVE
)||(mp
->m_flag
&MTOUCH
)==0) {
if (send(mp
, obuf
, (struct ignoretab
*)0, NOSTR
) < 0) {
while ((c
= getc(res
)) != EOF
)
printf("Held 1 message in %s\n", mailname
);
printf("Held %d messages in %s\n", p
, mailname
);
* Terminate an editing session by attempting to write out the user's
* file from the temporary. Save any new stuff appended to the file.
register struct message
*mp
;
FILE *obuf
, *ibuf
, *readstat
;
if ((readstat
= Fopen(Tflag
, "w")) == NULL
)
for (mp
= &message
[0], gotcha
= 0; mp
< &message
[msgCount
]; mp
++) {
if (mp
->m_flag
& (MODIFY
|MDELETED
|MSTATUS
))
if (Tflag
!= NOSTR
&& (mp
->m_flag
& (MREAD
|MDELETED
)) != 0) {
if ((id
= hfield("article-id", mp
)) != NOSTR
)
fprintf(readstat
, "%s\n", id
);
if (!gotcha
|| Tflag
!= NOSTR
)
if (stat(mailname
, &statb
) >= 0 && statb
.st_size
> mailsize
) {
strcpy(tempname
, _PATH_TMP
);
strcat(tempname
, "mboxXXXXXX");
if ((obuf
= Fopen(tempname
, "w")) == NULL
) {
if ((ibuf
= Fopen(mailname
, "r")) == NULL
) {
fseek(ibuf
, mailsize
, 0);
while ((c
= getc(ibuf
)) != EOF
)
if ((ibuf
= Fopen(tempname
, "r")) == NULL
) {
printf("\"%s\" ", mailname
);
if ((obuf
= Fopen(mailname
, "r+")) == NULL
) {
for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++) {
if ((mp
->m_flag
& MDELETED
) != 0)
if (send(mp
, obuf
, (struct ignoretab
*) NULL
, NOSTR
) < 0) {
gotcha
= (c
== 0 && ibuf
== NULL
);
while ((c
= getc(ibuf
)) != EOF
)