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