version 1.3, 1997/03/29 03:01:47 |
version 1.4, 1997/07/13 21:21:16 |
|
|
/* $OpenBSD$ */ |
/* $OpenBSD$ */ |
/* $NetBSD: quit.c,v 1.5 1996/06/08 19:48:37 christos Exp $ */ |
/* $NetBSD: quit.c,v 1.6 1996/12/28 07:11:07 tls Exp $ */ |
|
|
/* |
/* |
* Copyright (c) 1980, 1993 |
* Copyright (c) 1980, 1993 |
|
|
|
|
#ifndef lint |
#ifndef lint |
#if 0 |
#if 0 |
static char sccsid[] = "@(#)quit.c 8.1 (Berkeley) 6/6/93"; |
static char sccsid[] = "@(#)quit.c 8.2 (Berkeley) 4/28/95"; |
#else |
#else |
static char rcsid[] = "$OpenBSD$"; |
static char rcsid[] = "$OpenBSD$"; |
#endif |
#endif |
|
|
* Otherwise, return -1 to abort command loop. |
* Otherwise, return -1 to abort command loop. |
*/ |
*/ |
if (sourcing) |
if (sourcing) |
return 1; |
return(1); |
return -1; |
return(-1); |
} |
} |
|
|
/* |
/* |
|
|
if (fbuf == NULL) |
if (fbuf == NULL) |
goto newmail; |
goto newmail; |
if (flock(fileno(fbuf), LOCK_EX) == -1) { |
if (flock(fileno(fbuf), LOCK_EX) == -1) { |
perror("Unable to lock mailbox"); |
warn("Unable to lock mailbox"); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
return; |
return; |
} |
} |
if (!spool_lock()) { |
if (!spool_lock()) { |
Fclose(fbuf); |
(void)Fclose(fbuf); |
return; /* mail.local printed error for us */ |
return; /* mail.local printed error for us */ |
} |
} |
rbuf = NULL; |
rbuf = NULL; |
if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) { |
if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) { |
printf("New mail has arrived.\n"); |
puts("New mail has arrived."); |
rbuf = Fopen(tempResid, "w"); |
rbuf = Fopen(tempResid, "w"); |
if (rbuf == NULL || fbuf == NULL) |
if (rbuf == NULL || fbuf == NULL) |
goto newmail; |
goto newmail; |
|
|
(void) putc(c, rbuf); |
(void) putc(c, rbuf); |
} |
} |
#endif |
#endif |
Fclose(rbuf); |
(void)Fclose(rbuf); |
if ((rbuf = Fopen(tempResid, "r")) == NULL) |
if ((rbuf = Fopen(tempResid, "r")) == NULL) |
goto newmail; |
goto newmail; |
rm(tempResid); |
rm(tempResid); |
|
|
} |
} |
} |
} |
if (Tflag != NOSTR) |
if (Tflag != NOSTR) |
Fclose(readstat); |
(void)Fclose(readstat); |
if (p == msgCount && !modify && !anystat) { |
if (p == msgCount && !modify && !anystat) { |
printf("Held %d message%s in %s\n", |
printf("Held %d message%s in %s\n", |
p, p == 1 ? "" : "s", mailname); |
p, p == 1 ? "" : "s", mailname); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
if (c == 0) { |
if (c == 0) { |
if (p != 0) { |
if (p != 0) { |
writeback(rbuf); |
writeback(rbuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
|
|
mcount = c; |
mcount = c; |
if (value("append") == NOSTR) { |
if (value("append") == NOSTR) { |
if ((obuf = Fopen(tempQuit, "w")) == NULL) { |
if ((obuf = Fopen(tempQuit, "w")) == NULL) { |
perror(tempQuit); |
warn(tempQuit); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
if ((ibuf = Fopen(tempQuit, "r")) == NULL) { |
if ((ibuf = Fopen(tempQuit, "r")) == NULL) { |
perror(tempQuit); |
warn(tempQuit); |
rm(tempQuit); |
rm(tempQuit); |
Fclose(obuf); |
(void)Fclose(obuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
|
|
if ((abuf = Fopen(mbox, "r")) != NULL) { |
if ((abuf = Fopen(mbox, "r")) != NULL) { |
while ((c = getc(abuf)) != EOF) |
while ((c = getc(abuf)) != EOF) |
(void) putc(c, obuf); |
(void) putc(c, obuf); |
Fclose(abuf); |
(void)Fclose(abuf); |
} |
} |
if (ferror(obuf)) { |
if (ferror(obuf)) { |
perror(tempQuit); |
warn(tempQuit); |
Fclose(ibuf); |
(void)Fclose(ibuf); |
Fclose(obuf); |
(void)Fclose(obuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
Fclose(obuf); |
(void)Fclose(obuf); |
close(creat(mbox, 0600)); |
(void)close(creat(mbox, 0600)); |
if ((obuf = Fopen(mbox, "r+")) == NULL) { |
if ((obuf = Fopen(mbox, "r+")) == NULL) { |
perror(mbox); |
warn(mbox); |
Fclose(ibuf); |
(void)Fclose(ibuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
} |
} |
else { |
else { |
if ((obuf = Fopen(mbox, "a")) == NULL) { |
if ((obuf = Fopen(mbox, "a")) == NULL) { |
perror(mbox); |
warn(mbox); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
|
|
for (mp = &message[0]; mp < &message[msgCount]; mp++) |
for (mp = &message[0]; mp < &message[msgCount]; mp++) |
if (mp->m_flag & MBOX) |
if (mp->m_flag & MBOX) |
if (send(mp, obuf, saveignore, NOSTR) < 0) { |
if (send(mp, obuf, saveignore, NOSTR) < 0) { |
perror(mbox); |
warn(mbox); |
Fclose(ibuf); |
(void)Fclose(ibuf); |
Fclose(obuf); |
(void)Fclose(obuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
|
|
break; |
break; |
c = getc(ibuf); |
c = getc(ibuf); |
} |
} |
Fclose(ibuf); |
(void)Fclose(ibuf); |
fflush(obuf); |
fflush(obuf); |
} |
} |
trunc(obuf); |
trunc(obuf); |
if (ferror(obuf)) { |
if (ferror(obuf)) { |
perror(mbox); |
warn(mbox); |
Fclose(obuf); |
(void)Fclose(obuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
Fclose(obuf); |
(void)Fclose(obuf); |
if (mcount == 1) |
if (mcount == 1) |
printf("Saved 1 message in mbox\n"); |
puts("Saved 1 message in mbox"); |
else |
else |
printf("Saved %d messages in mbox\n", mcount); |
printf("Saved %d messages in mbox\n", mcount); |
|
|
|
|
|
|
if (p != 0) { |
if (p != 0) { |
writeback(rbuf); |
writeback(rbuf); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
|
|
goto newmail; |
goto newmail; |
while ((c = getc(rbuf)) != EOF) |
while ((c = getc(rbuf)) != EOF) |
(void) putc(c, abuf); |
(void) putc(c, abuf); |
Fclose(rbuf); |
(void)Fclose(rbuf); |
trunc(abuf); |
trunc(abuf); |
Fclose(abuf); |
(void)Fclose(abuf); |
alter(mailname); |
alter(mailname); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
} |
} |
demail(); |
demail(); |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
return; |
return; |
|
|
newmail: |
newmail: |
printf("Thou hast new mail.\n"); |
puts("Thou hast new mail."); |
if (fbuf != NULL) { |
if (fbuf != NULL) { |
Fclose(fbuf); |
(void)Fclose(fbuf); |
spool_unlock(); |
spool_unlock(); |
} |
} |
} |
} |
|
|
|
|
p = 0; |
p = 0; |
if ((obuf = Fopen(mailname, "r+")) == NULL) { |
if ((obuf = Fopen(mailname, "r+")) == NULL) { |
perror(mailname); |
warn(mailname); |
return(-1); |
return(-1); |
} |
} |
#ifndef APPEND |
#ifndef APPEND |
|
|
if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) { |
if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) { |
p++; |
p++; |
if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) { |
if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) { |
perror(mailname); |
warn(mailname); |
Fclose(obuf); |
(void)Fclose(obuf); |
return(-1); |
return(-1); |
} |
} |
} |
} |
|
|
fflush(obuf); |
fflush(obuf); |
trunc(obuf); |
trunc(obuf); |
if (ferror(obuf)) { |
if (ferror(obuf)) { |
perror(mailname); |
warn(mailname); |
Fclose(obuf); |
(void)Fclose(obuf); |
return(-1); |
return(-1); |
} |
} |
if (res != NULL) |
if (res != NULL) |
Fclose(res); |
(void)Fclose(res); |
Fclose(obuf); |
(void)Fclose(obuf); |
alter(mailname); |
alter(mailname); |
if (p == 1) |
if (p == 1) |
printf("Held 1 message in %s\n", mailname); |
printf("Held 1 message in %s\n", mailname); |
|
|
} |
} |
} |
} |
if (Tflag != NOSTR) |
if (Tflag != NOSTR) |
Fclose(readstat); |
(void)Fclose(readstat); |
if (!gotcha || Tflag != NOSTR) |
if (!gotcha || Tflag != NOSTR) |
goto done; |
goto done; |
ibuf = NULL; |
ibuf = NULL; |
|
|
"mboxXXXXXXXXXX"); |
"mboxXXXXXXXXXX"); |
if ((fd = mkstemp(tempname)) == -1 || |
if ((fd = mkstemp(tempname)) == -1 || |
(obuf = Fdopen(fd, "w")) == NULL) { |
(obuf = Fdopen(fd, "w")) == NULL) { |
perror(tempname); |
warn(tempname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
} |
} |
if ((ibuf = Fopen(mailname, "r")) == NULL) { |
if ((ibuf = Fopen(mailname, "r")) == NULL) { |
perror(mailname); |
warn(mailname); |
Fclose(obuf); |
(void)Fclose(obuf); |
rm(tempname); |
rm(tempname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
|
|
fseek(ibuf, (long)mailsize, 0); |
fseek(ibuf, (long)mailsize, 0); |
while ((c = getc(ibuf)) != EOF) |
while ((c = getc(ibuf)) != EOF) |
(void) putc(c, obuf); |
(void) putc(c, obuf); |
Fclose(ibuf); |
(void)Fclose(ibuf); |
Fclose(obuf); |
(void)Fclose(obuf); |
if ((ibuf = Fopen(tempname, "r")) == NULL) { |
if ((ibuf = Fopen(tempname, "r")) == NULL) { |
perror(tempname); |
warn(tempname); |
rm(tempname); |
rm(tempname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
|
|
printf("\"%s\" ", mailname); |
printf("\"%s\" ", mailname); |
fflush(stdout); |
fflush(stdout); |
if ((obuf = Fopen(mailname, "r+")) == NULL) { |
if ((obuf = Fopen(mailname, "r+")) == NULL) { |
perror(mailname); |
warn(mailname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
} |
} |
|
|
continue; |
continue; |
c++; |
c++; |
if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) { |
if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) { |
perror(mailname); |
warn(mailname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
} |
} |
|
|
if (ibuf != NULL) { |
if (ibuf != NULL) { |
while ((c = getc(ibuf)) != EOF) |
while ((c = getc(ibuf)) != EOF) |
(void) putc(c, obuf); |
(void) putc(c, obuf); |
Fclose(ibuf); |
(void)Fclose(ibuf); |
} |
} |
fflush(obuf); |
fflush(obuf); |
if (ferror(obuf)) { |
if (ferror(obuf)) { |
perror(mailname); |
warn(mailname); |
relsesigs(); |
relsesigs(); |
reset(0); |
reset(0); |
} |
} |
Fclose(obuf); |
(void)Fclose(obuf); |
if (gotcha) { |
if (gotcha) { |
rm(mailname); |
rm(mailname); |
printf("removed\n"); |
puts("removed"); |
} else |
} else |
printf("complete\n"); |
puts("complete"); |
fflush(stdout); |
fflush(stdout); |
|
|
done: |
done: |