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