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