xref: /illumos-gate/usr/src/cmd/mail/pushlist.c (revision d17be682)
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 #include "mail.h"
26 /*
27  * link new entry into list of headerlines encountered of this type.
28  * If contflg == TRUE, link this line to the end of the continuation lines
29  * for the headerline specified (head or tail of type hdrtype).
30  */
31 void pushlist(hdrtype, where, s, contflg)
32 register	int	hdrtype;
33 register	int	where;
34 register		char *s;
35 {
36 	static char pn[] = "pushlist";
37 	char		*p;
38 	struct	hdrs	*nhp, *ohp, *nextcont;
39 
40 	/* Keep track of total bytes added to message due to    */
41 	/* certain lines in case non-delivery                   */
42 	/* notification needs to be sent. (See also copylet())  */
43 	if (hdrtype == H_AFWDFROM) {
44 		affbytecnt += (strlen(s) + ((contflg == TRUE) ?
45 			1 :
46 			(strlen(header[H_AFWDFROM].tag) + 2)) );
47 		if (contflg == FALSE) {
48 			affcnt++;
49 		}
50 	}
51 	if (hdrtype == H_RECEIVED) {
52 		rcvbytecnt += (strlen(s) + ((contflg == TRUE) ?
53 			1 :
54 			(strlen(header[H_RECEIVED].tag) + 2)) );
55 	}
56 	if ((p = malloc(sizeof(struct hdrs))) == (char *)NULL) {
57 		errmsg(E_MEM,"malloc failed in pushlist()");
58 		done(1);
59 	}
60 	memset(p, 0, sizeof(struct hdrs));
61 
62 	ohp = (where == HEAD ? hdrlines[hdrtype].head : hdrlines[hdrtype].tail);
63 	nhp = (struct hdrs *)p;
64 
65 	(void) strlcpy(nhp->value, s, sizeof (nhp->value));
66 
67 	Dout(pn, 0, "hdrtype = %d/%s, contflg = %d, saved value = '%s'\n",
68 		hdrtype, header[hdrtype].tag, contflg, s);
69 
70 	if (contflg) {
71 		if (ohp == (struct hdrs *)NULL) {
72 			/* This shouldn't happen.....? */
73 			/* No headline of this type found so far. How */
74 			/* did we think this is a continuation of something? */
75 			if (debug > 0) {
76 				Dout(pn, 0, "H_CONT with no hdr yet\n");
77 				abort();
78 			}
79 			/* Throw it on the floor... (!) */
80 			/**/
81 			/* Subtract anything that might have been added above */
82 			if (hdrtype == H_AFWDFROM) {
83 			    affbytecnt -= (strlen(s) + ((contflg == TRUE) ?
84 				1 :
85 				(strlen(header[H_AFWDFROM].tag) + 2)) );
86 			}
87 			if (hdrtype == H_RECEIVED) {
88 			    rcvbytecnt -= (strlen(s) + ((contflg == TRUE) ?
89 				1 :
90 				(strlen(header[H_RECEIVED].tag) + 2)) );
91 			}
92 			free ((char *)nhp);
93 			return;
94 		}
95 		/* Since we ONLY walk down 'cont' chains, */
96 		/* we only need forward links */
97 		nextcont = ohp;
98 		while (nextcont->cont != (struct hdrs *)NULL) {
99 			nextcont = nextcont->cont;
100 		}
101 		/* Add this one to end of list... */
102 		nextcont->cont = nhp;
103 		return;
104 	}
105 
106 	/* link value from this header line to end of list for */
107 	/* all header lines of the same type */
108 
109 	if (ohp == (struct hdrs *)NULL) {
110 		/* Empty list so far. New element goes first */
111 		hdrlines[hdrtype].head = hdrlines[hdrtype].tail = nhp;
112 	} else {
113 		if (where == HEAD) {
114 			/* Add new element to head of list */
115 			nhp->next = ohp;
116 			hdrlines[hdrtype].head = ohp->prev = nhp;
117 		} else {
118 			/* Add new element to tail of list */
119 			nhp->prev = ohp;
120 			hdrlines[hdrtype].tail = ohp->next = nhp;
121 		}
122 	}
123 }
124