msgs.c revision 94504
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 35static 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#ifndef lint 41#if 0 42static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95"; 43#endif 44#endif /* not lint */ 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/usr.bin/msgs/msgs.c 94504 2002-04-12 13:11:42Z charnier $"); 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 110typedef char bool; 111 112FILE *msgsrc; 113FILE *newmsg; 114const char *sep = "-"; 115char inbuf[BUFSIZ]; 116char fname[MAXPATHLEN]; 117char cmdbuf[MAXPATHLEN + MAXPATHLEN]; 118char subj[128]; 119char from[128]; 120char date[128]; 121char *ptr; 122char *in; 123bool local; 124bool ruptible; 125bool totty; 126bool seenfrom; 127bool seensubj; 128bool blankline; 129bool printing = NO; 130bool mailing = NO; 131bool quitit = NO; 132bool sending = NO; 133bool intrpflg = NO; 134uid_t uid; 135int msg; 136int prevmsg; 137int lct; 138int nlines; 139int Lpp = 0; 140time_t t; 141time_t keep; 142 143/* option initialization */ 144bool hdrs = NO; 145bool qopt = NO; 146bool hush = NO; 147bool send_msg = NO; 148bool locomode = NO; 149bool use_pager = NO; 150bool clean = NO; 151bool lastcmd = NO; 152jmp_buf tstpbuf; 153 154 155void ask(const char *); 156void gfrsub(FILE *); 157int linecnt(FILE *); 158int main(int, char *[]); 159int next(char *); 160char *nxtfld(unsigned char *); 161void onsusp(int); 162void onintr(int); 163void prmesg(int); 164static void usage(void); 165 166int 167main(argc, argv) 168int argc; char *argv[]; 169{ 170 bool newrc, already; 171 int rcfirst = 0; /* first message to print (from .rc) */ 172 int rcback = 0; /* amount to back off of rcfirst */ 173 int firstmsg = 0, nextmsg = 0, lastmsg = 0; 174 int blast = 0; 175 struct stat buf; /* stat to check access of bounds */ 176 FILE *bounds; 177 178#ifdef UNBUFFERED 179 setbuf(stdout, NULL); 180#endif 181 setlocale(LC_ALL, ""); 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(argv[0][0])) { /* starting message # */ 192 rcfirst = atoi(argv[0]); 193 } 194 else if (isdigit(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 errx(1, 207 "only the super-user can use the c flag"); 208 clean = YES; 209 break; 210 211 case 'f': /* silently */ 212 hush = YES; 213 break; 214 215 case 'h': /* headers only */ 216 hdrs = YES; 217 break; 218 219 case 'l': /* local msgs only */ 220 locomode = YES; 221 break; 222 223 case 'o': /* option to save last message */ 224 lastcmd = YES; 225 break; 226 227 case 'p': /* pipe thru 'more' during long msgs */ 228 use_pager = YES; 229 break; 230 231 case 'q': /* query only */ 232 qopt = YES; 233 break; 234 235 case 's': /* sending TO msgs */ 236 send_msg = YES; 237 break; 238 239 default: 240 usage(); 241 } 242 } 243 argc--, argv++; 244 } 245 246 /* 247 * determine current message bounds 248 */ 249 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS); 250 251 /* 252 * Test access rights to the bounds file 253 * This can be a little tricky. if(send_msg), then 254 * we will create it. We assume that if(send_msg), 255 * then you have write permission there. 256 * Else, it better be there, or we bail. 257 */ 258 if (send_msg != YES) { 259 if (stat(fname, &buf) < 0) { 260 if (hush != YES) { 261 err(errno, "%s", fname); 262 } else { 263 exit(1); 264 } 265 } 266 } 267 bounds = fopen(fname, "r"); 268 269 if (bounds != NULL) { 270 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg); 271 fclose(bounds); 272 blast = lastmsg; /* save upper bound */ 273 } 274 275 if (clean) 276 keep = t - (rcback? rcback : NDAYS) DAYS; 277 278 if (clean || bounds == NULL) { /* relocate message bounds */ 279 struct dirent *dp; 280 struct stat stbuf; 281 bool seenany = NO; 282 DIR *dirp; 283 284 dirp = opendir(_PATH_MSGS); 285 if (dirp == NULL) 286 err(errno, "%s", _PATH_MSGS); 287 288 firstmsg = 32767; 289 lastmsg = 0; 290 291 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){ 292 register char *cp = dp->d_name; 293 register int i = 0; 294 295 if (dp->d_ino == 0) 296 continue; 297 if (dp->d_namlen == 0) 298 continue; 299 300 if (clean) 301 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp); 302 303 while (isdigit(*cp)) 304 i = i * 10 + *cp++ - '0'; 305 if (*cp) 306 continue; /* not a message! */ 307 308 if (clean) { 309 if (stat(inbuf, &stbuf) != 0) 310 continue; 311 if (stbuf.st_mtime < keep 312 && stbuf.st_mode&S_IWRITE) { 313 unlink(inbuf); 314 continue; 315 } 316 } 317 318 if (i > lastmsg) 319 lastmsg = i; 320 if (i < firstmsg) 321 firstmsg = i; 322 seenany = YES; 323 } 324 closedir(dirp); 325 326 if (!seenany) { 327 if (blast != 0) /* never lower the upper bound! */ 328 lastmsg = blast; 329 firstmsg = lastmsg + 1; 330 } 331 else if (blast > lastmsg) 332 lastmsg = blast; 333 334 if (!send_msg) { 335 bounds = fopen(fname, "w"); 336 if (bounds == NULL) 337 err(errno, "%s", fname); 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 err(errno, "%s", fname); 351 352 nextmsg = lastmsg + 1; 353 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg); 354 newmsg = fopen(fname, "w"); 355 if (newmsg == NULL) 356 err(errno, "%s", fname); 357 chmod(fname, CMODE); 358 359 fprintf(bounds, "%d %d\n", firstmsg, nextmsg); 360 fclose(bounds); 361 362 sending = YES; 363 if (ruptible) 364 signal(SIGINT, onintr); 365 366 if (isatty(fileno(stdin))) { 367 ptr = getpwuid(uid)->pw_name; 368 printf("Message %d:\nFrom %s %sSubject: ", 369 nextmsg, ptr, ctime(&t)); 370 fflush(stdout); 371 fgets(inbuf, sizeof inbuf, stdin); 372 putchar('\n'); 373 fflush(stdout); 374 fprintf(newmsg, "From %s %sSubject: %s\n", 375 ptr, ctime(&t), inbuf); 376 blankline = seensubj = YES; 377 } 378 else 379 blankline = seensubj = NO; 380 for (;;) { 381 fgets(inbuf, sizeof inbuf, stdin); 382 if (feof(stdin) || ferror(stdin)) 383 break; 384 blankline = (blankline || (inbuf[0] == '\n')); 385 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); 386 fputs(inbuf, newmsg); 387 } 388#ifdef OBJECT 389 if (!seensubj) { 390 printf("NOTICE: Messages should have a Subject field!\n"); 391#ifdef REJECT 392 unlink(fname); 393#endif 394 exit(1); 395 } 396#endif 397 exit(ferror(stdin)); 398 } 399 if (clean) 400 exit(0); 401 402 /* 403 * prepare to display messages 404 */ 405 totty = (isatty(fileno(stdout)) != 0); 406 use_pager = use_pager && totty; 407 408 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC); 409 msgsrc = fopen(fname, "r"); 410 if (msgsrc) { 411 newrc = NO; 412 fscanf(msgsrc, "%d\n", &nextmsg); 413 fclose(msgsrc); 414 if (nextmsg > lastmsg+1) { 415 printf("Warning: bounds have been reset (%d, %d)\n", 416 firstmsg, lastmsg); 417 truncate(fname, (off_t)0); 418 newrc = YES; 419 } 420 else if (!rcfirst) 421 rcfirst = nextmsg - rcback; 422 } 423 else 424 newrc = YES; 425 msgsrc = fopen(fname, "r+"); 426 if (msgsrc == NULL) 427 msgsrc = fopen(fname, "w"); 428 if (msgsrc == NULL) 429 err(errno, "%s", fname); 430 if (rcfirst) { 431 if (rcfirst > lastmsg+1) { 432 printf("Warning: the last message is number %d.\n", 433 lastmsg); 434 rcfirst = nextmsg; 435 } 436 if (rcfirst > firstmsg) 437 firstmsg = rcfirst; /* don't set below first msg */ 438 } 439 if (newrc) { 440 nextmsg = firstmsg; 441 rewind(msgsrc); 442 fprintf(msgsrc, "%d\n", nextmsg); 443 fflush(msgsrc); 444 } 445 446#ifdef V7 447 if (totty) { 448 struct winsize win; 449 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) 450 Lpp = win.ws_row; 451 if (Lpp <= 0) { 452 if (tgetent(inbuf, getenv("TERM")) <= 0 453 || (Lpp = tgetnum(__DECONST(char *, "li"))) <= 0) { 454 Lpp = NLINES; 455 } 456 } 457 } 458#endif 459 Lpp -= 6; /* for headers, etc. */ 460 461 already = NO; 462 prevmsg = firstmsg; 463 printing = YES; 464 if (ruptible) 465 signal(SIGINT, onintr); 466 467 /* 468 * Main program loop 469 */ 470 for (msg = firstmsg; msg <= lastmsg; msg++) { 471 472 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg); 473 newmsg = fopen(fname, "r"); 474 if (newmsg == NULL) 475 continue; 476 477 gfrsub(newmsg); /* get From and Subject fields */ 478 if (locomode && !local) { 479 fclose(newmsg); 480 continue; 481 } 482 483 if (qopt) { /* This has to be located here */ 484 printf("There are new messages.\n"); 485 exit(0); 486 } 487 488 if (already && !hdrs) 489 putchar('\n'); 490 491 /* 492 * Print header 493 */ 494 if (totty) 495 signal(SIGTSTP, onsusp); 496 (void) setjmp(tstpbuf); 497 already = YES; 498 nlines = 2; 499 if (seenfrom) { 500 printf("Message %d:\nFrom %s %s", msg, from, date); 501 nlines++; 502 } 503 if (seensubj) { 504 printf("Subject: %s", subj); 505 nlines++; 506 } 507 else { 508 if (seenfrom) { 509 putchar('\n'); 510 nlines++; 511 } 512 while (nlines < 6 513 && fgets(inbuf, sizeof inbuf, newmsg) 514 && inbuf[0] != '\n') { 515 fputs(inbuf, stdout); 516 nlines++; 517 } 518 } 519 520 lct = linecnt(newmsg); 521 if (lct) 522 printf("(%d%slines) ", lct, seensubj? " " : " more "); 523 524 if (hdrs) { 525 printf("\n-----\n"); 526 fclose(newmsg); 527 continue; 528 } 529 530 /* 531 * Ask user for command 532 */ 533 if (totty) 534 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT)); 535 else 536 inbuf[0] = 'y'; 537 if (totty) 538 signal(SIGTSTP, SIG_DFL); 539cmnd: 540 in = inbuf; 541 switch (*in) { 542 case 'x': 543 /* FALLTHROUGH */ 544 case 'X': 545 exit(0); 546 /* NOTREACHED */ 547 case 'q': 548 /* FALLTHROUGH */ 549 case 'Q': 550 quitit = YES; 551 printf("--Postponed--\n"); 552 exit(0); 553 /* NOTREACHED */ 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 624static void 625usage() 626{ 627 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n"); 628 exit(1); 629} 630 631void 632prmesg(length) 633int 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 679void 680onintr(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 */ 705void 706onsusp(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 717int 718linecnt(f) 719FILE *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 732int 733next(buf) 734char *buf; 735{ 736 int i; 737 sscanf(buf, "%d", &i); 738 sprintf(buf, "Goto %d", i); 739 return(--i); 740} 741 742void 743ask(prompt) 744const 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 829void 830gfrsub(infile) 831FILE *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 910char * 911nxtfld(s) 912unsigned 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