xref: /illumos-gate/usr/src/cmd/mail/pushlist.c (revision d2ec54f7875f7e05edd56195adbeb593c947763f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 #include "mail.h"
28 /*
29  * link new entry into list of headerlines encountered of this type.
30  * If contflg == TRUE, link this line to the end of the continuation lines
31  * for the headerline specified (head or tail of type hdrtype).
32  */
33 void pushlist(hdrtype, where, s, contflg)
34 register	int	hdrtype;
35 register	int	where;
36 register		char *s;
37 {
38 	static char pn[] = "pushlist";
39 	char		*p;
40 	struct	hdrs	*nhp, *ohp, *nextcont;
41 
42 	/* Keep track of total bytes added to message due to    */
43 	/* certain lines in case non-delivery                   */
44 	/* notification needs to be sent. (See also copylet())  */
45 	if (hdrtype == H_AFWDFROM) {
46 		affbytecnt += (strlen(s) + ((contflg == TRUE) ?
47 			1 :
48 			(strlen(header[H_AFWDFROM].tag) + 2)) );
49 		if (contflg == FALSE) {
50 			affcnt++;
51 		}
52 	}
53 	if (hdrtype == H_RECEIVED) {
54 		rcvbytecnt += (strlen(s) + ((contflg == TRUE) ?
55 			1 :
56 			(strlen(header[H_RECEIVED].tag) + 2)) );
57 	}
58 	if ((p = malloc(sizeof(struct hdrs))) == (char *)NULL) {
59 		errmsg(E_MEM,"malloc failed in pushlist()");
60 		done(1);
61 	}
62 	memset(p, 0, sizeof(struct hdrs));
63 
64 	ohp = (where == HEAD ? hdrlines[hdrtype].head : hdrlines[hdrtype].tail);
65 	nhp = (struct hdrs *)p;
66 
67 	(void) strlcpy(nhp->value, s, sizeof (nhp->value));
68 
69 	Dout(pn, 0, "hdrtype = %d/%s, contflg = %d, saved value = '%s'\n",
70 		hdrtype, header[hdrtype].tag, contflg, s);
71 
72 	if (contflg) {
73 		if (ohp == (struct hdrs *)NULL) {
74 			/* This shouldn't happen.....? */
75 			/* No headline of this type found so far. How */
76 			/* did we think this is a continuation of something? */
77 			if (debug > 0) {
78 				Dout(pn, 0, "H_CONT with no hdr yet\n");
79 				abort();
80 			}
81 			/* Throw it on the floor... (!) */
82 			/**/
83 			/* Subtract anything that might have been added above */
84 			if (hdrtype == H_AFWDFROM) {
85 			    affbytecnt -= (strlen(s) + ((contflg == TRUE) ?
86 				1 :
87 				(strlen(header[H_AFWDFROM].tag) + 2)) );
88 			}
89 			if (hdrtype == H_RECEIVED) {
90 			    rcvbytecnt -= (strlen(s) + ((contflg == TRUE) ?
91 				1 :
92 				(strlen(header[H_RECEIVED].tag) + 2)) );
93 			}
94 			free ((char *)nhp);
95 			return;
96 		}
97 		/* Since we ONLY walk down 'cont' chains, */
98 		/* we only need forward links */
99 		nextcont = ohp;
100 		while (nextcont->cont != (struct hdrs *)NULL) {
101 			nextcont = nextcont->cont;
102 		}
103 		/* Add this one to end of list... */
104 		nextcont->cont = nhp;
105 		return;
106 	}
107 
108 	/* link value from this header line to end of list for */
109 	/* all header lines of the same type */
110 
111 	if (ohp == (struct hdrs *)NULL) {
112 		/* Empty list so far. New element goes first */
113 		hdrlines[hdrtype].head = hdrlines[hdrtype].tail = nhp;
114 	} else {
115 		if (where == HEAD) {
116 			/* Add new element to head of list */
117 			nhp->next = ohp;
118 			hdrlines[hdrtype].head = ohp->prev = nhp;
119 		} else {
120 			/* Add new element to tail of list */
121 			nhp->prev = ohp;
122 			hdrlines[hdrtype].tail = ohp->next = nhp;
123 		}
124 	}
125 }
126