1 /* $NetBSD: msgs.c,v 1.17 2001/07/01 00:20:47 mjl Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95"; 45 #else 46 __RCSID("$NetBSD: msgs.c,v 1.17 2001/07/01 00:20:47 mjl Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 /* 51 * msgs - a user bulletin board program 52 * 53 * usage: 54 * msgs [fhlopqr] [[-]number] to read messages 55 * msgs -s to place messages 56 * msgs -c [-days] to clean up the bulletin board 57 * 58 * prompt commands are: 59 * y print message 60 * n flush message, go to next message 61 * q flush message, quit 62 * p print message, turn on 'pipe thru more' mode 63 * P print message, turn off 'pipe thru more' mode 64 * - reprint last message 65 * s[-][<num>] [<filename>] save message 66 * m[-][<num>] mail with message in temp mbox 67 * x exit without flushing this message 68 * <num> print message number <num> 69 */ 70 71 #define V7 /* will look for TERM in the environment */ 72 #define OBJECT /* will object to messages without Subjects */ 73 #define REJECT /* will reject messages without Subjects 74 (OBJECT must be defined also) */ 75 /*#define UNBUFFERED */ /* use unbuffered output */ 76 77 #include <sys/param.h> 78 #include <sys/ioctl.h> 79 #include <sys/stat.h> 80 81 #include <ctype.h> 82 #include <dirent.h> 83 #include <err.h> 84 #include <errno.h> 85 #include <pwd.h> 86 #include <setjmp.h> 87 #include <signal.h> 88 #include <stdio.h> 89 #include <stdlib.h> 90 #include <string.h> 91 #include <termcap.h> 92 #include <termios.h> 93 #include <time.h> 94 #include <unistd.h> 95 96 #include "pathnames.h" 97 98 #define CMODE 0664 /* bounds file creation mode */ 99 #define NO 0 100 #define YES 1 101 #define SUPERUSER 0 /* superuser uid */ 102 #define DAEMON 1 /* daemon uid */ 103 #define NLINES 24 /* default number of lines/crt screen */ 104 #define NDAYS 21 /* default keep time for messages */ 105 #define DAYS *24*60*60 /* seconds/day */ 106 #define MSGSRC ".msgsrc" /* user's rc file */ 107 #define BOUNDS "bounds" /* message bounds file */ 108 #define NEXT "Next message? [yq]" 109 #define MORE "More? [ynq]" 110 #define NOMORE "(No more) [q] ?" 111 112 typedef char bool; 113 114 FILE *msgsrc; 115 FILE *newmsg; 116 char *sep = "-"; 117 char inbuf[BUFSIZ]; 118 char fname[MAXPATHLEN]; 119 char cmdbuf[MAXPATHLEN + 16]; 120 char subj[128]; 121 char from[128]; 122 char date[128]; 123 char *ptr; 124 char *in; 125 bool local; 126 bool ruptible; 127 bool totty; 128 bool seenfrom; 129 bool seensubj; 130 bool blankline; 131 bool printing = NO; 132 bool mailing = NO; 133 bool quitit = NO; 134 bool sending = NO; 135 bool intrpflg = NO; 136 bool restricted = NO; 137 int uid; 138 int msg; 139 int prevmsg; 140 int lct; 141 int nlines; 142 int Lpp = 0; 143 time_t t; 144 time_t keep; 145 146 void ask __P((char *)); 147 void gfrsub __P((FILE *)); 148 int linecnt __P((FILE *)); 149 int main __P((int, char *[])); 150 int next __P((char *)); 151 char *nxtfld __P((char *)); 152 void onintr __P((int)); 153 void onsusp __P((int)); 154 void prmesg __P((int)); 155 156 /* option initialization */ 157 bool hdrs = NO; 158 bool qopt = NO; 159 bool hush = NO; 160 bool send_msg = NO; 161 bool locomode = NO; 162 bool use_pager = NO; 163 bool clean = NO; 164 bool lastcmd = NO; 165 jmp_buf tstpbuf; 166 167 int 168 main(argc, argv) 169 int argc; char *argv[]; 170 { 171 bool newrc, already; 172 int rcfirst = 0; /* first message to print (from .rc) */ 173 int rcback = 0; /* amount to back off of rcfirst */ 174 int firstmsg, nextmsg, lastmsg = 0; 175 int blast = 0; 176 FILE *bounds; 177 struct passwd *pw; 178 179 #ifdef UNBUFFERED 180 setbuf(stdout, NULL); 181 #endif 182 183 time(&t); 184 setuid(uid = getuid()); 185 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL); 186 if (ruptible) 187 signal(SIGINT, SIG_DFL); 188 189 argc--, argv++; 190 while (argc > 0) { 191 if (isdigit((unsigned char)argv[0][0])) {/* starting message # */ 192 rcfirst = atoi(argv[0]); 193 } 194 else if (isdigit((unsigned char)argv[0][1])) {/* backward offset */ 195 rcback = atoi( &( argv[0][1] ) ); 196 } 197 else { 198 ptr = *argv; 199 while (*ptr) switch (*ptr++) { 200 201 case '-': 202 break; 203 204 case 'c': 205 if (uid != SUPERUSER && uid != DAEMON) { 206 fprintf(stderr, "Sorry\n"); 207 exit(1); 208 } 209 clean = YES; 210 break; 211 212 case 'f': /* silently */ 213 hush = YES; 214 break; 215 216 case 'h': /* headers only */ 217 hdrs = YES; 218 break; 219 220 case 'l': /* local msgs only */ 221 locomode = YES; 222 break; 223 224 case 'o': /* option to save last message */ 225 lastcmd = YES; 226 break; 227 228 case 'p': /* pipe thru 'more' during long msgs */ 229 use_pager = YES; 230 break; 231 232 case 'q': /* query only */ 233 qopt = YES; 234 break; 235 236 case 'r': /* restricted */ 237 restricted = YES; 238 break; 239 240 241 case 's': /* sending TO msgs */ 242 send_msg = YES; 243 break; 244 245 default: 246 fprintf(stderr, 247 "usage: msgs [cfhlopqrs] [[-]number]\n"); 248 exit(1); 249 } 250 } 251 argc--, argv++; 252 } 253 254 /* 255 * determine current message bounds 256 */ 257 snprintf(fname, sizeof (fname), "%s/%s", _PATH_MSGS, BOUNDS); 258 bounds = fopen(fname, "r"); 259 260 if (bounds != NULL) { 261 if (fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg) < 2) 262 firstmsg = lastmsg = 0; 263 fclose(bounds); 264 blast = lastmsg; /* save upper bound */ 265 } 266 267 if (clean) 268 keep = t - (rcback? rcback : NDAYS) DAYS; 269 270 if (clean || bounds == NULL) { /* relocate message bounds */ 271 struct dirent *dp; 272 struct stat stbuf; 273 bool seenany = NO; 274 DIR *dirp; 275 276 dirp = opendir(_PATH_MSGS); 277 if (dirp == NULL) { 278 perror(_PATH_MSGS); 279 exit(errno); 280 } 281 chmod(fname, CMODE); 282 283 firstmsg = 32767; 284 lastmsg = 0; 285 286 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){ 287 char *cp = dp->d_name; 288 int i = 0; 289 290 if (dp->d_ino == 0) 291 continue; 292 #ifndef __SVR4 293 if (dp->d_namlen == 0) 294 continue; 295 #endif 296 297 if (clean) 298 snprintf(inbuf, sizeof (inbuf), "%s/%s", 299 _PATH_MSGS, cp); 300 301 while (isdigit((unsigned char)*cp)) 302 i = i * 10 + *cp++ - '0'; 303 if (*cp) 304 continue; /* not a message! */ 305 306 if (clean) { 307 if (stat(inbuf, &stbuf) != 0) 308 continue; 309 if (stbuf.st_mtime < keep 310 && stbuf.st_mode&S_IWRITE) { 311 unlink(inbuf); 312 continue; 313 } 314 } 315 316 if (i > lastmsg) 317 lastmsg = i; 318 if (i < firstmsg) 319 firstmsg = i; 320 seenany = YES; 321 } 322 closedir(dirp); 323 324 if (!seenany) { 325 if (blast != 0) /* never lower the upper bound! */ 326 lastmsg = blast; 327 firstmsg = lastmsg + 1; 328 } 329 else if (blast > lastmsg) 330 lastmsg = blast; 331 332 if (!send_msg) { 333 bounds = fopen(fname, "w"); 334 if (bounds == NULL) { 335 perror(fname); 336 exit(errno); 337 } 338 chmod(fname, CMODE); 339 fprintf(bounds, "%d %d\n", firstmsg, lastmsg); 340 fclose(bounds); 341 } 342 } 343 344 if (send_msg) { 345 /* 346 * Send mode - place msgs in _PATH_MSGS 347 */ 348 bounds = fopen(fname, "w"); 349 if (bounds == NULL) { 350 perror(fname); 351 exit(errno); 352 } 353 354 nextmsg = lastmsg + 1; 355 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, nextmsg); 356 newmsg = fopen(fname, "w"); 357 if (newmsg == NULL) { 358 perror(fname); 359 exit(errno); 360 } 361 chmod(fname, 0644); 362 363 fprintf(bounds, "%d %d\n", firstmsg, nextmsg); 364 fclose(bounds); 365 366 sending = YES; 367 if (ruptible) 368 signal(SIGINT, onintr); 369 370 if (isatty(fileno(stdin))) { 371 pw = getpwuid(uid); 372 if (!pw) { 373 fprintf(stderr, "Who are you?\n"); 374 exit(1); 375 } 376 printf("Message %d:\nFrom %s %sSubject: ", 377 nextmsg, pw->pw_name, ctime(&t)); 378 fflush(stdout); 379 fgets(inbuf, sizeof inbuf, stdin); 380 putchar('\n'); 381 fflush(stdout); 382 fprintf(newmsg, "From %s %sSubject: %s\n", 383 ptr, ctime(&t), inbuf); 384 blankline = seensubj = YES; 385 } 386 else 387 blankline = seensubj = NO; 388 for (;;) { 389 fgets(inbuf, sizeof inbuf, stdin); 390 if (feof(stdin) || ferror(stdin)) 391 break; 392 blankline = (blankline || (inbuf[0] == '\n')); 393 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); 394 fputs(inbuf, newmsg); 395 } 396 #ifdef OBJECT 397 if (!seensubj) { 398 printf("NOTICE: Messages should have a Subject field!\n"); 399 #ifdef REJECT 400 unlink(fname); 401 #endif 402 exit(1); 403 } 404 #endif 405 exit(ferror(stdin)); 406 } 407 if (clean) 408 exit(0); 409 410 /* 411 * prepare to display messages 412 */ 413 totty = (isatty(fileno(stdout)) != 0); 414 use_pager = use_pager && totty; 415 416 { 417 char *home = getenv("HOME"); 418 if(home == NULL || *home == '\0') 419 errx(1, "$HOME not set"); 420 snprintf(fname, sizeof (fname), "%s/%s", home, MSGSRC); 421 } 422 msgsrc = fopen(fname, "r"); 423 if (msgsrc) { 424 newrc = NO; 425 fscanf(msgsrc, "%d\n", &nextmsg); 426 fclose(msgsrc); 427 if (nextmsg > lastmsg+1) { 428 printf("Warning: bounds have been reset (%d, %d)\n", 429 firstmsg, lastmsg); 430 truncate(fname, (off_t)0); 431 newrc = YES; 432 } 433 else if (!rcfirst) 434 rcfirst = nextmsg - rcback; 435 } 436 else 437 newrc = YES; 438 msgsrc = fopen(fname, "r+"); 439 if (msgsrc == NULL) 440 msgsrc = fopen(fname, "w"); 441 if (msgsrc == NULL) { 442 perror(fname); 443 exit(errno); 444 } 445 if (rcfirst) { 446 if (rcfirst > lastmsg+1) { 447 printf("Warning: the last message is number %d.\n", 448 lastmsg); 449 rcfirst = nextmsg; 450 } 451 if (rcfirst > firstmsg) 452 firstmsg = rcfirst; /* don't set below first msg */ 453 } 454 if (newrc) { 455 nextmsg = firstmsg; 456 fseek(msgsrc, 0L, 0); 457 fprintf(msgsrc, "%d\n", nextmsg); 458 fflush(msgsrc); 459 } 460 461 #ifdef V7 462 if (totty) { 463 struct winsize win; 464 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) 465 Lpp = win.ws_row; 466 if (Lpp <= 0) { 467 if (tgetent(inbuf, getenv("TERM")) <= 0 468 || (Lpp = tgetnum("li")) <= 0) { 469 Lpp = NLINES; 470 } 471 } 472 } 473 #endif 474 Lpp -= 6; /* for headers, etc. */ 475 476 already = NO; 477 prevmsg = firstmsg; 478 printing = YES; 479 if (ruptible) 480 signal(SIGINT, onintr); 481 482 /* 483 * Main program loop 484 */ 485 for (msg = firstmsg; msg <= lastmsg; msg++) { 486 487 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, msg); 488 newmsg = fopen(fname, "r"); 489 if (newmsg == NULL) 490 continue; 491 492 gfrsub(newmsg); /* get From and Subject fields */ 493 if (locomode && !local) { 494 fclose(newmsg); 495 continue; 496 } 497 498 if (qopt) { /* This has to be located here */ 499 printf("There are new messages.\n"); 500 exit(0); 501 } 502 503 if (already && !hdrs) 504 putchar('\n'); 505 506 /* 507 * Print header 508 */ 509 if (totty) 510 signal(SIGTSTP, onsusp); 511 (void) setjmp(tstpbuf); 512 already = YES; 513 nlines = 2; 514 if (seenfrom) { 515 printf("Message %d:\nFrom %s %s", msg, from, date); 516 nlines++; 517 } 518 if (seensubj) { 519 printf("Subject: %s", subj); 520 nlines++; 521 } 522 else { 523 if (seenfrom) { 524 putchar('\n'); 525 nlines++; 526 } 527 while (nlines < 6 528 && fgets(inbuf, sizeof inbuf, newmsg) 529 && inbuf[0] != '\n') { 530 fputs(inbuf, stdout); 531 nlines++; 532 } 533 } 534 535 lct = linecnt(newmsg); 536 if (lct) 537 printf("(%d%slines) ", lct, seensubj? " " : " more "); 538 539 if (hdrs) { 540 printf("\n-----\n"); 541 fclose(newmsg); 542 continue; 543 } 544 545 /* 546 * Ask user for command 547 */ 548 if (totty) 549 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT)); 550 else 551 inbuf[0] = 'y'; 552 if (totty) 553 signal(SIGTSTP, SIG_DFL); 554 cmnd: 555 in = inbuf; 556 switch (*in) { 557 case 'x': 558 case 'X': 559 exit(0); 560 561 case 'q': 562 case 'Q': 563 quitit = YES; 564 printf("--Postponed--\n"); 565 exit(0); 566 /* intentional fall-thru */ 567 case 'n': 568 case 'N': 569 if (msg >= nextmsg) sep = "Flushed"; 570 prevmsg = msg; 571 break; 572 573 case 'p': 574 case 'P': 575 use_pager = (*in++ == 'p'); 576 /* intentional fallthru */ 577 case '\n': 578 case 'y': 579 default: 580 if (*in == '-') { 581 msg = prevmsg-1; 582 sep = "replay"; 583 break; 584 } 585 if (isdigit((unsigned char)*in)) { 586 msg = next(in); 587 sep = in; 588 break; 589 } 590 591 prmesg(nlines + lct + (seensubj? 1 : 0)); 592 prevmsg = msg; 593 594 } 595 596 printf("--%s--\n", sep); 597 sep = "-"; 598 if (msg >= nextmsg) { 599 nextmsg = msg + 1; 600 fseek(msgsrc, 0L, 0); 601 fprintf(msgsrc, "%d\n", nextmsg); 602 fflush(msgsrc); 603 } 604 if (newmsg) 605 fclose(newmsg); 606 if (quitit) 607 break; 608 } 609 610 /* 611 * Make sure .rc file gets updated 612 */ 613 if (--msg >= nextmsg) { 614 nextmsg = msg + 1; 615 fseek(msgsrc, 0L, 0); 616 fprintf(msgsrc, "%d\n", nextmsg); 617 fflush(msgsrc); 618 } 619 if (already && !quitit && lastcmd && totty) { 620 /* 621 * save or reply to last message? 622 */ 623 msg = prevmsg; 624 ask(NOMORE); 625 if (inbuf[0] == '-' || isdigit((unsigned char)inbuf[0])) 626 goto cmnd; 627 } 628 if (!(already || hush || qopt)) 629 printf("No new messages.\n"); 630 exit(0); 631 } 632 633 void 634 prmesg(length) 635 int length; 636 { 637 FILE *outf; 638 char *env_pager; 639 640 if (use_pager && length > Lpp) { 641 signal(SIGPIPE, SIG_IGN); 642 signal(SIGQUIT, SIG_IGN); 643 if ((env_pager = getenv("PAGER")) == NULL || 644 env_pager[0] == '\0') { 645 snprintf(cmdbuf, sizeof(cmdbuf), "%s -z%d", 646 _PATH_PAGER, Lpp); 647 } else { 648 strlcpy(cmdbuf, env_pager, sizeof (cmdbuf)); 649 } 650 outf = popen(cmdbuf, "w"); 651 if (!outf) 652 outf = stdout; 653 else 654 setbuf(outf, (char *)NULL); 655 } 656 else 657 outf = stdout; 658 659 if (seensubj) 660 putc('\n', outf); 661 662 while (fgets(inbuf, sizeof inbuf, newmsg)) { 663 fputs(inbuf, outf); 664 if (ferror(outf)) { 665 clearerr(outf); 666 break; 667 } 668 } 669 670 if (outf != stdout) { 671 pclose(outf); 672 signal(SIGPIPE, SIG_DFL); 673 signal(SIGQUIT, SIG_DFL); 674 } 675 else { 676 fflush(stdout); 677 } 678 679 /* trick to force wait on output */ 680 tcdrain(fileno(stdout)); 681 } 682 683 void 684 onintr(dummy) 685 int dummy; 686 { 687 signal(SIGINT, onintr); 688 if (mailing) 689 unlink(fname); 690 if (sending) { 691 unlink(fname); 692 puts("--Killed--"); 693 exit(1); 694 } 695 if (printing) { 696 putchar('\n'); 697 if (hdrs) 698 exit(0); 699 sep = "Interrupt"; 700 if (newmsg) 701 fseek(newmsg, 0L, 2); 702 intrpflg = YES; 703 } 704 } 705 706 /* 707 * We have just gotten a susp. Suspend and prepare to resume. 708 */ 709 void 710 onsusp(dummy) 711 int dummy; 712 { 713 714 signal(SIGTSTP, SIG_DFL); 715 sigsetmask(0); 716 kill(0, SIGTSTP); 717 signal(SIGTSTP, onsusp); 718 if (!mailing) 719 longjmp(tstpbuf, 0); 720 } 721 722 int 723 linecnt(f) 724 FILE *f; 725 { 726 off_t oldpos = ftell(f); 727 int l = 0; 728 char lbuf[BUFSIZ]; 729 730 while (fgets(lbuf, sizeof lbuf, f)) 731 l++; 732 clearerr(f); 733 fseek(f, oldpos, 0); 734 return (l); 735 } 736 737 int 738 next(buf) 739 char *buf; 740 { 741 int i; 742 sscanf(buf, "%d", &i); 743 snprintf(buf, sizeof (buf), "Goto %d", i); 744 return(--i); 745 } 746 747 void 748 ask(prompt) 749 char *prompt; 750 { 751 char inch; 752 int n, cmsg; 753 off_t oldpos; 754 FILE *cpfrom, *cpto; 755 756 printf("%s ", prompt); 757 fflush(stdout); 758 intrpflg = NO; 759 (void) fgets(inbuf, sizeof inbuf, stdin); 760 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n') 761 inbuf[n - 1] = '\0'; 762 if (intrpflg) 763 inbuf[0] = 'x'; 764 765 /* 766 * Handle 'mail' and 'save' here. 767 */ 768 if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) { 769 if (inbuf[1] == '-') 770 cmsg = prevmsg; 771 else if (isdigit((unsigned char)inbuf[1])) 772 cmsg = atoi(&inbuf[1]); 773 else 774 cmsg = msg; 775 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, cmsg); 776 777 oldpos = ftell(newmsg); 778 779 cpfrom = fopen(fname, "r"); 780 if (!cpfrom) { 781 printf("Message %d not found\n", cmsg); 782 ask (prompt); 783 return; 784 } 785 786 if (inch == 's') { 787 in = nxtfld(inbuf); 788 if (*in) { 789 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */ 790 fname[n] = in[n]; 791 } 792 fname[n] = '\0'; 793 } 794 else 795 strlcpy(fname, "Messages", sizeof(fname)); 796 } 797 else { 798 int fd; 799 800 strlcpy(fname, _PATH_TMP, sizeof(fname)); 801 fd = mkstemp(fname); 802 if (fd == -1) 803 err(1, "mkstemp failed"); 804 close(fd); 805 snprintf(cmdbuf, sizeof (cmdbuf), _PATH_MAIL, fname); 806 mailing = YES; 807 } 808 cpto = fopen(fname, "a"); 809 if (!cpto) { 810 perror(fname); 811 mailing = NO; 812 fseek(newmsg, oldpos, 0); 813 ask(prompt); 814 return; 815 } 816 817 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)) != 0) 818 fwrite(inbuf, 1, n, cpto); 819 820 fclose(cpfrom); 821 fclose(cpto); 822 fseek(newmsg, oldpos, 0); /* reposition current message */ 823 if (inch == 's') 824 printf("Message %d saved in \"%s\"\n", cmsg, fname); 825 else { 826 system(cmdbuf); 827 unlink(fname); 828 mailing = NO; 829 } 830 ask(prompt); 831 } 832 } 833 834 void 835 gfrsub(infile) 836 FILE *infile; 837 { 838 off_t frompos; 839 840 seensubj = seenfrom = NO; 841 local = YES; 842 subj[0] = from[0] = date[0] = 0; 843 844 /* 845 * Is this a normal message? 846 */ 847 if (fgets(inbuf, sizeof inbuf, infile)) { 848 if (strncmp(inbuf, "From", 4)==0) { 849 /* 850 * expected form starts with From 851 */ 852 seenfrom = YES; 853 frompos = ftell(infile); 854 ptr = from; 855 in = nxtfld(inbuf); 856 if (*in) while (*in && *in > ' ') { 857 if (*in == ':' || *in == '@' || *in == '!') 858 local = NO; 859 *ptr++ = *in++; 860 /* what about sizeof from ? */ 861 } 862 *ptr = '\0'; 863 if (*(in = nxtfld(in))) 864 strncpy(date, in, sizeof date); 865 else { 866 date[0] = '\n'; 867 date[1] = '\0'; 868 } 869 } 870 else { 871 /* 872 * not the expected form 873 */ 874 fseek(infile, 0L, 0); 875 return; 876 } 877 } 878 else 879 /* 880 * empty file ? 881 */ 882 return; 883 884 /* 885 * look for Subject line until EOF or a blank line 886 */ 887 while (fgets(inbuf, sizeof inbuf, infile) 888 && !(blankline = (inbuf[0] == '\n'))) { 889 /* 890 * extract Subject line 891 */ 892 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) { 893 seensubj = YES; 894 frompos = ftell(infile); 895 strncpy(subj, nxtfld(inbuf), sizeof subj); 896 } 897 } 898 if (!blankline) 899 /* 900 * ran into EOF 901 */ 902 fseek(infile, frompos, 0); 903 904 if (!seensubj) 905 /* 906 * for possible use with Mail 907 */ 908 strncpy(subj, "(No Subject)\n", sizeof subj); 909 } 910 911 char * 912 nxtfld(s) 913 char *s; 914 { 915 if (*s) while (*s && *s > ' ') s++; /* skip over this field */ 916 if (*s) while (*s && *s <= ' ') s++; /* find start of next field */ 917 return (s); 918 } 919