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