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