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