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(int argc, char *argv[]) 166 { 167 bool newrc, already; 168 int rcfirst = 0; /* first message to print (from .rc) */ 169 int rcback = 0; /* amount to back off of rcfirst */ 170 int firstmsg = 0, nextmsg = 0, lastmsg = 0; 171 int blast = 0; 172 struct stat buf; /* stat to check access of bounds */ 173 FILE *bounds; 174 175 #ifdef UNBUFFERED 176 setbuf(stdout, NULL); 177 #endif 178 setlocale(LC_ALL, ""); 179 180 time(&t); 181 setuid(uid = getuid()); 182 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL); 183 if (ruptible) 184 signal(SIGINT, SIG_DFL); 185 186 argc--, argv++; 187 while (argc > 0) { 188 if (isdigit(argv[0][0])) { /* starting message # */ 189 rcfirst = atoi(argv[0]); 190 } 191 else if (isdigit(argv[0][1])) { /* backward offset */ 192 rcback = atoi( &( argv[0][1] ) ); 193 } 194 else { 195 ptr = *argv; 196 while (*ptr) switch (*ptr++) { 197 198 case '-': 199 break; 200 201 case 'c': 202 if (uid != SUPERUSER && uid != DAEMON) 203 errx(1, 204 "only the super-user can use the c flag"); 205 clean = YES; 206 break; 207 208 case 'f': /* silently */ 209 hush = YES; 210 break; 211 212 case 'h': /* headers only */ 213 hdrs = YES; 214 break; 215 216 case 'l': /* local msgs only */ 217 locomode = YES; 218 break; 219 220 case 'o': /* option to save last message */ 221 lastcmd = YES; 222 break; 223 224 case 'p': /* pipe thru 'more' during long msgs */ 225 use_pager = YES; 226 break; 227 228 case 'q': /* query only */ 229 qopt = YES; 230 break; 231 232 case 's': /* sending TO msgs */ 233 send_msg = YES; 234 break; 235 236 default: 237 usage(); 238 } 239 } 240 argc--, argv++; 241 } 242 243 /* 244 * determine current message bounds 245 */ 246 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS); 247 248 /* 249 * Test access rights to the bounds file 250 * This can be a little tricky. if(send_msg), then 251 * we will create it. We assume that if(send_msg), 252 * then you have write permission there. 253 * Else, it better be there, or we bail. 254 */ 255 if (send_msg != YES) { 256 if (stat(fname, &buf) < 0) { 257 if (hush != YES) { 258 err(errno, "%s", fname); 259 } else { 260 exit(1); 261 } 262 } 263 } 264 bounds = fopen(fname, "r"); 265 266 if (bounds != NULL) { 267 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg); 268 fclose(bounds); 269 blast = lastmsg; /* save upper bound */ 270 } 271 272 if (clean) 273 keep = t - (rcback? rcback : NDAYS) DAYS; 274 275 if (clean || bounds == NULL) { /* relocate message bounds */ 276 struct dirent *dp; 277 struct stat stbuf; 278 bool seenany = NO; 279 DIR *dirp; 280 281 dirp = opendir(_PATH_MSGS); 282 if (dirp == NULL) 283 err(errno, "%s", _PATH_MSGS); 284 285 firstmsg = 32767; 286 lastmsg = 0; 287 288 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){ 289 char *cp = dp->d_name; 290 int i = 0; 291 292 if (dp->d_ino == 0) 293 continue; 294 if (dp->d_namlen == 0) 295 continue; 296 297 if (clean) 298 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp); 299 300 while (isdigit(*cp)) 301 i = i * 10 + *cp++ - '0'; 302 if (*cp) 303 continue; /* not a message! */ 304 305 if (clean) { 306 if (stat(inbuf, &stbuf) != 0) 307 continue; 308 if (stbuf.st_mtime < keep 309 && stbuf.st_mode&S_IWRITE) { 310 unlink(inbuf); 311 continue; 312 } 313 } 314 315 if (i > lastmsg) 316 lastmsg = i; 317 if (i < firstmsg) 318 firstmsg = i; 319 seenany = YES; 320 } 321 closedir(dirp); 322 323 if (!seenany) { 324 if (blast != 0) /* never lower the upper bound! */ 325 lastmsg = blast; 326 firstmsg = lastmsg + 1; 327 } 328 else if (blast > lastmsg) 329 lastmsg = blast; 330 331 if (!send_msg) { 332 bounds = fopen(fname, "w"); 333 if (bounds == NULL) 334 err(errno, "%s", fname); 335 chmod(fname, CMODE); 336 fprintf(bounds, "%d %d\n", firstmsg, lastmsg); 337 fclose(bounds); 338 } 339 } 340 341 if (send_msg) { 342 /* 343 * Send mode - place msgs in _PATH_MSGS 344 */ 345 bounds = fopen(fname, "w"); 346 if (bounds == NULL) 347 err(errno, "%s", fname); 348 349 nextmsg = lastmsg + 1; 350 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg); 351 newmsg = fopen(fname, "w"); 352 if (newmsg == NULL) 353 err(errno, "%s", fname); 354 chmod(fname, CMODE); 355 356 fprintf(bounds, "%d %d\n", firstmsg, nextmsg); 357 fclose(bounds); 358 359 sending = YES; 360 if (ruptible) 361 signal(SIGINT, onintr); 362 363 if (isatty(fileno(stdin))) { 364 ptr = getpwuid(uid)->pw_name; 365 printf("Message %d:\nFrom %s %sSubject: ", 366 nextmsg, ptr, ctime(&t)); 367 fflush(stdout); 368 fgets(inbuf, sizeof inbuf, stdin); 369 putchar('\n'); 370 fflush(stdout); 371 fprintf(newmsg, "From %s %sSubject: %s\n", 372 ptr, ctime(&t), inbuf); 373 blankline = seensubj = YES; 374 } 375 else 376 blankline = seensubj = NO; 377 for (;;) { 378 fgets(inbuf, sizeof inbuf, stdin); 379 if (feof(stdin) || ferror(stdin)) 380 break; 381 blankline = (blankline || (inbuf[0] == '\n')); 382 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); 383 fputs(inbuf, newmsg); 384 } 385 #ifdef OBJECT 386 if (!seensubj) { 387 printf("NOTICE: Messages should have a Subject field!\n"); 388 #ifdef REJECT 389 unlink(fname); 390 #endif 391 exit(1); 392 } 393 #endif 394 exit(ferror(stdin)); 395 } 396 if (clean) 397 exit(0); 398 399 /* 400 * prepare to display messages 401 */ 402 totty = (isatty(fileno(stdout)) != 0); 403 use_pager = use_pager && totty; 404 405 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC); 406 msgsrc = fopen(fname, "r"); 407 if (msgsrc) { 408 newrc = NO; 409 fscanf(msgsrc, "%d\n", &nextmsg); 410 fclose(msgsrc); 411 if (nextmsg > lastmsg+1) { 412 printf("Warning: bounds have been reset (%d, %d)\n", 413 firstmsg, lastmsg); 414 truncate(fname, (off_t)0); 415 newrc = YES; 416 } 417 else if (!rcfirst) 418 rcfirst = nextmsg - rcback; 419 } 420 else 421 newrc = YES; 422 msgsrc = fopen(fname, "r+"); 423 if (msgsrc == NULL) 424 msgsrc = fopen(fname, "w"); 425 if (msgsrc == NULL) 426 err(errno, "%s", fname); 427 if (rcfirst) { 428 if (rcfirst > lastmsg+1) { 429 printf("Warning: the last message is number %d.\n", 430 lastmsg); 431 rcfirst = nextmsg; 432 } 433 if (rcfirst > firstmsg) 434 firstmsg = rcfirst; /* don't set below first msg */ 435 } 436 if (newrc) { 437 nextmsg = firstmsg; 438 rewind(msgsrc); 439 fprintf(msgsrc, "%d\n", nextmsg); 440 fflush(msgsrc); 441 } 442 443 #ifdef V7 444 if (totty) { 445 struct winsize win; 446 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) 447 Lpp = win.ws_row; 448 if (Lpp <= 0) { 449 if (tgetent(inbuf, getenv("TERM")) <= 0 450 || (Lpp = tgetnum("li")) <= 0) { 451 Lpp = NLINES; 452 } 453 } 454 } 455 #endif 456 Lpp -= 6; /* for headers, etc. */ 457 458 already = NO; 459 prevmsg = firstmsg; 460 printing = YES; 461 if (ruptible) 462 signal(SIGINT, onintr); 463 464 /* 465 * Main program loop 466 */ 467 for (msg = firstmsg; msg <= lastmsg; msg++) { 468 469 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg); 470 newmsg = fopen(fname, "r"); 471 if (newmsg == NULL) 472 continue; 473 474 gfrsub(newmsg); /* get From and Subject fields */ 475 if (locomode && !local) { 476 fclose(newmsg); 477 continue; 478 } 479 480 if (qopt) { /* This has to be located here */ 481 printf("There are new messages.\n"); 482 exit(0); 483 } 484 485 if (already && !hdrs) 486 putchar('\n'); 487 488 /* 489 * Print header 490 */ 491 if (totty) 492 signal(SIGTSTP, onsusp); 493 (void) setjmp(tstpbuf); 494 already = YES; 495 nlines = 2; 496 if (seenfrom) { 497 printf("Message %d:\nFrom %s %s", msg, from, date); 498 nlines++; 499 } 500 if (seensubj) { 501 printf("Subject: %s", subj); 502 nlines++; 503 } 504 else { 505 if (seenfrom) { 506 putchar('\n'); 507 nlines++; 508 } 509 while (nlines < 6 510 && fgets(inbuf, sizeof inbuf, newmsg) 511 && inbuf[0] != '\n') { 512 fputs(inbuf, stdout); 513 nlines++; 514 } 515 } 516 517 lct = linecnt(newmsg); 518 if (lct) 519 printf("(%d%slines) ", lct, seensubj? " " : " more "); 520 521 if (hdrs) { 522 printf("\n-----\n"); 523 fclose(newmsg); 524 continue; 525 } 526 527 /* 528 * Ask user for command 529 */ 530 if (totty) 531 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT)); 532 else 533 inbuf[0] = 'y'; 534 if (totty) 535 signal(SIGTSTP, SIG_DFL); 536 cmnd: 537 in = inbuf; 538 switch (*in) { 539 case 'x': 540 /* FALLTHROUGH */ 541 case 'X': 542 exit(0); 543 /* NOTREACHED */ 544 545 case 'q': 546 /* FALLTHROUGH */ 547 case 'Q': 548 quitit = YES; 549 printf("--Postponed--\n"); 550 exit(0); 551 /* NOTREACHED */ 552 553 case 'n': 554 /* FALLTHROUGH */ 555 case 'N': 556 if (msg >= nextmsg) sep = "Flushed"; 557 prevmsg = msg; 558 break; 559 560 case 'p': 561 /* FALLTHROUGH */ 562 case 'P': 563 use_pager = (*in++ == 'p'); 564 /* FALLTHROUGH */ 565 case '\n': 566 /* FALLTHROUGH */ 567 case 'y': 568 default: 569 if (*in == '-') { 570 msg = prevmsg-1; 571 sep = "replay"; 572 break; 573 } 574 if (isdigit(*in)) { 575 msg = next(in); 576 sep = in; 577 break; 578 } 579 580 prmesg(nlines + lct + (seensubj? 1 : 0)); 581 prevmsg = msg; 582 583 } 584 585 printf("--%s--\n", sep); 586 sep = "-"; 587 if (msg >= nextmsg) { 588 nextmsg = msg + 1; 589 rewind(msgsrc); 590 fprintf(msgsrc, "%d\n", nextmsg); 591 fflush(msgsrc); 592 } 593 if (newmsg) 594 fclose(newmsg); 595 if (quitit) 596 break; 597 } 598 599 /* 600 * Make sure .rc file gets updated 601 */ 602 if (--msg >= nextmsg) { 603 nextmsg = msg + 1; 604 rewind(msgsrc); 605 fprintf(msgsrc, "%d\n", nextmsg); 606 fflush(msgsrc); 607 } 608 if (already && !quitit && lastcmd && totty) { 609 /* 610 * save or reply to last message? 611 */ 612 msg = prevmsg; 613 ask(NOMORE); 614 if (inbuf[0] == '-' || isdigit(inbuf[0])) 615 goto cmnd; 616 } 617 if (!(already || hush || qopt)) 618 printf("No new messages.\n"); 619 exit(0); 620 /* NOTREACHED */ 621 } 622 623 static void 624 usage(void) 625 { 626 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n"); 627 exit(1); 628 } 629 630 void 631 prmesg(int length) 632 { 633 FILE *outf; 634 char *env_pager; 635 636 if (use_pager && length > Lpp) { 637 signal(SIGPIPE, SIG_IGN); 638 signal(SIGQUIT, SIG_IGN); 639 if ((env_pager = getenv("PAGER")) == NULL) { 640 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp); 641 } else { 642 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager); 643 } 644 outf = popen(cmdbuf, "w"); 645 if (!outf) 646 outf = stdout; 647 else 648 setbuf(outf, (char *)NULL); 649 } 650 else 651 outf = stdout; 652 653 if (seensubj) 654 putc('\n', outf); 655 656 while (fgets(inbuf, sizeof inbuf, newmsg)) { 657 fputs(inbuf, outf); 658 if (ferror(outf)) { 659 clearerr(outf); 660 break; 661 } 662 } 663 664 if (outf != stdout) { 665 pclose(outf); 666 signal(SIGPIPE, SIG_DFL); 667 signal(SIGQUIT, SIG_DFL); 668 } 669 else { 670 fflush(stdout); 671 } 672 673 /* force wait on output */ 674 tcdrain(fileno(stdout)); 675 } 676 677 void 678 onintr(int unused __unused) 679 { 680 signal(SIGINT, onintr); 681 if (mailing) 682 unlink(fname); 683 if (sending) { 684 unlink(fname); 685 puts("--Killed--"); 686 exit(1); 687 } 688 if (printing) { 689 putchar('\n'); 690 if (hdrs) 691 exit(0); 692 sep = "Interrupt"; 693 if (newmsg) 694 fseeko(newmsg, (off_t)0, SEEK_END); 695 intrpflg = YES; 696 } 697 } 698 699 /* 700 * We have just gotten a susp. Suspend and prepare to resume. 701 */ 702 void 703 onsusp(int unused __unused) 704 { 705 signal(SIGTSTP, SIG_DFL); 706 sigsetmask(0); 707 kill(0, SIGTSTP); 708 signal(SIGTSTP, onsusp); 709 if (!mailing) 710 longjmp(tstpbuf, 0); 711 } 712 713 int 714 linecnt(FILE *f) 715 { 716 off_t oldpos = ftello(f); 717 int l = 0; 718 char lbuf[BUFSIZ]; 719 720 while (fgets(lbuf, sizeof lbuf, f)) 721 l++; 722 clearerr(f); 723 fseeko(f, oldpos, SEEK_SET); 724 return (l); 725 } 726 727 int 728 next(char *buf) 729 { 730 int i; 731 sscanf(buf, "%d", &i); 732 sprintf(buf, "Goto %d", i); 733 return(--i); 734 } 735 736 void 737 ask(const char *prompt) 738 { 739 char inch; 740 int n, cmsg, fd; 741 off_t oldpos; 742 FILE *cpfrom, *cpto; 743 744 printf("%s ", prompt); 745 fflush(stdout); 746 intrpflg = NO; 747 (void) fgets(inbuf, sizeof inbuf, stdin); 748 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n') 749 inbuf[n - 1] = '\0'; 750 if (intrpflg) 751 inbuf[0] = 'x'; 752 753 /* 754 * Handle 'mail' and 'save' here. 755 */ 756 if ((inch = inbuf[0]) == 's' || inch == 'm') { 757 if (inbuf[1] == '-') 758 cmsg = prevmsg; 759 else if (isdigit(inbuf[1])) 760 cmsg = atoi(&inbuf[1]); 761 else 762 cmsg = msg; 763 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg); 764 765 oldpos = ftello(newmsg); 766 767 cpfrom = fopen(fname, "r"); 768 if (!cpfrom) { 769 printf("Message %d not found\n", cmsg); 770 ask (prompt); 771 return; 772 } 773 774 if (inch == 's') { 775 in = nxtfld(inbuf); 776 if (*in) { 777 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */ 778 fname[n] = in[n]; 779 } 780 fname[n] = NULL; 781 } 782 else 783 strcpy(fname, "Messages"); 784 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND); 785 } 786 else { 787 strcpy(fname, _PATH_TMP); 788 fd = mkstemp(fname); 789 if (fd != -1) { 790 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, 791 fname); 792 mailing = YES; 793 } 794 } 795 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) { 796 if (fd != -1) 797 close(fd); 798 warn("%s", fname); 799 mailing = NO; 800 fseeko(newmsg, oldpos, SEEK_SET); 801 ask(prompt); 802 return; 803 } 804 805 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom))) 806 fwrite(inbuf, 1, n, cpto); 807 808 fclose(cpfrom); 809 fclose(cpto); 810 fseeko(newmsg, oldpos, SEEK_SET);/* reposition current message */ 811 if (inch == 's') 812 printf("Message %d saved in \"%s\"\n", cmsg, fname); 813 else { 814 system(cmdbuf); 815 unlink(fname); 816 mailing = NO; 817 } 818 ask(prompt); 819 } 820 } 821 822 void 823 gfrsub(FILE *infile) 824 { 825 off_t frompos; 826 int count; 827 828 seensubj = seenfrom = NO; 829 local = YES; 830 subj[0] = from[0] = date[0] = NULL; 831 832 /* 833 * Is this a normal message? 834 */ 835 if (fgets(inbuf, sizeof inbuf, infile)) { 836 if (strncmp(inbuf, "From", 4)==0) { 837 /* 838 * expected form starts with From 839 */ 840 seenfrom = YES; 841 frompos = ftello(infile); 842 ptr = from; 843 in = nxtfld(inbuf); 844 if (*in) { 845 count = sizeof(from) - 1; 846 while (*in && *in > ' ' && count-- > 0) { 847 if (*in == ':' || *in == '@' || 848 *in == '!') 849 local = NO; 850 *ptr++ = *in++; 851 } 852 } 853 *ptr = NULL; 854 if (*(in = nxtfld(in))) 855 strncpy(date, in, sizeof date); 856 else { 857 date[0] = '\n'; 858 date[1] = NULL; 859 } 860 } 861 else { 862 /* 863 * not the expected form 864 */ 865 rewind(infile); 866 return; 867 } 868 } 869 else 870 /* 871 * empty file ? 872 */ 873 return; 874 875 /* 876 * look for Subject line until EOF or a blank line 877 */ 878 while (fgets(inbuf, sizeof inbuf, infile) 879 && !(blankline = (inbuf[0] == '\n'))) { 880 /* 881 * extract Subject line 882 */ 883 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) { 884 seensubj = YES; 885 frompos = ftello(infile); 886 strncpy(subj, nxtfld(inbuf), sizeof subj); 887 } 888 } 889 if (!blankline) 890 /* 891 * ran into EOF 892 */ 893 fseeko(infile, frompos, SEEK_SET); 894 895 if (!seensubj) 896 /* 897 * for possible use with Mail 898 */ 899 strncpy(subj, "(No Subject)\n", sizeof subj); 900 } 901 902 char * 903 nxtfld(unsigned char *s) 904 { 905 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */ 906 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */ 907 return (s); 908 } 909