xref: /illumos-gate/usr/src/cmd/mail/add_recip.c (revision 08855964b9970604433f7b19dcd71cf5af5e5f14)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31     NAME
32 	add_recip, madd_recip - add recipients to recipient list
33 
34     SYNOPSIS
35 	int add_recip(reciplist *plist, char *name, int checkdups)
36 	void madd_recip(reciplist *plist, char *name, int checkdups)
37 
38     DESCRIPTION
39 	add_recip() adds the name to the recipient linked list.
40 	If checkdups is set, it first checks to make certain that
41 	the name is not in the list.
42 
43 	madd_recips() is given a list of names separated by white
44 	space. Each name is split off and passed to add_recips.
45 */
46 
47 #include "mail.h"
48 
49 int
50 add_recip(reciplist *plist, char *name, int checkdups)
51 {
52 	char		*p;
53 	static char	pn[] = "add_recip";
54 	recip		*r = &plist->recip_list;
55 
56 	if ((name == (char *)NULL) || (*name == '\0')) {
57 		Tout(pn, "translation to NULL name ignored\n");
58 		return(0);
59 	}
60 
61 	p = name;
62 	while (*p && !isspace(*p)) {
63 		p++;
64 	}
65 	if (*p != '\0') {
66 	    Tout(pn, "'%s' not added due to imbedded spaces\n", name);
67 	    return(0);
68 	}
69 
70 	if (checkdups == TRUE) {
71 	    while (r->next != (struct recip *)NULL) {
72 		r = r->next;
73 		if (strcmp(r->name, name) == 0) {
74 			Tout(pn, "duplicate recipient '%s' not added to list\n",
75 									name);
76 			return(0);
77 		}
78 	    }
79 	}
80 
81 	if ((p = malloc (sizeof(struct recip))) == (char *)NULL) {
82 		errmsg(E_MEM,"first malloc failed in add_recip()");
83 		done(1);
84 	}
85 	plist->last_recip->next = (struct recip *)p;
86 	r = plist->last_recip = plist->last_recip->next;
87 	if ((r->name = malloc (strlen(name)+1)) == (char *)NULL) {
88 		errmsg(E_MEM,"second malloc failed in add_recip()");
89 		done(1);
90 	}
91 	strcpy (r->name, name);
92 	r->next = (struct recip *)NULL;
93 	Tout(pn, "'%s' added to recipient list\n", name);
94 
95 	return(1);
96 }
97 
98 void
99 madd_recip(reciplist *plist, char *namelist, int checkdups)
100 {
101 	char	*name;
102 	for (name = strtok(namelist, " \t"); name; name = strtok((char*)0, " \t"))
103 		add_recip(plist, name, checkdups);
104 }
105