send.c revision 1.20 1 1.20 wiz /* $NetBSD: send.c,v 1.20 2002/03/06 17:36:44 wiz Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.4 deraadt * Copyright (c) 1980, 1993
5 1.4 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.7 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.6 christos #if 0
39 1.6 christos static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
40 1.6 christos #else
41 1.20 wiz __RCSID("$NetBSD: send.c,v 1.20 2002/03/06 17:36:44 wiz Exp $");
42 1.6 christos #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include "rcv.h"
46 1.4 deraadt #include "extern.h"
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Mail -- a mail program
50 1.1 cgd *
51 1.1 cgd * Mail to others.
52 1.1 cgd */
53 1.1 cgd
54 1.20 wiz extern char *tmpdir;
55 1.13 christos
56 1.1 cgd /*
57 1.1 cgd * Send message described by the passed pointer to the
58 1.1 cgd * passed output buffer. Return -1 on error.
59 1.1 cgd * Adjust the status: field if need be.
60 1.1 cgd * If doign is given, suppress ignored header fields.
61 1.1 cgd * prefix is a string to prepend to each output line.
62 1.1 cgd */
63 1.4 deraadt int
64 1.14 wiz sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
65 1.14 wiz char *prefix)
66 1.1 cgd {
67 1.15 wiz long len;
68 1.7 lukem FILE *ibuf;
69 1.1 cgd char line[LINESIZE];
70 1.15 wiz int isheadflag, infld, ignoring = 0, dostat, firstline;
71 1.7 lukem char *cp, *cp2;
72 1.7 lukem int c = 0;
73 1.1 cgd int length;
74 1.6 christos int prefixlen = 0;
75 1.1 cgd
76 1.1 cgd /*
77 1.1 cgd * Compute the prefix string, without trailing whitespace
78 1.1 cgd */
79 1.16 wiz if (prefix != NULL) {
80 1.1 cgd cp2 = 0;
81 1.1 cgd for (cp = prefix; *cp; cp++)
82 1.1 cgd if (*cp != ' ' && *cp != '\t')
83 1.1 cgd cp2 = cp;
84 1.1 cgd prefixlen = cp2 == 0 ? 0 : cp2 - prefix + 1;
85 1.1 cgd }
86 1.1 cgd ibuf = setinput(mp);
87 1.15 wiz len = mp->m_size;
88 1.15 wiz isheadflag = 1;
89 1.1 cgd dostat = doign == 0 || !isign("status", doign);
90 1.1 cgd infld = 0;
91 1.1 cgd firstline = 1;
92 1.1 cgd /*
93 1.1 cgd * Process headers first
94 1.1 cgd */
95 1.15 wiz while (len > 0 && isheadflag) {
96 1.1 cgd if (fgets(line, LINESIZE, ibuf) == NULL)
97 1.1 cgd break;
98 1.15 wiz len -= length = strlen(line);
99 1.1 cgd if (firstline) {
100 1.1 cgd /*
101 1.1 cgd * First line is the From line, so no headers
102 1.1 cgd * there to worry about
103 1.1 cgd */
104 1.1 cgd firstline = 0;
105 1.1 cgd ignoring = doign == ignoreall;
106 1.1 cgd } else if (line[0] == '\n') {
107 1.1 cgd /*
108 1.1 cgd * If line is blank, we've reached end of
109 1.1 cgd * headers, so force out status: field
110 1.1 cgd * and note that we are no longer in header
111 1.1 cgd * fields
112 1.1 cgd */
113 1.1 cgd if (dostat) {
114 1.1 cgd statusput(mp, obuf, prefix);
115 1.1 cgd dostat = 0;
116 1.1 cgd }
117 1.15 wiz isheadflag = 0;
118 1.1 cgd ignoring = doign == ignoreall;
119 1.1 cgd } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
120 1.1 cgd /*
121 1.1 cgd * If this line is a continuation (via space or tab)
122 1.1 cgd * of a previous header field, just echo it
123 1.1 cgd * (unless the field should be ignored).
124 1.1 cgd * In other words, nothing to do.
125 1.1 cgd */
126 1.1 cgd } else {
127 1.1 cgd /*
128 1.1 cgd * Pick up the header field if we have one.
129 1.1 cgd */
130 1.1 cgd for (cp = line; (c = *cp++) && c != ':' && !isspace(c);)
131 1.1 cgd ;
132 1.1 cgd cp2 = --cp;
133 1.10 christos while (isspace((unsigned char)*cp++))
134 1.1 cgd ;
135 1.1 cgd if (cp[-1] != ':') {
136 1.1 cgd /*
137 1.1 cgd * Not a header line, force out status:
138 1.1 cgd * This happens in uucp style mail where
139 1.1 cgd * there are no headers at all.
140 1.1 cgd */
141 1.1 cgd if (dostat) {
142 1.1 cgd statusput(mp, obuf, prefix);
143 1.1 cgd dostat = 0;
144 1.1 cgd }
145 1.1 cgd if (doign != ignoreall)
146 1.1 cgd /* add blank line */
147 1.18 wiz (void)putc('\n', obuf);
148 1.15 wiz isheadflag = 0;
149 1.1 cgd ignoring = 0;
150 1.1 cgd } else {
151 1.1 cgd /*
152 1.1 cgd * If it is an ignored field and
153 1.1 cgd * we care about such things, skip it.
154 1.1 cgd */
155 1.1 cgd *cp2 = 0; /* temporarily null terminate */
156 1.1 cgd if (doign && isign(line, doign))
157 1.1 cgd ignoring = 1;
158 1.1 cgd else if ((line[0] == 's' || line[0] == 'S') &&
159 1.1 cgd strcasecmp(line, "status") == 0) {
160 1.1 cgd /*
161 1.1 cgd * If the field is "status," go compute
162 1.1 cgd * and print the real Status: field
163 1.1 cgd */
164 1.1 cgd if (dostat) {
165 1.1 cgd statusput(mp, obuf, prefix);
166 1.1 cgd dostat = 0;
167 1.1 cgd }
168 1.1 cgd ignoring = 1;
169 1.1 cgd } else {
170 1.1 cgd ignoring = 0;
171 1.1 cgd *cp2 = c; /* restore */
172 1.1 cgd }
173 1.1 cgd infld = 1;
174 1.1 cgd }
175 1.1 cgd }
176 1.1 cgd if (!ignoring) {
177 1.1 cgd /*
178 1.1 cgd * Strip trailing whitespace from prefix
179 1.1 cgd * if line is blank.
180 1.1 cgd */
181 1.16 wiz if (prefix != NULL) {
182 1.1 cgd if (length > 1)
183 1.1 cgd fputs(prefix, obuf);
184 1.1 cgd else
185 1.18 wiz (void)fwrite(prefix, sizeof *prefix,
186 1.1 cgd prefixlen, obuf);
187 1.9 ross }
188 1.18 wiz (void)fwrite(line, sizeof *line, length, obuf);
189 1.1 cgd if (ferror(obuf))
190 1.1 cgd return -1;
191 1.1 cgd }
192 1.1 cgd }
193 1.1 cgd /*
194 1.1 cgd * Copy out message body
195 1.1 cgd */
196 1.1 cgd if (doign == ignoreall)
197 1.15 wiz len--; /* skip final blank line */
198 1.16 wiz if (prefix != NULL)
199 1.15 wiz while (len > 0) {
200 1.1 cgd if (fgets(line, LINESIZE, ibuf) == NULL) {
201 1.1 cgd c = 0;
202 1.1 cgd break;
203 1.1 cgd }
204 1.15 wiz len -= c = strlen(line);
205 1.1 cgd /*
206 1.1 cgd * Strip trailing whitespace from prefix
207 1.1 cgd * if line is blank.
208 1.1 cgd */
209 1.1 cgd if (c > 1)
210 1.1 cgd fputs(prefix, obuf);
211 1.1 cgd else
212 1.18 wiz (void)fwrite(prefix, sizeof *prefix,
213 1.1 cgd prefixlen, obuf);
214 1.18 wiz (void)fwrite(line, sizeof *line, c, obuf);
215 1.1 cgd if (ferror(obuf))
216 1.1 cgd return -1;
217 1.1 cgd }
218 1.1 cgd else
219 1.15 wiz while (len > 0) {
220 1.15 wiz c = len < LINESIZE ? len : LINESIZE;
221 1.1 cgd if ((c = fread(line, sizeof *line, c, ibuf)) <= 0)
222 1.1 cgd break;
223 1.15 wiz len -= c;
224 1.1 cgd if (fwrite(line, sizeof *line, c, obuf) != c)
225 1.1 cgd return -1;
226 1.1 cgd }
227 1.1 cgd if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
228 1.1 cgd /* no final blank line */
229 1.1 cgd if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
230 1.1 cgd return -1;
231 1.1 cgd return 0;
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd /*
235 1.1 cgd * Output a reasonable looking status field.
236 1.1 cgd */
237 1.4 deraadt void
238 1.14 wiz statusput(struct message *mp, FILE *obuf, char *prefix)
239 1.1 cgd {
240 1.1 cgd char statout[3];
241 1.7 lukem char *cp = statout;
242 1.1 cgd
243 1.1 cgd if (mp->m_flag & MREAD)
244 1.1 cgd *cp++ = 'R';
245 1.1 cgd if ((mp->m_flag & MNEW) == 0)
246 1.1 cgd *cp++ = 'O';
247 1.1 cgd *cp = 0;
248 1.1 cgd if (statout[0])
249 1.1 cgd fprintf(obuf, "%sStatus: %s\n",
250 1.16 wiz prefix == NULL ? "" : prefix, statout);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd /*
254 1.1 cgd * Interface between the argument list and the mail1 routine
255 1.1 cgd * which does all the dirty work.
256 1.1 cgd */
257 1.4 deraadt int
258 1.14 wiz mail(struct name *to, struct name *cc, struct name *bcc,
259 1.14 wiz struct name *smopts, char *subject)
260 1.1 cgd {
261 1.1 cgd struct header head;
262 1.1 cgd
263 1.1 cgd head.h_to = to;
264 1.1 cgd head.h_subject = subject;
265 1.1 cgd head.h_cc = cc;
266 1.1 cgd head.h_bcc = bcc;
267 1.1 cgd head.h_smopts = smopts;
268 1.1 cgd mail1(&head, 0);
269 1.1 cgd return(0);
270 1.1 cgd }
271 1.1 cgd
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Send mail to a bunch of user names. The interface is through
275 1.1 cgd * the mail routine below.
276 1.1 cgd */
277 1.4 deraadt int
278 1.14 wiz sendmail(void *v)
279 1.1 cgd {
280 1.6 christos char *str = v;
281 1.1 cgd struct header head;
282 1.1 cgd
283 1.1 cgd head.h_to = extract(str, GTO);
284 1.16 wiz head.h_subject = NULL;
285 1.17 wiz head.h_cc = NULL;
286 1.17 wiz head.h_bcc = NULL;
287 1.17 wiz head.h_smopts = NULL;
288 1.1 cgd mail1(&head, 0);
289 1.1 cgd return(0);
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd /*
293 1.1 cgd * Mail a message on standard input to the people indicated
294 1.1 cgd * in the passed header. (Internal interface).
295 1.1 cgd */
296 1.4 deraadt void
297 1.14 wiz mail1(struct header *hp, int printheaders)
298 1.1 cgd {
299 1.1 cgd char *cp;
300 1.1 cgd int pid;
301 1.1 cgd char **namelist;
302 1.1 cgd struct name *to;
303 1.1 cgd FILE *mtf;
304 1.1 cgd
305 1.1 cgd /*
306 1.1 cgd * Collect user's mail from standard input.
307 1.1 cgd * Get the result as mtf.
308 1.1 cgd */
309 1.1 cgd if ((mtf = collect(hp, printheaders)) == NULL)
310 1.1 cgd return;
311 1.16 wiz if (value("interactive") != NULL) {
312 1.16 wiz if (value("askcc") != NULL || value("askbcc") != NULL) {
313 1.16 wiz if (value("askcc") != NULL)
314 1.3 jtc grabh(hp, GCC);
315 1.16 wiz if (value("askbcc") != NULL)
316 1.3 jtc grabh(hp, GBCC);
317 1.3 jtc } else {
318 1.1 cgd printf("EOT\n");
319 1.18 wiz (void)fflush(stdout);
320 1.1 cgd }
321 1.9 ross }
322 1.9 ross if (fsize(mtf) == 0) {
323 1.16 wiz if (value("dontsendempty") != NULL)
324 1.12 christos goto out;
325 1.16 wiz if (hp->h_subject == NULL)
326 1.1 cgd printf("No message, no subject; hope that's ok\n");
327 1.1 cgd else
328 1.1 cgd printf("Null message body; hope that's ok\n");
329 1.9 ross }
330 1.1 cgd /*
331 1.1 cgd * Now, take the user names from the combined
332 1.1 cgd * to and cc lists and do all the alias
333 1.1 cgd * processing.
334 1.1 cgd */
335 1.1 cgd senderr = 0;
336 1.1 cgd to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
337 1.17 wiz if (to == NULL) {
338 1.1 cgd printf("No recipients specified\n");
339 1.1 cgd senderr++;
340 1.1 cgd }
341 1.1 cgd /*
342 1.1 cgd * Look through the recipient list for names with /'s
343 1.1 cgd * in them which we write to as files directly.
344 1.1 cgd */
345 1.1 cgd to = outof(to, mtf, hp);
346 1.1 cgd if (senderr)
347 1.1 cgd savedeadletter(mtf);
348 1.1 cgd to = elide(to);
349 1.1 cgd if (count(to) == 0)
350 1.1 cgd goto out;
351 1.1 cgd fixhead(hp, to);
352 1.1 cgd if ((mtf = infix(hp, mtf)) == NULL) {
353 1.1 cgd fprintf(stderr, ". . . message lost, sorry.\n");
354 1.1 cgd return;
355 1.1 cgd }
356 1.1 cgd namelist = unpack(cat(hp->h_smopts, to));
357 1.1 cgd if (debug) {
358 1.1 cgd char **t;
359 1.1 cgd
360 1.1 cgd printf("Sendmail arguments:");
361 1.16 wiz for (t = namelist; *t != NULL; t++)
362 1.1 cgd printf(" \"%s\"", *t);
363 1.1 cgd printf("\n");
364 1.1 cgd goto out;
365 1.1 cgd }
366 1.16 wiz if ((cp = value("record")) != NULL)
367 1.18 wiz (void)savemail(expand(cp), mtf);
368 1.1 cgd /*
369 1.1 cgd * Fork, set up the temporary mail file as standard
370 1.1 cgd * input for "mail", and exec with the user list we generated
371 1.1 cgd * far above.
372 1.1 cgd */
373 1.1 cgd pid = fork();
374 1.1 cgd if (pid == -1) {
375 1.19 wiz warn("fork");
376 1.1 cgd savedeadletter(mtf);
377 1.1 cgd goto out;
378 1.1 cgd }
379 1.1 cgd if (pid == 0) {
380 1.6 christos sigset_t nset;
381 1.6 christos sigemptyset(&nset);
382 1.6 christos sigaddset(&nset, SIGHUP);
383 1.6 christos sigaddset(&nset, SIGINT);
384 1.6 christos sigaddset(&nset, SIGQUIT);
385 1.6 christos sigaddset(&nset, SIGTSTP);
386 1.6 christos sigaddset(&nset, SIGTTIN);
387 1.6 christos sigaddset(&nset, SIGTTOU);
388 1.6 christos prepare_child(&nset, fileno(mtf), -1);
389 1.16 wiz if ((cp = value("sendmail")) != NULL)
390 1.1 cgd cp = expand(cp);
391 1.1 cgd else
392 1.1 cgd cp = _PATH_SENDMAIL;
393 1.1 cgd execv(cp, namelist);
394 1.19 wiz warn("%s", cp);
395 1.1 cgd _exit(1);
396 1.1 cgd }
397 1.16 wiz if (value("verbose") != NULL)
398 1.18 wiz (void)wait_child(pid);
399 1.1 cgd else
400 1.1 cgd free_child(pid);
401 1.1 cgd out:
402 1.18 wiz (void)Fclose(mtf);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd /*
406 1.1 cgd * Fix the header by glopping all of the expanded names from
407 1.1 cgd * the distribution list into the appropriate fields.
408 1.1 cgd */
409 1.4 deraadt void
410 1.14 wiz fixhead(struct header *hp, struct name *tolist)
411 1.1 cgd {
412 1.7 lukem struct name *np;
413 1.1 cgd
414 1.17 wiz hp->h_to = NULL;
415 1.17 wiz hp->h_cc = NULL;
416 1.17 wiz hp->h_bcc = NULL;
417 1.17 wiz for (np = tolist; np != NULL; np = np->n_flink) {
418 1.8 bad if (np->n_type & GDEL)
419 1.8 bad continue; /* Don't copy deleted addresses to the header */
420 1.1 cgd if ((np->n_type & GMASK) == GTO)
421 1.1 cgd hp->h_to =
422 1.1 cgd cat(hp->h_to, nalloc(np->n_name, np->n_type));
423 1.1 cgd else if ((np->n_type & GMASK) == GCC)
424 1.1 cgd hp->h_cc =
425 1.1 cgd cat(hp->h_cc, nalloc(np->n_name, np->n_type));
426 1.1 cgd else if ((np->n_type & GMASK) == GBCC)
427 1.1 cgd hp->h_bcc =
428 1.1 cgd cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
429 1.8 bad }
430 1.1 cgd }
431 1.1 cgd
432 1.1 cgd /*
433 1.1 cgd * Prepend a header in front of the collected stuff
434 1.1 cgd * and return the new file.
435 1.1 cgd */
436 1.1 cgd FILE *
437 1.14 wiz infix(struct header *hp, FILE *fi)
438 1.1 cgd {
439 1.7 lukem FILE *nfo, *nfi;
440 1.20 wiz int c, fd;
441 1.20 wiz char tempname[PATHSIZE];
442 1.1 cgd
443 1.20 wiz (void)snprintf(tempname, sizeof(tempname),
444 1.20 wiz "%s/mail.RsXXXXXXXXXX", tmpdir);
445 1.20 wiz if ((fd = mkstemp(tempname)) == -1 ||
446 1.20 wiz (nfo = Fdopen(fd, "w")) == NULL) {
447 1.20 wiz if (fd != -1)
448 1.20 wiz close(fd);
449 1.20 wiz warn("%s", tempname);
450 1.1 cgd return(fi);
451 1.1 cgd }
452 1.20 wiz if ((nfi = Fopen(tempname, "r")) == NULL) {
453 1.20 wiz warn("%s", tempname);
454 1.18 wiz (void)Fclose(nfo);
455 1.20 wiz (void)rm(tempname);
456 1.1 cgd return(fi);
457 1.1 cgd }
458 1.20 wiz (void)rm(tempname);
459 1.18 wiz (void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
460 1.1 cgd c = getc(fi);
461 1.1 cgd while (c != EOF) {
462 1.18 wiz (void)putc(c, nfo);
463 1.1 cgd c = getc(fi);
464 1.1 cgd }
465 1.1 cgd if (ferror(fi)) {
466 1.19 wiz warn("read");
467 1.1 cgd rewind(fi);
468 1.1 cgd return(fi);
469 1.1 cgd }
470 1.18 wiz (void)fflush(nfo);
471 1.1 cgd if (ferror(nfo)) {
472 1.20 wiz warn("%s", tempname);
473 1.18 wiz (void)Fclose(nfo);
474 1.18 wiz (void)Fclose(nfi);
475 1.1 cgd rewind(fi);
476 1.1 cgd return(fi);
477 1.1 cgd }
478 1.18 wiz (void)Fclose(nfo);
479 1.18 wiz (void)Fclose(fi);
480 1.1 cgd rewind(nfi);
481 1.1 cgd return(nfi);
482 1.1 cgd }
483 1.1 cgd
484 1.1 cgd /*
485 1.1 cgd * Dump the to, subject, cc header on the
486 1.1 cgd * passed file buffer.
487 1.1 cgd */
488 1.4 deraadt int
489 1.14 wiz puthead(struct header *hp, FILE *fo, int w)
490 1.1 cgd {
491 1.7 lukem int gotcha;
492 1.1 cgd
493 1.1 cgd gotcha = 0;
494 1.17 wiz if (hp->h_to != NULL && w & GTO)
495 1.1 cgd fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
496 1.16 wiz if (hp->h_subject != NULL && w & GSUBJECT)
497 1.1 cgd fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
498 1.17 wiz if (hp->h_cc != NULL && w & GCC)
499 1.1 cgd fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
500 1.17 wiz if (hp->h_bcc != NULL && w & GBCC)
501 1.1 cgd fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
502 1.1 cgd if (gotcha && w & GNL)
503 1.18 wiz (void)putc('\n', fo);
504 1.1 cgd return(0);
505 1.1 cgd }
506 1.1 cgd
507 1.1 cgd /*
508 1.1 cgd * Format the given header line to not exceed 72 characters.
509 1.1 cgd */
510 1.4 deraadt void
511 1.14 wiz fmt(char *str, struct name *np, FILE *fo, int comma)
512 1.1 cgd {
513 1.7 lukem int col, len;
514 1.1 cgd
515 1.1 cgd comma = comma ? 1 : 0;
516 1.1 cgd col = strlen(str);
517 1.1 cgd if (col)
518 1.1 cgd fputs(str, fo);
519 1.17 wiz for (; np != NULL; np = np->n_flink) {
520 1.17 wiz if (np->n_flink == NULL)
521 1.1 cgd comma = 0;
522 1.1 cgd len = strlen(np->n_name);
523 1.1 cgd col++; /* for the space */
524 1.1 cgd if (col + len + comma > 72 && col > 4) {
525 1.1 cgd fputs("\n ", fo);
526 1.1 cgd col = 4;
527 1.1 cgd } else
528 1.1 cgd putc(' ', fo);
529 1.1 cgd fputs(np->n_name, fo);
530 1.1 cgd if (comma)
531 1.1 cgd putc(',', fo);
532 1.1 cgd col += len + comma;
533 1.1 cgd }
534 1.1 cgd putc('\n', fo);
535 1.1 cgd }
536 1.1 cgd
537 1.1 cgd /*
538 1.1 cgd * Save the outgoing mail on the passed file.
539 1.1 cgd */
540 1.1 cgd
541 1.1 cgd /*ARGSUSED*/
542 1.4 deraadt int
543 1.14 wiz savemail(char name[], FILE *fi)
544 1.1 cgd {
545 1.7 lukem FILE *fo;
546 1.1 cgd char buf[BUFSIZ];
547 1.7 lukem int i;
548 1.6 christos time_t now;
549 1.1 cgd
550 1.1 cgd if ((fo = Fopen(name, "a")) == NULL) {
551 1.19 wiz warn("%s", name);
552 1.1 cgd return (-1);
553 1.1 cgd }
554 1.18 wiz (void)time(&now);
555 1.1 cgd fprintf(fo, "From %s %s", myname, ctime(&now));
556 1.1 cgd while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
557 1.18 wiz (void)fwrite(buf, 1, i, fo);
558 1.18 wiz (void)putc('\n', fo);
559 1.18 wiz (void)fflush(fo);
560 1.1 cgd if (ferror(fo))
561 1.19 wiz warn("%s", name);
562 1.18 wiz (void)Fclose(fo);
563 1.1 cgd rewind(fi);
564 1.1 cgd return (0);
565 1.1 cgd }
566