]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/msgs/msgs.c
This commit was generated by cvs2svn to compensate for changes in r73188,
[FreeBSD/FreeBSD.git] / usr.bin / msgs / msgs.c
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 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)msgs.c      8.2 (Berkeley) 4/28/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 /*
49  * msgs - a user bulletin board program
50  *
51  * usage:
52  *      msgs [fhlopq] [[-]number]       to read messages
53  *      msgs -s                         to place messages
54  *      msgs -c [-days]                 to clean up the bulletin board
55  *
56  * prompt commands are:
57  *      y       print message
58  *      n       flush message, go to next message
59  *      q       flush message, quit
60  *      p       print message, turn on 'pipe thru more' mode
61  *      P       print message, turn off 'pipe thru more' mode
62  *      -       reprint last message
63  *      s[-][<num>] [<filename>]        save message
64  *      m[-][<num>]     mail with message in temp mbox
65  *      x       exit without flushing this message
66  *      <num>   print message number <num>
67  */
68
69 #define V7              /* will look for TERM in the environment */
70 #define OBJECT          /* will object to messages without Subjects */
71 /* #define REJECT */    /* will reject messages without Subjects
72                            (OBJECT must be defined also) */
73 /* #define UNBUFFERED *//* use unbuffered output */
74
75 #include <sys/param.h>
76 #include <sys/stat.h>
77 #include <ctype.h>
78 #include <dirent.h>
79 #include <err.h>
80 #include <errno.h>
81 #include <fcntl.h>
82 #include <locale.h>
83 #include <pwd.h>
84 #include <setjmp.h>
85 #include <termcap.h>
86 #include <termios.h>
87 #include <signal.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <time.h>
92 #include <unistd.h>
93 #include "pathnames.h"
94
95 #define CMODE   0644            /* bounds file creation mode */
96 #define NO      0
97 #define YES     1
98 #define SUPERUSER       0       /* superuser uid */
99 #define DAEMON          1       /* daemon uid */
100 #define NLINES  24              /* default number of lines/crt screen */
101 #define NDAYS   21              /* default keep time for messages */
102 #define DAYS    *24*60*60       /* seconds/day */
103 #define MSGSRC  ".msgsrc"       /* user's rc file */
104 #define BOUNDS  "bounds"        /* message bounds file */
105 #define NEXT    "Next message? [yq]"
106 #define MORE    "More? [ynq]"
107 #define NOMORE  "(No more) [q] ?"
108
109 typedef char    bool;
110
111 FILE    *msgsrc;
112 FILE    *newmsg;
113 char    *sep = "-";
114 char    inbuf[BUFSIZ];
115 char    fname[MAXPATHLEN];
116 char    cmdbuf[MAXPATHLEN + MAXPATHLEN];
117 char    subj[128];
118 char    from[128];
119 char    date[128];
120 char    *ptr;
121 char    *in;
122 bool    local;
123 bool    ruptible;
124 bool    totty;
125 bool    seenfrom;
126 bool    seensubj;
127 bool    blankline;
128 bool    printing = NO;
129 bool    mailing = NO;
130 bool    quitit = NO;
131 bool    sending = NO;
132 bool    intrpflg = NO;
133 int     uid;
134 int     msg;
135 int     prevmsg;
136 int     lct;
137 int     nlines;
138 int     Lpp = 0;
139 time_t  t;
140 time_t  keep;
141
142 /* option initialization */
143 bool    hdrs = NO;
144 bool    qopt = NO;
145 bool    hush = NO;
146 bool    send_msg = NO;
147 bool    locomode = NO;
148 bool    use_pager = NO;
149 bool    clean = NO;
150 bool    lastcmd = NO;
151 jmp_buf tstpbuf;
152
153
154 void ask __P((char *));
155 void gfrsub __P((FILE *));
156 int linecnt __P((FILE *));
157 int next __P((char *));
158 char *nxtfld __P((unsigned char *));
159 void onsusp __P((int));
160 void onintr __P((int));
161 void prmesg __P((int));
162 static void usage __P((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                                         fprintf(stderr, "Sorry\n");
205                                         exit(1);
206                                 }
207                                 clean = YES;
208                                 break;
209
210                         case 'f':               /* silently */
211                                 hush = YES;
212                                 break;
213
214                         case 'h':               /* headers only */
215                                 hdrs = YES;
216                                 break;
217
218                         case 'l':               /* local msgs only */
219                                 locomode = YES;
220                                 break;
221
222                         case 'o':               /* option to save last message */
223                                 lastcmd = YES;
224                                 break;
225
226                         case 'p':               /* pipe thru 'more' during long msgs */
227                                 use_pager = YES;
228                                 break;
229
230                         case 'q':               /* query only */
231                                 qopt = YES;
232                                 break;
233
234                         case 's':               /* sending TO msgs */
235                                 send_msg = YES;
236                                 break;
237
238                         default:
239                                 usage();
240                         }
241                 }
242                 argc--, argv++;
243         }
244
245         /*
246          * determine current message bounds
247          */
248         snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
249
250         /*
251          * Test access rights to the bounds file
252          * This can be a little tricky.  if(send_msg), then
253          * we will create it.  We assume that if(send_msg),     
254          * then you have write permission there.
255          * Else, it better be there, or we bail.
256          */
257         if (send_msg != YES) {
258                 if (stat(fname, &buf) < 0) {
259                         if (hush != YES) {
260                                 err(errno, "%s", fname);
261                         } else {
262                                 exit(1);
263                         }
264                 }
265         }
266         bounds = fopen(fname, "r");
267
268         if (bounds != NULL) {
269                 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
270                 fclose(bounds);
271                 blast = lastmsg;        /* save upper bound */
272         }
273
274         if (clean)
275                 keep = t - (rcback? rcback : NDAYS) DAYS;
276
277         if (clean || bounds == NULL) {  /* relocate message bounds */
278                 struct dirent *dp;
279                 struct stat stbuf;
280                 bool seenany = NO;
281                 DIR     *dirp;
282
283                 dirp = opendir(_PATH_MSGS);
284                 if (dirp == NULL)
285                         err(errno, "%s", _PATH_MSGS);
286
287                 firstmsg = 32767;
288                 lastmsg = 0;
289
290                 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
291                         register char *cp = dp->d_name;
292                         register int i = 0;
293
294                         if (dp->d_ino == 0)
295                                 continue;
296                         if (dp->d_namlen == 0)
297                                 continue;
298
299                         if (clean)
300                                 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
301
302                         while (isdigit(*cp))
303                                 i = i * 10 + *cp++ - '0';
304                         if (*cp)
305                                 continue;       /* not a message! */
306
307                         if (clean) {
308                                 if (stat(inbuf, &stbuf) != 0)
309                                         continue;
310                                 if (stbuf.st_mtime < keep
311                                     && stbuf.st_mode&S_IWRITE) {
312                                         unlink(inbuf);
313                                         continue;
314                                 }
315                         }
316
317                         if (i > lastmsg)
318                                 lastmsg = i;
319                         if (i < firstmsg)
320                                 firstmsg = i;
321                         seenany = YES;
322                 }
323                 closedir(dirp);
324
325                 if (!seenany) {
326                         if (blast != 0) /* never lower the upper bound! */
327                                 lastmsg = blast;
328                         firstmsg = lastmsg + 1;
329                 }
330                 else if (blast > lastmsg)
331                         lastmsg = blast;
332
333                 if (!send_msg) {
334                         bounds = fopen(fname, "w");
335                         if (bounds == NULL)
336                                 err(errno, "%s", fname);
337                         chmod(fname, CMODE);
338                         fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
339                         fclose(bounds);
340                 }
341         }
342
343         if (send_msg) {
344                 /*
345                  * Send mode - place msgs in _PATH_MSGS
346                  */
347                 bounds = fopen(fname, "w");
348                 if (bounds == NULL)
349                         err(errno, "%s", fname);
350
351                 nextmsg = lastmsg + 1;
352                 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
353                 newmsg = fopen(fname, "w");
354                 if (newmsg == NULL)
355                         err(errno, "%s", fname);
356                 chmod(fname, CMODE);
357
358                 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
359                 fclose(bounds);
360
361                 sending = YES;
362                 if (ruptible)
363                         signal(SIGINT, onintr);
364
365                 if (isatty(fileno(stdin))) {
366                         ptr = getpwuid(uid)->pw_name;
367                         printf("Message %d:\nFrom %s %sSubject: ",
368                                 nextmsg, ptr, ctime(&t));
369                         fflush(stdout);
370                         fgets(inbuf, sizeof inbuf, stdin);
371                         putchar('\n');
372                         fflush(stdout);
373                         fprintf(newmsg, "From %s %sSubject: %s\n",
374                                 ptr, ctime(&t), inbuf);
375                         blankline = seensubj = YES;
376                 }
377                 else
378                         blankline = seensubj = NO;
379                 for (;;) {
380                         fgets(inbuf, sizeof inbuf, stdin);
381                         if (feof(stdin) || ferror(stdin))
382                                 break;
383                         blankline = (blankline || (inbuf[0] == '\n'));
384                         seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
385                         fputs(inbuf, newmsg);
386                 }
387 #ifdef OBJECT
388                 if (!seensubj) {
389                         printf("NOTICE: Messages should have a Subject field!\n");
390 #ifdef REJECT
391                         unlink(fname);
392 #endif
393                         exit(1);
394                 }
395 #endif
396                 exit(ferror(stdin));
397         }
398         if (clean)
399                 exit(0);
400
401         /*
402          * prepare to display messages
403          */
404         totty = (isatty(fileno(stdout)) != 0);
405         use_pager = use_pager && totty;
406
407         snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
408         msgsrc = fopen(fname, "r");
409         if (msgsrc) {
410                 newrc = NO;
411                 fscanf(msgsrc, "%d\n", &nextmsg);
412                 fclose(msgsrc);
413                 if (nextmsg > lastmsg+1) {
414                         printf("Warning: bounds have been reset (%d, %d)\n",
415                                 firstmsg, lastmsg);
416                         truncate(fname, (off_t)0);
417                         newrc = YES;
418                 }
419                 else if (!rcfirst)
420                         rcfirst = nextmsg - rcback;
421         }
422         else
423                 newrc = YES;
424         msgsrc = fopen(fname, "r+");
425         if (msgsrc == NULL)
426                 msgsrc = fopen(fname, "w");
427         if (msgsrc == NULL)
428                 err(errno, "%s", fname);
429         if (rcfirst) {
430                 if (rcfirst > lastmsg+1) {
431                         printf("Warning: the last message is number %d.\n",
432                                 lastmsg);
433                         rcfirst = nextmsg;
434                 }
435                 if (rcfirst > firstmsg)
436                         firstmsg = rcfirst;     /* don't set below first msg */
437         }
438         if (newrc) {
439                 nextmsg = firstmsg;
440                 fseek(msgsrc, 0L, 0);
441                 fprintf(msgsrc, "%d\n", nextmsg);
442                 fflush(msgsrc);
443         }
444
445 #ifdef V7
446         if (totty) {
447                 struct winsize win;
448                 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
449                         Lpp = win.ws_row;
450                 if (Lpp <= 0) {
451                         if (tgetent(inbuf, getenv("TERM")) <= 0
452                             || (Lpp = tgetnum("li")) <= 0) {
453                                 Lpp = NLINES;
454                         }
455                 }
456         }
457 #endif
458         Lpp -= 6;       /* for headers, etc. */
459
460         already = NO;
461         prevmsg = firstmsg;
462         printing = YES;
463         if (ruptible)
464                 signal(SIGINT, onintr);
465
466         /*
467          * Main program loop
468          */
469         for (msg = firstmsg; msg <= lastmsg; msg++) {
470
471                 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
472                 newmsg = fopen(fname, "r");
473                 if (newmsg == NULL)
474                         continue;
475
476                 gfrsub(newmsg);         /* get From and Subject fields */
477                 if (locomode && !local) {
478                         fclose(newmsg);
479                         continue;
480                 }
481
482                 if (qopt) {     /* This has to be located here */
483                         printf("There are new messages.\n");
484                         exit(0);
485                 }
486
487                 if (already && !hdrs)
488                         putchar('\n');
489
490                 /*
491                  * Print header
492                  */
493                 if (totty)
494                         signal(SIGTSTP, onsusp);
495                 (void) setjmp(tstpbuf);
496                 already = YES;
497                 nlines = 2;
498                 if (seenfrom) {
499                         printf("Message %d:\nFrom %s %s", msg, from, date);
500                         nlines++;
501                 }
502                 if (seensubj) {
503                         printf("Subject: %s", subj);
504                         nlines++;
505                 }
506                 else {
507                         if (seenfrom) {
508                                 putchar('\n');
509                                 nlines++;
510                         }
511                         while (nlines < 6
512                             && fgets(inbuf, sizeof inbuf, newmsg)
513                             && inbuf[0] != '\n') {
514                                 fputs(inbuf, stdout);
515                                 nlines++;
516                         }
517                 }
518
519                 lct = linecnt(newmsg);
520                 if (lct)
521                         printf("(%d%slines) ", lct, seensubj? " " : " more ");
522
523                 if (hdrs) {
524                         printf("\n-----\n");
525                         fclose(newmsg);
526                         continue;
527                 }
528
529                 /*
530                  * Ask user for command
531                  */
532                 if (totty)
533                         ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
534                 else
535                         inbuf[0] = 'y';
536                 if (totty)
537                         signal(SIGTSTP, SIG_DFL);
538 cmnd:
539                 in = inbuf;
540                 switch (*in) {
541                         case 'x':
542                         case 'X':
543                                 exit(0);
544
545                         case 'q':
546                         case 'Q':
547                                 quitit = YES;
548                                 printf("--Postponed--\n");
549                                 exit(0);
550                                 /* intentional fall-thru */
551                         case 'n':
552                         case 'N':
553                                 if (msg >= nextmsg) sep = "Flushed";
554                                 prevmsg = msg;
555                                 break;
556
557                         case 'p':
558                         case 'P':
559                                 use_pager = (*in++ == 'p');
560                                 /* intentional fallthru */
561                         case '\n':
562                         case 'y':
563                         default:
564                                 if (*in == '-') {
565                                         msg = prevmsg-1;
566                                         sep = "replay";
567                                         break;
568                                 }
569                                 if (isdigit(*in)) {
570                                         msg = next(in);
571                                         sep = in;
572                                         break;
573                                 }
574
575                                 prmesg(nlines + lct + (seensubj? 1 : 0));
576                                 prevmsg = msg;
577
578                 }
579
580                 printf("--%s--\n", sep);
581                 sep = "-";
582                 if (msg >= nextmsg) {
583                         nextmsg = msg + 1;
584                         fseek(msgsrc, 0L, 0);
585                         fprintf(msgsrc, "%d\n", nextmsg);
586                         fflush(msgsrc);
587                 }
588                 if (newmsg)
589                         fclose(newmsg);
590                 if (quitit)
591                         break;
592         }
593
594         /*
595          * Make sure .rc file gets updated
596          */
597         if (--msg >= nextmsg) {
598                 nextmsg = msg + 1;
599                 fseek(msgsrc, 0L, 0);
600                 fprintf(msgsrc, "%d\n", nextmsg);
601                 fflush(msgsrc);
602         }
603         if (already && !quitit && lastcmd && totty) {
604                 /*
605                  * save or reply to last message?
606                  */
607                 msg = prevmsg;
608                 ask(NOMORE);
609                 if (inbuf[0] == '-' || isdigit(inbuf[0]))
610                         goto cmnd;
611         }
612         if (!(already || hush || qopt))
613                 printf("No new messages.\n");
614         exit(0);
615 }
616
617 static void
618 usage()
619 {
620         fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
621         exit(1);
622 }
623
624 void
625 prmesg(length)
626 int length;
627 {
628         FILE *outf;
629         char *env_pager;
630
631         if (use_pager && length > Lpp) {
632                 signal(SIGPIPE, SIG_IGN);
633                 signal(SIGQUIT, SIG_IGN);
634                 if ((env_pager = getenv("PAGER")) == NULL) {
635                         snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
636                 } else {
637                         snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
638                 }
639                 outf = popen(cmdbuf, "w");
640                 if (!outf)
641                         outf = stdout;
642                 else
643                         setbuf(outf, (char *)NULL);
644         }
645         else
646                 outf = stdout;
647
648         if (seensubj)
649                 putc('\n', outf);
650
651         while (fgets(inbuf, sizeof inbuf, newmsg)) {
652                 fputs(inbuf, outf);
653                 if (ferror(outf)) {
654                         clearerr(outf);
655                         break;
656                 }
657         }
658
659         if (outf != stdout) {
660                 pclose(outf);
661                 signal(SIGPIPE, SIG_DFL);
662                 signal(SIGQUIT, SIG_DFL);
663         }
664         else {
665                 fflush(stdout);
666         }
667
668         /* force wait on output */
669         tcdrain(fileno(stdout));
670 }
671
672 void
673 onintr(unused)
674         int unused;
675 {
676         signal(SIGINT, onintr);
677         if (mailing)
678                 unlink(fname);
679         if (sending) {
680                 unlink(fname);
681                 puts("--Killed--");
682                 exit(1);
683         }
684         if (printing) {
685                 putchar('\n');
686                 if (hdrs)
687                         exit(0);
688                 sep = "Interrupt";
689                 if (newmsg)
690                         fseek(newmsg, 0L, 2);
691                 intrpflg = YES;
692         }
693 }
694
695 /*
696  * We have just gotten a susp.  Suspend and prepare to resume.
697  */
698 void
699 onsusp(unused)
700         int unused;
701 {
702         signal(SIGTSTP, SIG_DFL);
703         sigsetmask(0);
704         kill(0, SIGTSTP);
705         signal(SIGTSTP, onsusp);
706         if (!mailing)
707                 longjmp(tstpbuf, 0);
708 }
709
710 int
711 linecnt(f)
712 FILE *f;
713 {
714         off_t oldpos = ftell(f);
715         int l = 0;
716         char lbuf[BUFSIZ];
717
718         while (fgets(lbuf, sizeof lbuf, f))
719                 l++;
720         clearerr(f);
721         fseek(f, oldpos, 0);
722         return (l);
723 }
724
725 int
726 next(buf)
727 char *buf;
728 {
729         int i;
730         sscanf(buf, "%d", &i);
731         sprintf(buf, "Goto %d", i);
732         return(--i);
733 }
734
735 void
736 ask(prompt)
737 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 = ftell(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                         fseek(newmsg, oldpos, 0);
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                 fseek(newmsg, oldpos, 0);       /* 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(infile)
824 FILE *infile;
825 {
826         off_t frompos;
827         int count;
828
829         seensubj = seenfrom = NO;
830         local = YES;
831         subj[0] = from[0] = date[0] = NULL;
832
833         /*
834          * Is this a normal message?
835          */
836         if (fgets(inbuf, sizeof inbuf, infile)) {
837                 if (strncmp(inbuf, "From", 4)==0) {
838                         /*
839                          * expected form starts with From
840                          */
841                         seenfrom = YES;
842                         frompos = ftell(infile);
843                         ptr = from;
844                         in = nxtfld(inbuf);
845                         if (*in) {
846                                 count = sizeof(from) - 1;
847                                 while (*in && *in > ' ' && count-- > 0) {
848                                         if (*in == ':' || *in == '@' ||
849                                             *in == '!')
850                                                 local = NO;
851                                         *ptr++ = *in++;
852                                 }
853                         }
854                         *ptr = NULL;
855                         if (*(in = nxtfld(in)))
856                                 strncpy(date, in, sizeof date);
857                         else {
858                                 date[0] = '\n';
859                                 date[1] = NULL;
860                         }
861                 }
862                 else {
863                         /*
864                          * not the expected form
865                          */
866                         fseek(infile, 0L, 0);
867                         return;
868                 }
869         }
870         else
871                 /*
872                  * empty file ?
873                  */
874                 return;
875
876         /*
877          * look for Subject line until EOF or a blank line
878          */
879         while (fgets(inbuf, sizeof inbuf, infile)
880             && !(blankline = (inbuf[0] == '\n'))) {
881                 /*
882                  * extract Subject line
883                  */
884                 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
885                         seensubj = YES;
886                         frompos = ftell(infile);
887                         strncpy(subj, nxtfld(inbuf), sizeof subj);
888                 }
889         }
890         if (!blankline)
891                 /*
892                  * ran into EOF
893                  */
894                 fseek(infile, frompos, 0);
895
896         if (!seensubj)
897                 /*
898                  * for possible use with Mail
899                  */
900                 strncpy(subj, "(No Subject)\n", sizeof subj);
901 }
902
903 char *
904 nxtfld(s)
905 unsigned char *s;
906 {
907         if (*s) while (*s && !isspace(*s)) s++;     /* skip over this field */
908         if (*s) while (*s && isspace(*s)) s++;    /* find start of next field */
909         return (s);
910 }