Add/update copyright notice in all source code files.
[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 /*
185  * static prototypes
186  */
187 static void putfmt (char *, char *, FILE *);
188 static void start_headers (void);
189 static void finish_headers (FILE *);
190 static int get_header (char *, struct headers *);
191 static void putadr (char *, struct mailname *);
192 static int putone (char *, int, int);
193 static void insert_fcc (struct headers *, char *);
194 static void file (char *);
195 static void fcc (char *, char *);
196
197 #if 0
198 static void die (char *, char *, ...);
199 static void make_bcc_file (void);
200 #endif
201
202
203 int
204 main (int argc, char **argv)
205 {
206     int state, i, pid, compnum;
207     char *cp, *msg = NULL, **argp, **arguments;
208     char *sargv[16], buf[BUFSIZ], name[NAMESZ];
209     FILE *in;
210
211 #ifdef LOCALE
212     setlocale(LC_ALL, "");
213 #endif
214     invo_name = r1bindex (argv[0], '/');
215
216     /* foil search of user profile/context */
217     if (context_foil (NULL) == -1)
218         done (1);
219
220     mts_init (invo_name);
221     arguments = getarguments (invo_name, argc, argv, 0);
222     argp = arguments;
223
224     while ((cp = *argp++)) {
225         if (*cp == '-') {
226             switch (smatch (++cp, switches)) {
227                 case AMBIGSW: 
228                     ambigsw (cp, switches);
229                     done (1);
230                 case UNKWNSW: 
231                     adios (NULL, "-%s unknown", cp);
232
233                 case HELPSW: 
234                     snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
235                     print_help (buf, switches, 1);
236                     done (1);
237                 case VERSIONSW:
238                     print_version(invo_name);
239                     done (1);
240
241                 case DEBUGSW: 
242                     debug++;
243                     continue;
244
245                 case DISTSW:
246                     msgstate = resent;
247                     continue;
248
249                 case WHOMSW:
250                     whomflg++;
251                     continue;
252
253                 case FILTSW:
254                     if (!(filter = *argp++) || *filter == '-')
255                         adios (NULL, "missing argument to %s", argp[-2]);
256                     continue;
257                 case NFILTSW:
258                     filter = NULL;
259                     continue;
260                 
261                 case REMVSW: 
262                     rmflg++;
263                     continue;
264                 case NREMVSW: 
265                     rmflg = 0;
266                     continue;
267
268                 case BACKSW: 
269                     backflg++;
270                     continue;
271                 case NBACKSW: 
272                     backflg = 0;
273                     continue;
274
275                 case VERBSW: 
276                     verbose++;
277                     continue;
278                 case NVERBSW: 
279                     verbose = 0;
280                     continue;
281
282                 case WATCSW: 
283                     watch++;
284                     continue;
285                 case NWATCSW: 
286                     watch = 0;
287                     continue;
288                 
289                 case PUSHSW:
290                     pushflg++;
291                     continue;
292                 case NPUSHSW:
293                     pushflg = 0;
294                     continue;
295
296                 case ALIASW:
297                     if (!(cp = *argp++) || *cp == '-')
298                         adios (NULL, "missing argument to %s", argp[-2]);
299                     if (aliasflg < 0)
300                         alias (AliasFile);/* load default aka's */
301                     aliasflg = 1;
302                     if ((state = alias(cp)) != AK_OK)
303                         adios (NULL, "aliasing error in file %s - %s",
304                                cp, akerror(state) );
305                     continue;
306                 case NALIASW:
307                     aliasflg = 0;
308                     continue;
309
310                 case WIDTHSW:
311                     if (!(cp = *argp++) || *cp == '-')
312                         adios (NULL, "missing argument to %s", argp[-2]);
313                     outputlinelen = atoi (cp);
314                     if (outputlinelen <= 10)
315                         outputlinelen = 72;
316                     continue;
317
318                 case LIBSW:
319                     if (!(cp = *argp++) || *cp == '-')
320                         adios (NULL, "missing argument to %s", argp[-2]);
321                     /* create a minimal context */
322                     if (context_foil (cp) == -1)
323                         done(1);
324                     continue;
325
326                 case ANNOSW:
327                     /* -idanno switch ignored */
328                     if (!(cp = *argp++) || *cp == '-')
329                         adios (NULL, "missing argument to %s", argp[-2]);
330                     continue;
331             }
332         }
333         if (msg)
334             adios (NULL, "only one message at a time!");
335         else
336             msg = cp;
337     }
338
339     if (aliasflg < 0)
340         alias (AliasFile);      /* load default aka's */
341
342     if (!msg)
343         adios (NULL, "usage: %s [switches] file", invo_name);
344
345     if ((in = fopen (msg, "r")) == NULL)
346         adios (msg, "unable to open");
347
348     start_headers ();
349     if (debug) {
350         verbose++;
351         out = stdout;
352     }
353     else {
354 #ifdef HAVE_MKSTEMP
355             if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
356                 adios (tmpfil, "unable to create");
357 #else
358             mktemp (tmpfil);
359             if ((out = fopen (tmpfil, "w")) == NULL)
360                 adios (tmpfil, "unable to create");
361             chmod (tmpfil, 0600);
362 #endif
363         }
364
365     hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
366
367     for (compnum = 1, state = FLD;;) {
368         switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
369             case FLD: 
370                 compnum++;
371                 putfmt (name, buf, out);
372                 continue;
373
374             case FLDPLUS: 
375                 compnum++;
376                 cp = add (buf, cp);
377                 while (state == FLDPLUS) {
378                     state = m_getfld (state, name, buf, sizeof(buf), in);
379                     cp = add (buf, cp);
380                 }
381                 putfmt (name, cp, out);
382                 free (cp);
383                 continue;
384
385             case BODY: 
386                 finish_headers (out);
387                 fprintf (out, "\n%s", buf);
388                 if(whomflg == 0)
389                     while (state == BODY) {
390                         state = m_getfld (state, name, buf, sizeof(buf), in);
391                         fputs (buf, out);
392                     }
393                 break;
394
395             case FILEEOF: 
396                 finish_headers (out);
397                 break;
398
399             case LENERR: 
400             case FMTERR: 
401                 adios (NULL, "message format error in component #%d",
402                         compnum);
403
404             default: 
405                 adios (NULL, "getfld() returned %d", state);
406         }
407         break;
408     }
409
410     fclose (in);
411     if (backflg && !whomflg) {
412         strncpy (buf, m_backup (msg), sizeof(buf));
413         if (rename (msg, buf) == NOTOK)
414             advise (buf, "unable to rename %s to", msg);
415     }
416
417     if (debug) {
418         done (0);
419     }
420     else
421         fclose (out);
422
423     file (tmpfil);
424
425     /*
426      * re-open the temp file, unlink it and exec sendmail, giving it
427      * the msg temp file as std in.
428      */
429     if ( freopen( tmpfil, "r", stdin) == NULL)
430         adios (tmpfil, "can't reopen for sendmail");
431     if (rmflg)
432         unlink (tmpfil);
433
434     argp = sargv;
435     *argp++ = "send-mail";
436     *argp++ = "-m";     /* send to me too */
437     *argp++ = "-t";     /* read msg for recipients */
438     *argp++ = "-i";     /* don't stop on "." */
439     if (whomflg)
440         *argp++ = "-bv";
441     if (watch || verbose)
442         *argp++ = "-v";
443     *argp = NULL;
444
445     if (pushflg && !(watch || verbose)) {
446         /* fork to a child to run sendmail */
447         for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
448             sleep(5);
449         switch (pid) {
450             case NOTOK:
451                 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
452                          invo_name, sendmail);
453                 exit(-1);
454             case OK:
455                 /* we're the child .. */
456                 break;
457             default:
458                 exit(0);
459         }
460     }
461     execv ( sendmail, sargv);
462     adios ( sendmail, "can't exec");
463     return 0;  /* dead code to satisfy the compiler */
464 }
465
466 /* DRAFT GENERATION */
467
468 static void
469 putfmt (char *name, char *str, FILE *out)
470 {
471     int i;
472     char *cp, *pp;
473     struct headers *hdr;
474
475     while (*str == ' ' || *str == '\t')
476         str++;
477
478     if ((i = get_header (name, hdrtab)) == NOTOK) {
479         fprintf (out, "%s: %s", name, str);
480         return;
481     }
482
483     hdr = &hdrtab[i];
484     if (hdr->flags & HIGN)
485         return;
486     if (hdr->flags & HBAD) {
487         advise (NULL, "illegal header line -- %s:", name);
488         badmsg++;
489         return;
490     }
491     msgflags |= hdr->set;
492
493     if (hdr->flags & HSUB)
494         subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
495
496     if (hdr->flags & HFCC) {
497         if ((cp = strrchr(str, '\n')))
498             *cp = 0;
499         for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
500             *cp++ = 0;
501             insert_fcc (hdr, pp);
502         }
503         insert_fcc (hdr, pp);
504         return;
505     }
506
507 #ifdef notdef
508     if (hdr->flags & HBCC) {
509         insert_bcc(str);
510         return;
511     }
512 #endif  /* notdef */
513
514     if (*str != '\n' && *str != '\0') {
515         if (aliasflg && hdr->flags & HTRY) {
516             /* this header contains address(es) that we have to do
517              * alias expansion on.  Because of the saved state in
518              * getname we have to put all the addresses into a list.
519              * We then let putadr munch on that list, possibly
520              * expanding aliases.
521              */
522             register struct mailname *f = 0;
523             register struct mailname *mp = 0;
524
525             while ((cp = getname(str))) {
526                 mp = getm( cp, NULL, 0, AD_HOST, NULL);
527                 if (f == 0) {
528                     f = mp;
529                     mp->m_next = mp;
530                 } else {
531                     mp->m_next = f->m_next;
532                     f->m_next = mp;
533                     f = mp;
534                 }
535             }
536             f = mp->m_next; mp->m_next = 0;
537             putadr( name, f );
538         } else {
539             /* The author(s) of spost decided that alias substitution wasn't
540                necessary for the non-HTRY headers.  Unfortunately, one of those
541                headers is "From:", and having alias substitution work on that is
542                extremely useful for someone with a lot of POP3 email accounts or
543                aliases.  post supports aliasing of "From:"...
544
545                Since "From:"-processing is incompletely implemented in this
546                unsupported and undocumented spost backend, I'm not going to take
547                the time to implement my new draft-From:-based email address
548                masquerading.  If I do ever implement it here, I'd almost
549                certainly want to implement "From:" line alias processing as
550                well.  -- Dan Harkless <dan-nmh@dilvish.speed.net> */
551             fprintf (out, "%s: %s", name, str );
552         }
553     }
554 }
555
556
557 static void
558 start_headers (void)
559 {
560     char *cp;
561     char sigbuf[BUFSIZ];
562
563     strncpy(from, getusername(), sizeof(from));
564
565     if ((cp = getfullname ()) && *cp) {
566         strncpy (sigbuf, cp, sizeof(sigbuf));
567         snprintf (signature, sizeof(signature), "%s <%s>", sigbuf,  from);
568     }
569     else
570         snprintf (signature, sizeof(signature), "%s",  from);
571 }
572
573
574 static void
575 finish_headers (FILE *out)
576 {
577     switch (msgstate) {
578         case normal: 
579             if (!(msgflags & MDAT))
580                 fprintf (out, "Date: %s\n", dtimenow (0));
581             if (msgflags & MFRM)
582                 fprintf (out, "Sender: %s\n", from);
583             else
584                 fprintf (out, "From: %s\n", signature);
585 #ifdef notdef
586             if (!(msgflags & MVIS))
587                 fprintf (out, "Bcc: Blind Distribution List: ;\n");
588 #endif  /* notdef */
589             break;
590
591         case resent: 
592             if (!(msgflags & MRDT))
593                 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
594             if (msgflags & MRFM)
595                 fprintf (out, "Resent-Sender: %s\n", from);
596             else
597                 fprintf (out, "Resent-From: %s\n", signature);
598 #ifdef notdef
599             if (!(msgflags & MVIS))
600                 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
601 #endif  /* notdef */
602             break;
603     }
604
605     if (badmsg)
606         adios (NULL, "re-format message and try again");
607 }
608
609
610 static int
611 get_header (char *header, struct headers *table)
612 {
613     struct headers *h;
614
615     for (h = table; h->value; h++)
616         if (!strcasecmp (header, h->value))
617             return (h - table);
618
619     return NOTOK;
620 }
621
622
623 /*
624  * output the address list for header "name".  The address list
625  * is a linked list of mailname structs.  "nl" points to the head
626  * of the list.  Alias substitution should be done on nl.
627  */
628 static void
629 putadr (char *name, struct mailname *nl)
630 {
631     register struct mailname *mp, *mp2;
632     register int linepos;
633     register char *cp;
634     int namelen;
635
636     fprintf (out, "%s: ", name);
637     namelen = strlen(name) + 2;
638     linepos = namelen;
639
640     for (mp = nl; mp; ) {
641         if (linepos > MAX_SM_FIELD) {
642                 fprintf (out, "\n%s: ", name);
643                 linepos = namelen;
644         }
645         if (mp->m_nohost) {
646             /* a local name - see if it's an alias */
647             cp = akvalue(mp->m_mbox);
648             if (cp == mp->m_mbox)
649                 /* wasn't an alias - use what the user typed */
650                 linepos = putone( mp->m_text, linepos, namelen );
651             else
652                 /* an alias - expand it */
653                 while ((cp = getname(cp))) {
654                     if (linepos > MAX_SM_FIELD) {
655                             fprintf (out, "\n%s: ", name);
656                             linepos = namelen;
657                     }
658                     mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
659                     if (akvisible()) {
660                         mp2->m_pers = getcpy(mp->m_mbox);
661                         linepos = putone( adrformat(mp2), linepos, namelen );
662                     } else {
663                         linepos = putone( mp2->m_text, linepos, namelen );
664                     }
665                     mnfree( mp2 );
666                 }
667         } else {
668             /* not a local name - use what the user typed */
669             linepos = putone( mp->m_text, linepos, namelen );
670         }
671         mp2 = mp;
672         mp = mp->m_next;
673         mnfree( mp2 );
674     }
675     putc( '\n', out );
676 }
677
678 static int
679 putone (char *adr, int pos, int indent)
680 {
681     register int len;
682     static int linepos;
683
684     len = strlen( adr );
685     if (pos == indent)
686         linepos = pos;
687     else if ( linepos+len > outputlinelen ) {
688         fprintf ( out, ",\n%*s", indent, "");
689         linepos = indent;
690         pos += indent + 2;
691     }
692     else {
693         fputs( ", ", out );
694         linepos += 2;
695         pos += 2;
696     }
697     fputs( adr, out );
698
699     linepos += len;
700     return (pos+len);
701 }
702
703
704 static void
705 insert_fcc (struct headers *hdr, char *pp)
706 {
707     char   *cp;
708
709     for (cp = pp; isspace (*cp); cp++)
710         continue;
711     for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
712         continue;
713     if (pp >= cp)
714         *++pp = 0;
715     if (*cp == 0)
716         return;
717
718     if (fccind >= FCCS)
719         adios (NULL, "too many %ss", hdr->value);
720     fccfold[fccind++] = getcpy (cp);
721 }
722
723 #if 0
724 /* BCC GENERATION */
725
726 static void
727 make_bcc_file (void)
728 {
729     pid_t child_id;
730     int fd, i, status;
731     char *vec[6];
732     FILE * in, *out;
733
734 #ifdef HAVE_MKSTEMP
735     fd = mkstemp(bccfil);
736     if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
737         adios (bccfil, "unable to create");
738 #else
739     mktemp (bccfil);
740     if ((out = fopen (bccfil, "w")) == NULL)
741         adios (bccfil, "unable to create");
742 #endif
743     chmod (bccfil, 0600);
744
745     fprintf (out, "Date: %s\n", dtimenow (0));
746     fprintf (out, "From: %s\n", signature);
747     if (subject)
748         fprintf (out, "Subject: %s", subject);
749     fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
750     fflush (out);
751
752     if (filter == NULL) {
753         if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
754             adios (NULL, "unable to re-open");
755         cpydgst (fd, fileno (out), tmpfil, bccfil);
756         close (fd);
757     }
758     else {
759         vec[0] = r1bindex (mhlproc, '/');
760
761         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
762             sleep (5);
763         switch (child_id) {
764             case NOTOK: 
765                 adios ("vfork", "unable to");
766
767             case OK: 
768                 dup2 (fileno (out), 1);
769
770                 i = 1;
771                 vec[i++] = "-forward";
772                 vec[i++] = "-form";
773                 vec[i++] = filter;
774                 vec[i++] = tmpfil;
775                 vec[i] = NULL;
776
777                 execvp (mhlproc, vec);
778                 adios (mhlproc, "unable to exec");
779
780             default: 
781                 if (status = pidwait(child_id, OK))
782                     admonish (NULL, "%s lost (status=0%o)", vec[0], status);
783                 break;
784         }
785     }
786
787     fseek (out, 0L, SEEK_END);
788     fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
789     fclose (out);
790 }
791 #endif  /* if 0 */
792
793 /* FCC INTERACTION */
794
795 static void
796 file (char *path)
797 {
798     int i;
799
800     if (fccind == 0)
801         return;
802
803     for (i = 0; i < fccind; i++)
804         if (whomflg)
805             printf ("Fcc: %s\n", fccfold[i]);
806         else
807             fcc (path, fccfold[i]);
808 }
809
810
811 static void
812 fcc (char *file, char *folder)
813 {
814     pid_t child_id;
815     int i, status;
816     char fold[BUFSIZ];
817
818     if (verbose)
819         printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
820     fflush (stdout);
821
822     for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
823         sleep (5);
824     switch (child_id) {
825         case NOTOK: 
826             if (!verbose)
827                 fprintf (stderr, "  %sFcc %s: ",
828                         msgstate == resent ? "Resent-" : "", folder);
829             fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
830             break;
831
832         case OK: 
833             snprintf (fold, sizeof(fold), "%s%s",
834                     *folder == '+' || *folder == '@' ? "" : "+", folder);
835             execlp (fileproc, r1bindex (fileproc, '/'),
836                     "-link", "-file", file, fold, NULL);
837             _exit (-1);
838
839         default: 
840             if ((status = pidwait(child_id, OK))) {
841                 if (!verbose)
842                     fprintf (stderr, "  %sFcc %s: ",
843                             msgstate == resent ? "Resent-" : "", folder);
844                 fprintf (verbose ? stdout : stderr,
845                         " errored (0%o)\n", status);
846             }
847     }
848
849     fflush (stdout);
850 }
851
852
853 #if 0
854
855 /*
856  * TERMINATION
857  */
858
859 static void
860 die (char *what, char *fmt, ...)
861 {
862     va_list ap;
863
864     va_start(ap, fmt);
865     advertise (what, NULL, fmt, ap);
866     va_end(ap);
867
868     done (1);
869 }
870 #endif