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