[bug #4302] errno is not always an extern int
[mmh] / uip / spost.c
1
2 /*
3  * spost.c -- feed messages to sendmail
4  *
5  * This is a simpler, faster, replacement for "post" for use
6  * when "sendmail" is the transport system.
7  *
8  * $Id$
9  *
10  * This code is Copyright (c) 2002, by the authors of nmh.  See the
11  * COPYRIGHT file in the root directory of the nmh distribution for
12  * complete copyright information.
13  */
14
15 #include <h/mh.h>
16 #include <signal.h>
17 #include <h/addrsbr.h>
18 #include <h/aliasbr.h>
19 #include <h/dropsbr.h>
20 #include <h/tws.h>
21 #include <h/mts.h>
22
23 #define uptolow(c)      ((isalpha(c) && isupper (c)) ? tolower (c) : c)
24
25 #define MAX_SM_FIELD    1476    /* < largest hdr field sendmail will accept */
26 #define FCCS            10      /* max number of fccs allowed */
27
28 struct swit switches[] = {
29 #define FILTSW               0
30     { "filter filterfile", 0 },
31 #define NFILTSW              1
32     { "nofilter", 0 },
33 #define FRMTSW               2
34     { "format", 0 },
35 #define NFRMTSW              3
36     { "noformat", 0 },
37 #define REMVSW               4
38     { "remove", 0 },
39 #define NREMVSW              5
40     { "noremove", 0 },
41 #define VERBSW               6
42     { "verbose", 0 },
43 #define NVERBSW              7
44     { "noverbose", 0 },
45 #define WATCSW               8
46     { "watch", 0 },
47 #define NWATCSW              9
48     { "nowatch", 0 },
49 #define BACKSW              10
50     { "backup", 0 },
51 #define NBACKSW             11
52     { "nobackup", 0 },
53 #define ALIASW              12
54     { "alias aliasfile", 0 },
55 #define NALIASW             13
56     { "noalias", 0 },
57 #define WIDTHSW             14
58     { "width columns", 0 },
59 #define VERSIONSW           15
60     { "version", 0 },
61 #define HELPSW              16
62     { "help", 0 },
63 #define DEBUGSW             17
64     { "debug", -5 },
65 #define DISTSW              18
66     { "dist", -4 },             /* interface from dist */
67 #define CHKSW               19
68     { "check", -5 },            /* interface from whom */
69 #define NCHKSW              20
70     { "nocheck", -7 },          /* interface from whom */
71 #define WHOMSW              21
72     { "whom", -4 },             /* interface from whom */
73 #define PUSHSW              22  /* fork to sendmail then exit */
74     { "push", -4 },
75 #define NPUSHSW             23  /* exec sendmail */
76     { "nopush", -6 },
77 #define LIBSW               24
78     { "library directory", -7 },
79 #define ANNOSW              25
80     { "idanno number", -6 },
81     { NULL, 0 }
82 };
83
84
85 /* flags for headers->flags */
86 #define HNOP    0x0000          /* just used to keep .set around */
87 #define HBAD    0x0001          /* bad header - don't let it through */
88 #define HADR    0x0002          /* header has an address field */
89 #define HSUB    0x0004          /* Subject: header */
90 #define HTRY    0x0008          /* try to send to addrs on header */
91 #define HBCC    0x0010          /* don't output this header */
92 #define HMNG    0x0020          /* mung this header */
93 #define HNGR    0x0040          /* no groups allowed in this header */
94 #define HFCC    0x0080          /* FCC: type header */
95 #define HNIL    0x0100          /* okay for this header not to have addrs */
96 #define HIGN    0x0200          /* ignore this header */
97
98 /* flags for headers->set */
99 #define MFRM    0x0001          /* we've seen a From: */
100 #define MDAT    0x0002          /* we've seen a Date: */
101 #define MRFM    0x0004          /* we've seen a Resent-From: */
102 #define MVIS    0x0008          /* we've seen sighted addrs */
103 #define MINV    0x0010          /* we've seen blind addrs */
104 #define MRDT    0x0020          /* we've seen a Resent-Date: */
105
106 struct headers {
107     char *value;
108     unsigned int flags;
109     unsigned int set;
110 };
111
112
113 static struct headers NHeaders[] = {
114     { "Return-Path", HBAD,                0 },
115     { "Received",    HBAD,                0 },
116     { "Reply-To",    HADR|HNGR,           0 },
117     { "From",        HADR|HNGR,           MFRM },
118     { "Sender",      HADR|HBAD,           0 },
119     { "Date",        HNOP,                MDAT },
120     { "Subject",     HSUB,                0 },
121     { "To",          HADR|HTRY,           MVIS },
122     { "cc",          HADR|HTRY,           MVIS },
123     { "Bcc",         HADR|HTRY|HBCC|HNIL, MINV },
124     { "Message-Id",  HBAD,                0 },
125     { "Fcc",         HFCC,                0 },
126     { NULL,          0,                   0 }
127 };
128
129 static struct headers RHeaders[] = {
130     { "Resent-Reply-To",   HADR|HNGR,      0 },
131     { "Resent-From",       HADR|HNGR,      MRFM },
132     { "Resent-Sender",     HADR|HBAD,      0 },
133     { "Resent-Date",       HNOP,           MRDT },
134     { "Resent-Subject",    HSUB,           0 },
135     { "Resent-To",         HADR|HTRY,      MVIS },
136     { "Resent-cc",         HADR|HTRY,      MVIS },
137     { "Resent-Bcc",        HADR|HTRY|HBCC, MINV },
138     { "Resent-Message-Id", HBAD,           0 },
139     { "Resent-Fcc",        HFCC,           0 },
140     { "Reply-To",          HADR,           0 },
141     { "Fcc",               HIGN,           0 },
142     { NULL,                0,              0 }
143 };
144
145
146 static short fccind = 0;        /* index into fccfold[] */
147
148 static int badmsg = 0;          /* message has bad semantics            */
149 static int verbose = 0;         /* spell it out                         */
150 static int debug = 0;           /* debugging post                       */
151 static int rmflg = 1;           /* remove temporary file when done      */
152 static int watch = 0;           /* watch the delivery process           */
153 static int backflg = 0;         /* rename input file as *.bak when done */
154 static int whomflg = 0;         /* if just checking addresses           */
155 static int pushflg = 0;         /* if going to fork to sendmail         */
156 static int aliasflg = -1;       /* if going to process aliases          */
157 static int outputlinelen=72;
158
159 static unsigned msgflags = 0;   /* what we've seen */
160
161 static enum {
162     normal, resent
163 } msgstate = normal;
164
165 static char tmpfil[] = "/tmp/pstXXXXXX";
166
167 static char from[BUFSIZ];       /* my network address */
168 static char signature[BUFSIZ];  /* my signature */
169 static char *filter = NULL;     /* the filter for BCC'ing */
170 static char *subject = NULL;    /* the subject field for BCC'ing */
171 static char *fccfold[FCCS];     /* foldernames for FCC'ing */
172
173 static struct headers *hdrtab;  /* table for the message we're doing */
174 static FILE *out;               /* output (temp) file */
175
176 extern char *sendmail;
177
178 /*
179  * external prototypes
180  */
181 extern char *getfullname (void);
182 extern char *getusername (void);
183
184 extern boolean  draft_from_masquerading;  /* defined in mts.c */
185
186 /*
187  * static prototypes
188  */
189 static void putfmt (char *, char *, FILE *);
190 static void start_headers (void);
191 static void finish_headers (FILE *);
192 static int get_header (char *, struct headers *);
193 static void putadr (char *, struct mailname *);
194 static int putone (char *, int, int);
195 static void insert_fcc (struct headers *, char *);
196 static void file (char *);
197 static void fcc (char *, char *);
198
199 #if 0
200 static void die (char *, char *, ...);
201 static void make_bcc_file (void);
202 #endif
203
204
205 int
206 main (int argc, char **argv)
207 {
208     int state, i, pid, compnum;
209     char *cp, *msg = NULL, **argp, **arguments;
210     char *sargv[16], buf[BUFSIZ], name[NAMESZ];
211     FILE *in;
212
213 #ifdef LOCALE
214     setlocale(LC_ALL, "");
215 #endif
216     invo_name = r1bindex (argv[0], '/');
217
218     /* foil search of user profile/context */
219     if (context_foil (NULL) == -1)
220         done (1);
221
222     mts_init (invo_name);
223     arguments = getarguments (invo_name, argc, argv, 0);
224     argp = arguments;
225
226     while ((cp = *argp++)) {
227         if (*cp == '-') {
228             switch (smatch (++cp, switches)) {
229                 case AMBIGSW: 
230                     ambigsw (cp, switches);
231                     done (1);
232                 case UNKWNSW: 
233                     adios (NULL, "-%s unknown", cp);
234
235                 case HELPSW: 
236                     snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
237                     print_help (buf, switches, 1);
238                     done (1);
239                 case VERSIONSW:
240                     print_version(invo_name);
241                     done (1);
242
243                 case DEBUGSW: 
244                     debug++;
245                     continue;
246
247                 case DISTSW:
248                     msgstate = resent;
249                     continue;
250
251                 case WHOMSW:
252                     whomflg++;
253                     continue;
254
255                 case FILTSW:
256                     if (!(filter = *argp++) || *filter == '-')
257                         adios (NULL, "missing argument to %s", argp[-2]);
258                     continue;
259                 case NFILTSW:
260                     filter = NULL;
261                     continue;
262                 
263                 case REMVSW: 
264                     rmflg++;
265                     continue;
266                 case NREMVSW: 
267                     rmflg = 0;
268                     continue;
269
270                 case BACKSW: 
271                     backflg++;
272                     continue;
273                 case NBACKSW: 
274                     backflg = 0;
275                     continue;
276
277                 case VERBSW: 
278                     verbose++;
279                     continue;
280                 case NVERBSW: 
281                     verbose = 0;
282                     continue;
283
284                 case WATCSW: 
285                     watch++;
286                     continue;
287                 case NWATCSW: 
288                     watch = 0;
289                     continue;
290                 
291                 case PUSHSW:
292                     pushflg++;
293                     continue;
294                 case NPUSHSW:
295                     pushflg = 0;
296                     continue;
297
298                 case ALIASW:
299                     if (!(cp = *argp++) || *cp == '-')
300                         adios (NULL, "missing argument to %s", argp[-2]);
301                     if (aliasflg < 0)
302                         alias (AliasFile);/* load default aka's */
303                     aliasflg = 1;
304                     if ((state = alias(cp)) != AK_OK)
305                         adios (NULL, "aliasing error in file %s - %s",
306                                cp, akerror(state) );
307                     continue;
308                 case NALIASW:
309                     aliasflg = 0;
310                     continue;
311
312                 case WIDTHSW:
313                     if (!(cp = *argp++) || *cp == '-')
314                         adios (NULL, "missing argument to %s", argp[-2]);
315                     outputlinelen = atoi (cp);
316                     if (outputlinelen <= 10)
317                         outputlinelen = 72;
318                     continue;
319
320                 case LIBSW:
321                     if (!(cp = *argp++) || *cp == '-')
322                         adios (NULL, "missing argument to %s", argp[-2]);
323                     /* create a minimal context */
324                     if (context_foil (cp) == -1)
325                         done(1);
326                     continue;
327
328                 case ANNOSW:
329                     /* -idanno switch ignored */
330                     if (!(cp = *argp++) || *cp == '-')
331                         adios (NULL, "missing argument to %s", argp[-2]);
332                     continue;
333             }
334         }
335         if (msg)
336             adios (NULL, "only one message at a time!");
337         else
338             msg = cp;
339     }
340
341     if (aliasflg < 0)
342         alias (AliasFile);      /* load default aka's */
343
344     if (!msg)
345         adios (NULL, "usage: %s [switches] file", invo_name);
346
347     if ((in = fopen (msg, "r")) == NULL)
348         adios (msg, "unable to open");
349
350     start_headers ();
351     if (debug) {
352         verbose++;
353         out = stdout;
354     }
355     else {
356 #ifdef HAVE_MKSTEMP
357             if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
358                 adios (tmpfil, "unable to create");
359 #else
360             mktemp (tmpfil);
361             if ((out = fopen (tmpfil, "w")) == NULL)
362                 adios (tmpfil, "unable to create");
363             chmod (tmpfil, 0600);
364 #endif
365         }
366
367     hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
368
369     for (compnum = 1, state = FLD;;) {
370         switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
371             case FLD: 
372                 compnum++;
373                 putfmt (name, buf, out);
374                 continue;
375
376             case FLDPLUS: 
377                 compnum++;
378                 cp = add (buf, cp);
379                 while (state == FLDPLUS) {
380                     state = m_getfld (state, name, buf, sizeof(buf), in);
381                     cp = add (buf, cp);
382                 }
383                 putfmt (name, cp, out);
384                 free (cp);
385                 continue;
386
387             case BODY: 
388                 finish_headers (out);
389                 fprintf (out, "\n%s", buf);
390                 if(whomflg == 0)
391                     while (state == BODY) {
392                         state = m_getfld (state, name, buf, sizeof(buf), in);
393                         fputs (buf, out);
394                     }
395                 break;
396
397             case FILEEOF: 
398                 finish_headers (out);
399                 break;
400
401             case LENERR: 
402             case FMTERR: 
403                 adios (NULL, "message format error in component #%d",
404                         compnum);
405
406             default: 
407                 adios (NULL, "getfld() returned %d", state);
408         }
409         break;
410     }
411
412     fclose (in);
413     if (backflg && !whomflg) {
414         strncpy (buf, m_backup (msg), sizeof(buf));
415         if (rename (msg, buf) == NOTOK)
416             advise (buf, "unable to rename %s to", msg);
417     }
418
419     if (debug) {
420         done (0);
421     }
422     else
423         fclose (out);
424
425     file (tmpfil);
426
427     /*
428      * re-open the temp file, unlink it and exec sendmail, giving it
429      * the msg temp file as std in.
430      */
431     if ( freopen( tmpfil, "r", stdin) == NULL)
432         adios (tmpfil, "can't reopen for sendmail");
433     if (rmflg)
434         unlink (tmpfil);
435
436     argp = sargv;
437     *argp++ = "send-mail";
438     *argp++ = "-m";     /* send to me too */
439     *argp++ = "-t";     /* read msg for recipients */
440     *argp++ = "-i";     /* don't stop on "." */
441     if (whomflg)
442         *argp++ = "-bv";
443     if (watch || verbose)
444         *argp++ = "-v";
445     *argp = NULL;
446
447     if (pushflg && !(watch || verbose)) {
448         /* fork to a child to run sendmail */
449         for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
450             sleep(5);
451         switch (pid) {
452             case NOTOK:
453                 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
454                          invo_name, sendmail);
455                 exit(-1);
456             case OK:
457                 /* we're the child .. */
458                 break;
459             default:
460                 exit(0);
461         }
462     }
463     execv ( sendmail, sargv);
464     adios ( sendmail, "can't exec");
465     return 0;  /* dead code to satisfy the compiler */
466 }
467
468 /* DRAFT GENERATION */
469
470 static void
471 putfmt (char *name, char *str, FILE *out)
472 {
473     int i;
474     char *cp, *pp;
475     struct headers *hdr;
476
477     while (*str == ' ' || *str == '\t')
478         str++;
479
480     if ((i = get_header (name, hdrtab)) == NOTOK) {
481         fprintf (out, "%s: %s", name, str);
482         return;
483     }
484
485     hdr = &hdrtab[i];
486     if (hdr->flags & HIGN)
487         return;
488     if (hdr->flags & HBAD) {
489         advise (NULL, "illegal header line -- %s:", name);
490         badmsg++;
491         return;
492     }
493     msgflags |= hdr->set;
494
495     if (hdr->flags & HSUB)
496         subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
497
498     if (hdr->flags & HFCC) {
499         if ((cp = strrchr(str, '\n')))
500             *cp = 0;
501         for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
502             *cp++ = 0;
503             insert_fcc (hdr, pp);
504         }
505         insert_fcc (hdr, pp);
506         return;
507     }
508
509 #ifdef notdef
510     if (hdr->flags & HBCC) {
511         insert_bcc(str);
512         return;
513     }
514 #endif  /* notdef */
515
516     if (*str != '\n' && *str != '\0') {
517         if (aliasflg && hdr->flags & HTRY) {
518             /* this header contains address(es) that we have to do
519              * alias expansion on.  Because of the saved state in
520              * getname we have to put all the addresses into a list.
521              * We then let putadr munch on that list, possibly
522              * expanding aliases.
523              */
524             register struct mailname *f = 0;
525             register struct mailname *mp = 0;
526
527             while ((cp = getname(str))) {
528                 mp = getm( cp, NULL, 0, AD_HOST, NULL);
529                 if (f == 0) {
530                     f = mp;
531                     mp->m_next = mp;
532                 } else {
533                     mp->m_next = f->m_next;
534                     f->m_next = mp;
535                     f = mp;
536                 }
537             }
538             f = mp->m_next; mp->m_next = 0;
539             putadr( name, f );
540         } else {
541             /* The author(s) of spost decided that alias substitution wasn't
542                necessary for the non-HTRY headers.  Unfortunately, one of those
543                headers is "From:", and having alias substitution work on that is
544                extremely useful for someone with a lot of POP3 email accounts or
545                aliases.  post supports aliasing of "From:"...
546
547                Since "From:"-processing is incompletely implemented in this
548                unsupported and undocumented spost backend, I'm not going to take
549                the time to implement my new draft-From:-based email address
550                masquerading.  If I do ever implement it here, I'd almost
551                certainly want to implement "From:" line alias processing as
552                well.  -- Dan Harkless <dan-nmh@dilvish.speed.net> */
553             fprintf (out, "%s: %s", name, str );
554         }
555     }
556 }
557
558
559 static void
560 start_headers (void)
561 {
562     char *cp;
563     char sigbuf[BUFSIZ];
564
565     strncpy(from, getusername(), sizeof(from));
566
567     if ((cp = getfullname ()) && *cp) {
568         strncpy (sigbuf, cp, sizeof(sigbuf));
569         snprintf (signature, sizeof(signature), "%s <%s>", sigbuf,  from);
570     }
571     else
572         snprintf (signature, sizeof(signature), "%s",  from);
573 }
574
575
576 static void
577 finish_headers (FILE *out)
578 {
579     switch (msgstate) {
580         case normal: 
581             if (!(msgflags & MDAT))
582                 fprintf (out, "Date: %s\n", dtimenow (0));
583             
584             if (msgflags & MFRM) {
585                 /* There was already a From: in the draft.  Don't add one. */
586                 if (!draft_from_masquerading)
587                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
588                        so we'll reveal the user's actual account@thismachine
589                        address in a Sender: header (and use it as the envelope
590                        From: later). */
591                     fprintf (out, "Sender: %s\n", from);
592             }
593             else
594                 fprintf (out, "From: %s\n", signature);
595            
596 #ifdef notdef
597             if (!(msgflags & MVIS))
598                 fprintf (out, "Bcc: Blind Distribution List: ;\n");
599 #endif  /* notdef */
600             break;
601
602         case resent: 
603             if (!(msgflags & MRDT))
604                 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
605             if (msgflags & MRFM) {
606                 /* There was already a Resent-From: in draft.  Don't add one. */
607                 if (!draft_from_masquerading)
608                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
609                        so we'll reveal the user's actual account@thismachine
610                        address in a Sender: header (and use it as the envelope
611                        From: later). */
612                     fprintf (out, "Resent-Sender: %s\n", from);
613             }
614             else
615                 /* Construct a Resent-From: header. */
616                 fprintf (out, "Resent-From: %s\n", signature);
617 #ifdef notdef
618             if (!(msgflags & MVIS))
619                 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
620 #endif  /* notdef */
621             break;
622     }
623
624     if (badmsg)
625         adios (NULL, "re-format message and try again");
626 }
627
628
629 static int
630 get_header (char *header, struct headers *table)
631 {
632     struct headers *h;
633
634     for (h = table; h->value; h++)
635         if (!strcasecmp (header, h->value))
636             return (h - table);
637
638     return NOTOK;
639 }
640
641
642 /*
643  * output the address list for header "name".  The address list
644  * is a linked list of mailname structs.  "nl" points to the head
645  * of the list.  Alias substitution should be done on nl.
646  */
647 static void
648 putadr (char *name, struct mailname *nl)
649 {
650     register struct mailname *mp, *mp2;
651     register int linepos;
652     register char *cp;
653     int namelen;
654
655     fprintf (out, "%s: ", name);
656     namelen = strlen(name) + 2;
657     linepos = namelen;
658
659     for (mp = nl; mp; ) {
660         if (linepos > MAX_SM_FIELD) {
661                 fprintf (out, "\n%s: ", name);
662                 linepos = namelen;
663         }
664         if (mp->m_nohost) {
665             /* a local name - see if it's an alias */
666             cp = akvalue(mp->m_mbox);
667             if (cp == mp->m_mbox)
668                 /* wasn't an alias - use what the user typed */
669                 linepos = putone( mp->m_text, linepos, namelen );
670             else
671                 /* an alias - expand it */
672                 while ((cp = getname(cp))) {
673                     if (linepos > MAX_SM_FIELD) {
674                             fprintf (out, "\n%s: ", name);
675                             linepos = namelen;
676                     }
677                     mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
678                     if (akvisible()) {
679                         mp2->m_pers = getcpy(mp->m_mbox);
680                         linepos = putone( adrformat(mp2), linepos, namelen );
681                     } else {
682                         linepos = putone( mp2->m_text, linepos, namelen );
683                     }
684                     mnfree( mp2 );
685                 }
686         } else {
687             /* not a local name - use what the user typed */
688             linepos = putone( mp->m_text, linepos, namelen );
689         }
690         mp2 = mp;
691         mp = mp->m_next;
692         mnfree( mp2 );
693     }
694     putc( '\n', out );
695 }
696
697 static int
698 putone (char *adr, int pos, int indent)
699 {
700     register int len;
701     static int linepos;
702
703     len = strlen( adr );
704     if (pos == indent)
705         linepos = pos;
706     else if ( linepos+len > outputlinelen ) {
707         fprintf ( out, ",\n%*s", indent, "");
708         linepos = indent;
709         pos += indent + 2;
710     }
711     else {
712         fputs( ", ", out );
713         linepos += 2;
714         pos += 2;
715     }
716     fputs( adr, out );
717
718     linepos += len;
719     return (pos+len);
720 }
721
722
723 static void
724 insert_fcc (struct headers *hdr, char *pp)
725 {
726     char   *cp;
727
728     for (cp = pp; isspace (*cp); cp++)
729         continue;
730     for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
731         continue;
732     if (pp >= cp)
733         *++pp = 0;
734     if (*cp == 0)
735         return;
736
737     if (fccind >= FCCS)
738         adios (NULL, "too many %ss", hdr->value);
739     fccfold[fccind++] = getcpy (cp);
740 }
741
742 #if 0
743 /* BCC GENERATION */
744
745 static void
746 make_bcc_file (void)
747 {
748     pid_t child_id;
749     int fd, i, status;
750     char *vec[6];
751     FILE * in, *out;
752
753 #ifdef HAVE_MKSTEMP
754     fd = mkstemp(bccfil);
755     if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
756         adios (bccfil, "unable to create");
757 #else
758     mktemp (bccfil);
759     if ((out = fopen (bccfil, "w")) == NULL)
760         adios (bccfil, "unable to create");
761 #endif
762     chmod (bccfil, 0600);
763
764     fprintf (out, "Date: %s\n", dtimenow (0));
765     fprintf (out, "From: %s\n", signature);
766     if (subject)
767         fprintf (out, "Subject: %s", subject);
768     fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
769     fflush (out);
770
771     if (filter == NULL) {
772         if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
773             adios (NULL, "unable to re-open");
774         cpydgst (fd, fileno (out), tmpfil, bccfil);
775         close (fd);
776     }
777     else {
778         vec[0] = r1bindex (mhlproc, '/');
779
780         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
781             sleep (5);
782         switch (child_id) {
783             case NOTOK: 
784                 adios ("vfork", "unable to");
785
786             case OK: 
787                 dup2 (fileno (out), 1);
788
789                 i = 1;
790                 vec[i++] = "-forward";
791                 vec[i++] = "-form";
792                 vec[i++] = filter;
793                 vec[i++] = tmpfil;
794                 vec[i] = NULL;
795
796                 execvp (mhlproc, vec);
797                 adios (mhlproc, "unable to exec");
798
799             default: 
800                 if (status = pidwait(child_id, OK))
801                     admonish (NULL, "%s lost (status=0%o)", vec[0], status);
802                 break;
803         }
804     }
805
806     fseek (out, 0L, SEEK_END);
807     fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
808     fclose (out);
809 }
810 #endif  /* if 0 */
811
812 /* FCC INTERACTION */
813
814 static void
815 file (char *path)
816 {
817     int i;
818
819     if (fccind == 0)
820         return;
821
822     for (i = 0; i < fccind; i++)
823         if (whomflg)
824             printf ("Fcc: %s\n", fccfold[i]);
825         else
826             fcc (path, fccfold[i]);
827 }
828
829
830 static void
831 fcc (char *file, char *folder)
832 {
833     pid_t child_id;
834     int i, status;
835     char fold[BUFSIZ];
836
837     if (verbose)
838         printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
839     fflush (stdout);
840
841     for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
842         sleep (5);
843     switch (child_id) {
844         case NOTOK: 
845             if (!verbose)
846                 fprintf (stderr, "  %sFcc %s: ",
847                         msgstate == resent ? "Resent-" : "", folder);
848             fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
849             break;
850
851         case OK: 
852             snprintf (fold, sizeof(fold), "%s%s",
853                     *folder == '+' || *folder == '@' ? "" : "+", folder);
854             execlp (fileproc, r1bindex (fileproc, '/'),
855                     "-link", "-file", file, fold, NULL);
856             _exit (-1);
857
858         default: 
859             if ((status = pidwait(child_id, OK))) {
860                 if (!verbose)
861                     fprintf (stderr, "  %sFcc %s: ",
862                             msgstate == resent ? "Resent-" : "", folder);
863                 fprintf (verbose ? stdout : stderr,
864                         " errored (0%o)\n", status);
865             }
866     }
867
868     fflush (stdout);
869 }
870
871
872 #if 0
873
874 /*
875  * TERMINATION
876  */
877
878 static void
879 die (char *what, char *fmt, ...)
880 {
881     va_list ap;
882
883     va_start(ap, fmt);
884     advertise (what, NULL, fmt, ap);
885     va_end(ap);
886
887     done (1);
888 }
889 #endif