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