quit.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd static char sccsid[] = "@(#)quit.c 5.16 (Berkeley) 2/9/91";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #include "rcv.h"
39 1.1 cgd #include <sys/stat.h>
40 1.1 cgd #include <sys/file.h>
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * Rcv -- receive mail rationally.
44 1.1 cgd *
45 1.1 cgd * Termination processing.
46 1.1 cgd */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * The "quit" command.
50 1.1 cgd */
51 1.1 cgd quitcmd()
52 1.1 cgd {
53 1.1 cgd /*
54 1.1 cgd * If we are sourcing, then return 1 so execute() can handle it.
55 1.1 cgd * Otherwise, return -1 to abort command loop.
56 1.1 cgd */
57 1.1 cgd if (sourcing)
58 1.1 cgd return 1;
59 1.1 cgd return -1;
60 1.1 cgd }
61 1.1 cgd
62 1.1 cgd /*
63 1.1 cgd * Save all of the undetermined messages at the top of "mbox"
64 1.1 cgd * Save all untouched messages back in the system mailbox.
65 1.1 cgd * Remove the system mailbox, if none saved there.
66 1.1 cgd */
67 1.1 cgd
68 1.1 cgd quit()
69 1.1 cgd {
70 1.1 cgd int mcount, p, modify, autohold, anystat, holdbit, nohold;
71 1.1 cgd FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
72 1.1 cgd register struct message *mp;
73 1.1 cgd register int c;
74 1.1 cgd extern char tempQuit[], tempResid[];
75 1.1 cgd struct stat minfo;
76 1.1 cgd char *mbox;
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * If we are read only, we can't do anything,
80 1.1 cgd * so just return quickly.
81 1.1 cgd */
82 1.1 cgd if (readonly)
83 1.1 cgd return;
84 1.1 cgd /*
85 1.1 cgd * If editing (not reading system mail box), then do the work
86 1.1 cgd * in edstop()
87 1.1 cgd */
88 1.1 cgd if (edit) {
89 1.1 cgd edstop();
90 1.1 cgd return;
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd /*
94 1.1 cgd * See if there any messages to save in mbox. If no, we
95 1.1 cgd * can save copying mbox to /tmp and back.
96 1.1 cgd *
97 1.1 cgd * Check also to see if any files need to be preserved.
98 1.1 cgd * Delete all untouched messages to keep them out of mbox.
99 1.1 cgd * If all the messages are to be preserved, just exit with
100 1.1 cgd * a message.
101 1.1 cgd */
102 1.1 cgd
103 1.1 cgd fbuf = Fopen(mailname, "r");
104 1.1 cgd if (fbuf == NULL)
105 1.1 cgd goto newmail;
106 1.1 cgd flock(fileno(fbuf), LOCK_EX);
107 1.1 cgd rbuf = NULL;
108 1.1 cgd if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
109 1.1 cgd printf("New mail has arrived.\n");
110 1.1 cgd rbuf = Fopen(tempResid, "w");
111 1.1 cgd if (rbuf == NULL || fbuf == NULL)
112 1.1 cgd goto newmail;
113 1.1 cgd #ifdef APPEND
114 1.1 cgd fseek(fbuf, mailsize, 0);
115 1.1 cgd while ((c = getc(fbuf)) != EOF)
116 1.1 cgd (void) putc(c, rbuf);
117 1.1 cgd #else
118 1.1 cgd p = minfo.st_size - mailsize;
119 1.1 cgd while (p-- > 0) {
120 1.1 cgd c = getc(fbuf);
121 1.1 cgd if (c == EOF)
122 1.1 cgd goto newmail;
123 1.1 cgd (void) putc(c, rbuf);
124 1.1 cgd }
125 1.1 cgd #endif
126 1.1 cgd Fclose(rbuf);
127 1.1 cgd if ((rbuf = Fopen(tempResid, "r")) == NULL)
128 1.1 cgd goto newmail;
129 1.1 cgd rm(tempResid);
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Adjust the message flags in each message.
134 1.1 cgd */
135 1.1 cgd
136 1.1 cgd anystat = 0;
137 1.1 cgd autohold = value("hold") != NOSTR;
138 1.1 cgd holdbit = autohold ? MPRESERVE : MBOX;
139 1.1 cgd nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
140 1.1 cgd if (value("keepsave") != NOSTR)
141 1.1 cgd nohold &= ~MSAVED;
142 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++) {
143 1.1 cgd if (mp->m_flag & MNEW) {
144 1.1 cgd mp->m_flag &= ~MNEW;
145 1.1 cgd mp->m_flag |= MSTATUS;
146 1.1 cgd }
147 1.1 cgd if (mp->m_flag & MSTATUS)
148 1.1 cgd anystat++;
149 1.1 cgd if ((mp->m_flag & MTOUCH) == 0)
150 1.1 cgd mp->m_flag |= MPRESERVE;
151 1.1 cgd if ((mp->m_flag & nohold) == 0)
152 1.1 cgd mp->m_flag |= holdbit;
153 1.1 cgd }
154 1.1 cgd modify = 0;
155 1.1 cgd if (Tflag != NOSTR) {
156 1.1 cgd if ((readstat = Fopen(Tflag, "w")) == NULL)
157 1.1 cgd Tflag = NOSTR;
158 1.1 cgd }
159 1.1 cgd for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
160 1.1 cgd if (mp->m_flag & MBOX)
161 1.1 cgd c++;
162 1.1 cgd if (mp->m_flag & MPRESERVE)
163 1.1 cgd p++;
164 1.1 cgd if (mp->m_flag & MODIFY)
165 1.1 cgd modify++;
166 1.1 cgd if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
167 1.1 cgd char *id;
168 1.1 cgd
169 1.1 cgd if ((id = hfield("article-id", mp)) != NOSTR)
170 1.1 cgd fprintf(readstat, "%s\n", id);
171 1.1 cgd }
172 1.1 cgd }
173 1.1 cgd if (Tflag != NOSTR)
174 1.1 cgd Fclose(readstat);
175 1.1 cgd if (p == msgCount && !modify && !anystat) {
176 1.1 cgd printf("Held %d message%s in %s\n",
177 1.1 cgd p, p == 1 ? "" : "s", mailname);
178 1.1 cgd Fclose(fbuf);
179 1.1 cgd return;
180 1.1 cgd }
181 1.1 cgd if (c == 0) {
182 1.1 cgd if (p != 0) {
183 1.1 cgd writeback(rbuf);
184 1.1 cgd Fclose(fbuf);
185 1.1 cgd return;
186 1.1 cgd }
187 1.1 cgd goto cream;
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Create another temporary file and copy user's mbox file
192 1.1 cgd * darin. If there is no mbox, copy nothing.
193 1.1 cgd * If he has specified "append" don't copy his mailbox,
194 1.1 cgd * just copy saveable entries at the end.
195 1.1 cgd */
196 1.1 cgd
197 1.1 cgd mbox = expand("&");
198 1.1 cgd mcount = c;
199 1.1 cgd if (value("append") == NOSTR) {
200 1.1 cgd if ((obuf = Fopen(tempQuit, "w")) == NULL) {
201 1.1 cgd perror(tempQuit);
202 1.1 cgd Fclose(fbuf);
203 1.1 cgd return;
204 1.1 cgd }
205 1.1 cgd if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
206 1.1 cgd perror(tempQuit);
207 1.1 cgd rm(tempQuit);
208 1.1 cgd Fclose(obuf);
209 1.1 cgd Fclose(fbuf);
210 1.1 cgd return;
211 1.1 cgd }
212 1.1 cgd rm(tempQuit);
213 1.1 cgd if ((abuf = Fopen(mbox, "r")) != NULL) {
214 1.1 cgd while ((c = getc(abuf)) != EOF)
215 1.1 cgd (void) putc(c, obuf);
216 1.1 cgd Fclose(abuf);
217 1.1 cgd }
218 1.1 cgd if (ferror(obuf)) {
219 1.1 cgd perror(tempQuit);
220 1.1 cgd Fclose(ibuf);
221 1.1 cgd Fclose(obuf);
222 1.1 cgd Fclose(fbuf);
223 1.1 cgd return;
224 1.1 cgd }
225 1.1 cgd Fclose(obuf);
226 1.1 cgd close(creat(mbox, 0600));
227 1.1 cgd if ((obuf = Fopen(mbox, "r+")) == NULL) {
228 1.1 cgd perror(mbox);
229 1.1 cgd Fclose(ibuf);
230 1.1 cgd Fclose(fbuf);
231 1.1 cgd return;
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd if (value("append") != NOSTR) {
235 1.1 cgd if ((obuf = Fopen(mbox, "a")) == NULL) {
236 1.1 cgd perror(mbox);
237 1.1 cgd Fclose(fbuf);
238 1.1 cgd return;
239 1.1 cgd }
240 1.1 cgd fchmod(fileno(obuf), 0600);
241 1.1 cgd }
242 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++)
243 1.1 cgd if (mp->m_flag & MBOX)
244 1.1 cgd if (send(mp, obuf, saveignore, NOSTR) < 0) {
245 1.1 cgd perror(mbox);
246 1.1 cgd Fclose(ibuf);
247 1.1 cgd Fclose(obuf);
248 1.1 cgd Fclose(fbuf);
249 1.1 cgd return;
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd /*
253 1.1 cgd * Copy the user's old mbox contents back
254 1.1 cgd * to the end of the stuff we just saved.
255 1.1 cgd * If we are appending, this is unnecessary.
256 1.1 cgd */
257 1.1 cgd
258 1.1 cgd if (value("append") == NOSTR) {
259 1.1 cgd rewind(ibuf);
260 1.1 cgd c = getc(ibuf);
261 1.1 cgd while (c != EOF) {
262 1.1 cgd (void) putc(c, obuf);
263 1.1 cgd if (ferror(obuf))
264 1.1 cgd break;
265 1.1 cgd c = getc(ibuf);
266 1.1 cgd }
267 1.1 cgd Fclose(ibuf);
268 1.1 cgd fflush(obuf);
269 1.1 cgd }
270 1.1 cgd trunc(obuf);
271 1.1 cgd if (ferror(obuf)) {
272 1.1 cgd perror(mbox);
273 1.1 cgd Fclose(obuf);
274 1.1 cgd Fclose(fbuf);
275 1.1 cgd return;
276 1.1 cgd }
277 1.1 cgd Fclose(obuf);
278 1.1 cgd if (mcount == 1)
279 1.1 cgd printf("Saved 1 message in mbox\n");
280 1.1 cgd else
281 1.1 cgd printf("Saved %d messages in mbox\n", mcount);
282 1.1 cgd
283 1.1 cgd /*
284 1.1 cgd * Now we are ready to copy back preserved files to
285 1.1 cgd * the system mailbox, if any were requested.
286 1.1 cgd */
287 1.1 cgd
288 1.1 cgd if (p != 0) {
289 1.1 cgd writeback(rbuf);
290 1.1 cgd Fclose(fbuf);
291 1.1 cgd return;
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd /*
295 1.1 cgd * Finally, remove his /usr/mail file.
296 1.1 cgd * If new mail has arrived, copy it back.
297 1.1 cgd */
298 1.1 cgd
299 1.1 cgd cream:
300 1.1 cgd if (rbuf != NULL) {
301 1.1 cgd abuf = Fopen(mailname, "r+");
302 1.1 cgd if (abuf == NULL)
303 1.1 cgd goto newmail;
304 1.1 cgd while ((c = getc(rbuf)) != EOF)
305 1.1 cgd (void) putc(c, abuf);
306 1.1 cgd Fclose(rbuf);
307 1.1 cgd trunc(abuf);
308 1.1 cgd Fclose(abuf);
309 1.1 cgd alter(mailname);
310 1.1 cgd Fclose(fbuf);
311 1.1 cgd return;
312 1.1 cgd }
313 1.1 cgd demail();
314 1.1 cgd Fclose(fbuf);
315 1.1 cgd return;
316 1.1 cgd
317 1.1 cgd newmail:
318 1.1 cgd printf("Thou hast new mail.\n");
319 1.1 cgd if (fbuf != NULL)
320 1.1 cgd Fclose(fbuf);
321 1.1 cgd }
322 1.1 cgd
323 1.1 cgd /*
324 1.1 cgd * Preserve all the appropriate messages back in the system
325 1.1 cgd * mailbox, and print a nice message indicated how many were
326 1.1 cgd * saved. On any error, just return -1. Else return 0.
327 1.1 cgd * Incorporate the any new mail that we found.
328 1.1 cgd */
329 1.1 cgd writeback(res)
330 1.1 cgd register FILE *res;
331 1.1 cgd {
332 1.1 cgd register struct message *mp;
333 1.1 cgd register int p, c;
334 1.1 cgd FILE *obuf;
335 1.1 cgd
336 1.1 cgd p = 0;
337 1.1 cgd if ((obuf = Fopen(mailname, "r+")) == NULL) {
338 1.1 cgd perror(mailname);
339 1.1 cgd return(-1);
340 1.1 cgd }
341 1.1 cgd #ifndef APPEND
342 1.1 cgd if (res != NULL)
343 1.1 cgd while ((c = getc(res)) != EOF)
344 1.1 cgd (void) putc(c, obuf);
345 1.1 cgd #endif
346 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++)
347 1.1 cgd if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
348 1.1 cgd p++;
349 1.1 cgd if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
350 1.1 cgd perror(mailname);
351 1.1 cgd Fclose(obuf);
352 1.1 cgd return(-1);
353 1.1 cgd }
354 1.1 cgd }
355 1.1 cgd #ifdef APPEND
356 1.1 cgd if (res != NULL)
357 1.1 cgd while ((c = getc(res)) != EOF)
358 1.1 cgd (void) putc(c, obuf);
359 1.1 cgd #endif
360 1.1 cgd fflush(obuf);
361 1.1 cgd trunc(obuf);
362 1.1 cgd if (ferror(obuf)) {
363 1.1 cgd perror(mailname);
364 1.1 cgd Fclose(obuf);
365 1.1 cgd return(-1);
366 1.1 cgd }
367 1.1 cgd if (res != NULL)
368 1.1 cgd Fclose(res);
369 1.1 cgd Fclose(obuf);
370 1.1 cgd alter(mailname);
371 1.1 cgd if (p == 1)
372 1.1 cgd printf("Held 1 message in %s\n", mailname);
373 1.1 cgd else
374 1.1 cgd printf("Held %d messages in %s\n", p, mailname);
375 1.1 cgd return(0);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.1 cgd * Terminate an editing session by attempting to write out the user's
380 1.1 cgd * file from the temporary. Save any new stuff appended to the file.
381 1.1 cgd */
382 1.1 cgd edstop()
383 1.1 cgd {
384 1.1 cgd register int gotcha, c;
385 1.1 cgd register struct message *mp;
386 1.1 cgd FILE *obuf, *ibuf, *readstat;
387 1.1 cgd struct stat statb;
388 1.1 cgd char tempname[30];
389 1.1 cgd char *mktemp();
390 1.1 cgd
391 1.1 cgd if (readonly)
392 1.1 cgd return;
393 1.1 cgd holdsigs();
394 1.1 cgd if (Tflag != NOSTR) {
395 1.1 cgd if ((readstat = Fopen(Tflag, "w")) == NULL)
396 1.1 cgd Tflag = NOSTR;
397 1.1 cgd }
398 1.1 cgd for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
399 1.1 cgd if (mp->m_flag & MNEW) {
400 1.1 cgd mp->m_flag &= ~MNEW;
401 1.1 cgd mp->m_flag |= MSTATUS;
402 1.1 cgd }
403 1.1 cgd if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
404 1.1 cgd gotcha++;
405 1.1 cgd if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
406 1.1 cgd char *id;
407 1.1 cgd
408 1.1 cgd if ((id = hfield("article-id", mp)) != NOSTR)
409 1.1 cgd fprintf(readstat, "%s\n", id);
410 1.1 cgd }
411 1.1 cgd }
412 1.1 cgd if (Tflag != NOSTR)
413 1.1 cgd Fclose(readstat);
414 1.1 cgd if (!gotcha || Tflag != NOSTR)
415 1.1 cgd goto done;
416 1.1 cgd ibuf = NULL;
417 1.1 cgd if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
418 1.1 cgd strcpy(tempname, _PATH_TMP);
419 1.1 cgd strcat(tempname, "mboxXXXXXX");
420 1.1 cgd mktemp(tempname);
421 1.1 cgd if ((obuf = Fopen(tempname, "w")) == NULL) {
422 1.1 cgd perror(tempname);
423 1.1 cgd relsesigs();
424 1.1 cgd reset(0);
425 1.1 cgd }
426 1.1 cgd if ((ibuf = Fopen(mailname, "r")) == NULL) {
427 1.1 cgd perror(mailname);
428 1.1 cgd Fclose(obuf);
429 1.1 cgd rm(tempname);
430 1.1 cgd relsesigs();
431 1.1 cgd reset(0);
432 1.1 cgd }
433 1.1 cgd fseek(ibuf, mailsize, 0);
434 1.1 cgd while ((c = getc(ibuf)) != EOF)
435 1.1 cgd (void) putc(c, obuf);
436 1.1 cgd Fclose(ibuf);
437 1.1 cgd Fclose(obuf);
438 1.1 cgd if ((ibuf = Fopen(tempname, "r")) == NULL) {
439 1.1 cgd perror(tempname);
440 1.1 cgd rm(tempname);
441 1.1 cgd relsesigs();
442 1.1 cgd reset(0);
443 1.1 cgd }
444 1.1 cgd rm(tempname);
445 1.1 cgd }
446 1.1 cgd printf("\"%s\" ", mailname);
447 1.1 cgd fflush(stdout);
448 1.1 cgd if ((obuf = Fopen(mailname, "r+")) == NULL) {
449 1.1 cgd perror(mailname);
450 1.1 cgd relsesigs();
451 1.1 cgd reset(0);
452 1.1 cgd }
453 1.1 cgd trunc(obuf);
454 1.1 cgd c = 0;
455 1.1 cgd for (mp = &message[0]; mp < &message[msgCount]; mp++) {
456 1.1 cgd if ((mp->m_flag & MDELETED) != 0)
457 1.1 cgd continue;
458 1.1 cgd c++;
459 1.1 cgd if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
460 1.1 cgd perror(mailname);
461 1.1 cgd relsesigs();
462 1.1 cgd reset(0);
463 1.1 cgd }
464 1.1 cgd }
465 1.1 cgd gotcha = (c == 0 && ibuf == NULL);
466 1.1 cgd if (ibuf != NULL) {
467 1.1 cgd while ((c = getc(ibuf)) != EOF)
468 1.1 cgd (void) putc(c, obuf);
469 1.1 cgd Fclose(ibuf);
470 1.1 cgd }
471 1.1 cgd fflush(obuf);
472 1.1 cgd if (ferror(obuf)) {
473 1.1 cgd perror(mailname);
474 1.1 cgd relsesigs();
475 1.1 cgd reset(0);
476 1.1 cgd }
477 1.1 cgd Fclose(obuf);
478 1.1 cgd if (gotcha) {
479 1.1 cgd rm(mailname);
480 1.1 cgd printf("removed\n");
481 1.1 cgd } else
482 1.1 cgd printf("complete\n");
483 1.1 cgd fflush(stdout);
484 1.1 cgd
485 1.1 cgd done:
486 1.1 cgd relsesigs();
487 1.1 cgd }
488