Home | History | Annotate | Line # | Download | only in mail
names.c revision 1.19.2.1
      1  1.19.2.1      chap /*	$NetBSD: names.c,v 1.19.2.1 2006/06/19 04:17:06 chap Exp $	*/
      2       1.5  christos 
      3       1.1       cgd /*
      4       1.3   deraadt  * Copyright (c) 1980, 1993
      5       1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32       1.6     lukem #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34       1.5  christos #if 0
     35       1.5  christos static char sccsid[] = "@(#)names.c	8.1 (Berkeley) 6/6/93";
     36       1.5  christos #else
     37  1.19.2.1      chap __RCSID("$NetBSD: names.c,v 1.19.2.1 2006/06/19 04:17:06 chap Exp $");
     38       1.5  christos #endif
     39       1.1       cgd #endif /* not lint */
     40       1.1       cgd 
     41       1.1       cgd /*
     42       1.1       cgd  * Mail -- a mail program
     43       1.1       cgd  *
     44       1.1       cgd  * Handle name lists.
     45       1.1       cgd  */
     46       1.1       cgd 
     47       1.1       cgd #include "rcv.h"
     48       1.3   deraadt #include "extern.h"
     49       1.1       cgd 
     50       1.9  christos 
     51       1.1       cgd /*
     52       1.1       cgd  * Allocate a single element of a name list,
     53       1.1       cgd  * initialize its name field to the passed
     54       1.1       cgd  * name and return it.
     55       1.1       cgd  */
     56       1.1       cgd struct name *
     57      1.10       wiz nalloc(char str[], int ntype)
     58       1.1       cgd {
     59       1.6     lukem 	struct name *np;
     60       1.1       cgd 
     61      1.18  christos 	np = salloc(sizeof *np);
     62      1.13       wiz 	np->n_flink = NULL;
     63      1.13       wiz 	np->n_blink = NULL;
     64       1.1       cgd 	np->n_type = ntype;
     65       1.1       cgd 	np->n_name = savestr(str);
     66       1.1       cgd 	return(np);
     67       1.1       cgd }
     68       1.1       cgd 
     69       1.1       cgd /*
     70       1.1       cgd  * Find the tail of a list and return it.
     71       1.1       cgd  */
     72       1.1       cgd struct name *
     73      1.10       wiz tailof(struct name *name)
     74       1.1       cgd {
     75       1.6     lukem 	struct name *np;
     76       1.1       cgd 
     77       1.1       cgd 	np = name;
     78      1.13       wiz 	if (np == NULL)
     79      1.13       wiz 		return(NULL);
     80      1.13       wiz 	while (np->n_flink != NULL)
     81       1.1       cgd 		np = np->n_flink;
     82       1.1       cgd 	return(np);
     83       1.1       cgd }
     84       1.1       cgd 
     85       1.1       cgd /*
     86       1.1       cgd  * Extract a list of names from a line,
     87       1.1       cgd  * and make a list of names from it.
     88      1.13       wiz  * Return the list or NULL if none found.
     89       1.1       cgd  */
     90       1.1       cgd struct name *
     91      1.10       wiz extract(char line[], int ntype)
     92       1.1       cgd {
     93       1.6     lukem 	char *cp;
     94      1.11       wiz 	struct name *begin, *np, *t;
     95       1.1       cgd 	char nbuf[BUFSIZ];
     96       1.1       cgd 
     97      1.12       wiz 	if (line == NULL || *line == '\0')
     98      1.13       wiz 		return NULL;
     99      1.13       wiz 	begin = NULL;
    100      1.13       wiz 	np = NULL;
    101       1.1       cgd 	cp = line;
    102      1.12       wiz 	while ((cp = yankword(cp, nbuf)) != NULL) {
    103       1.1       cgd 		t = nalloc(nbuf, ntype);
    104      1.13       wiz 		if (begin == NULL)
    105      1.11       wiz 			begin = t;
    106       1.1       cgd 		else
    107       1.1       cgd 			np->n_flink = t;
    108       1.1       cgd 		t->n_blink = np;
    109       1.1       cgd 		np = t;
    110       1.1       cgd 	}
    111      1.11       wiz 	return begin;
    112       1.1       cgd }
    113       1.1       cgd 
    114       1.1       cgd /*
    115       1.1       cgd  * Turn a list of names into a string of the same names.
    116       1.1       cgd  */
    117       1.1       cgd char *
    118      1.10       wiz detract(struct name *np, int ntype)
    119       1.1       cgd {
    120      1.19  christos 	size_t s;
    121      1.11       wiz 	char *cp, *begin;
    122       1.6     lukem 	struct name *p;
    123       1.6     lukem 	int comma;
    124       1.1       cgd 
    125       1.1       cgd 	comma = ntype & GCOMMA;
    126      1.13       wiz 	if (np == NULL)
    127      1.12       wiz 		return(NULL);
    128       1.1       cgd 	ntype &= ~GCOMMA;
    129       1.1       cgd 	s = 0;
    130       1.1       cgd 	if (debug && comma)
    131      1.19  christos 		(void)fprintf(stderr, "detract asked to insert commas\n");
    132      1.13       wiz 	for (p = np; p != NULL; p = p->n_flink) {
    133       1.1       cgd 		if (ntype && (p->n_type & GMASK) != ntype)
    134       1.1       cgd 			continue;
    135       1.1       cgd 		s += strlen(p->n_name) + 1;
    136       1.1       cgd 		if (comma)
    137       1.1       cgd 			s++;
    138       1.1       cgd 	}
    139       1.1       cgd 	if (s == 0)
    140      1.12       wiz 		return(NULL);
    141       1.1       cgd 	s += 2;
    142      1.11       wiz 	begin = salloc(s);
    143      1.11       wiz 	cp = begin;
    144      1.13       wiz 	for (p = np; p != NULL; p = p->n_flink) {
    145       1.1       cgd 		if (ntype && (p->n_type & GMASK) != ntype)
    146       1.1       cgd 			continue;
    147       1.1       cgd 		cp = copy(p->n_name, cp);
    148      1.13       wiz 		if (comma && p->n_flink != NULL)
    149       1.1       cgd 			*cp++ = ',';
    150       1.1       cgd 		*cp++ = ' ';
    151       1.1       cgd 	}
    152       1.1       cgd 	*--cp = 0;
    153       1.1       cgd 	if (comma && *--cp == ',')
    154       1.1       cgd 		*cp = 0;
    155      1.11       wiz 	return(begin);
    156       1.1       cgd }
    157       1.1       cgd 
    158       1.1       cgd /*
    159       1.1       cgd  * Grab a single word (liberal word)
    160       1.1       cgd  * Throw away things between ()'s, and take anything between <>.
    161       1.1       cgd  */
    162       1.1       cgd char *
    163      1.10       wiz yankword(char *ap, char wbuf[])
    164       1.1       cgd {
    165       1.6     lukem 	char *cp, *cp2;
    166       1.1       cgd 
    167       1.1       cgd 	cp = ap;
    168       1.1       cgd 	for (;;) {
    169       1.1       cgd 		if (*cp == '\0')
    170      1.12       wiz 			return NULL;
    171       1.1       cgd 		if (*cp == '(') {
    172       1.6     lukem 			int nesting = 0;
    173       1.1       cgd 
    174       1.1       cgd 			while (*cp != '\0') {
    175       1.1       cgd 				switch (*cp++) {
    176       1.1       cgd 				case '(':
    177       1.1       cgd 					nesting++;
    178       1.1       cgd 					break;
    179       1.1       cgd 				case ')':
    180       1.1       cgd 					--nesting;
    181       1.1       cgd 					break;
    182       1.1       cgd 				}
    183       1.1       cgd 				if (nesting <= 0)
    184       1.1       cgd 					break;
    185       1.1       cgd 			}
    186       1.1       cgd 		} else if (*cp == ' ' || *cp == '\t' || *cp == ',')
    187       1.1       cgd 			cp++;
    188       1.1       cgd 		else
    189       1.1       cgd 			break;
    190       1.1       cgd 	}
    191       1.1       cgd 	if (*cp ==  '<')
    192       1.1       cgd 		for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
    193       1.1       cgd 			;
    194       1.1       cgd 	else
    195       1.8  christos 		for (cp2 = wbuf; *cp && !strchr(" \t,(", *cp); *cp2++ = *cp++)
    196       1.1       cgd 			;
    197       1.1       cgd 	*cp2 = '\0';
    198       1.1       cgd 	return cp;
    199       1.1       cgd }
    200       1.1       cgd 
    201       1.1       cgd /*
    202       1.1       cgd  * For each recipient in the passed name list with a /
    203       1.1       cgd  * in the name, append the message to the end of the named file
    204       1.1       cgd  * and remove him from the recipient list.
    205       1.1       cgd  *
    206       1.1       cgd  * Recipients whose name begins with | are piped through the given
    207       1.1       cgd  * program and removed.
    208       1.1       cgd  */
    209       1.1       cgd struct name *
    210      1.10       wiz outof(struct name *names, FILE *fo, struct header *hp)
    211       1.1       cgd {
    212      1.16       wiz 	int c, fd;
    213      1.11       wiz 	struct name *np, *begin;
    214       1.5  christos 	time_t now;
    215      1.18  christos 	char *date;
    216      1.18  christos 	const char *fname;
    217       1.1       cgd 	FILE *fout, *fin;
    218       1.1       cgd 	int ispipe;
    219      1.16       wiz 	char tempname[PATHSIZE];
    220       1.1       cgd 
    221      1.11       wiz 	begin = names;
    222       1.1       cgd 	np = names;
    223      1.14       wiz 	(void)time(&now);
    224       1.1       cgd 	date = ctime(&now);
    225      1.13       wiz 	while (np != NULL) {
    226       1.1       cgd 		if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
    227       1.1       cgd 			np = np->n_flink;
    228       1.1       cgd 			continue;
    229       1.1       cgd 		}
    230       1.1       cgd 		ispipe = np->n_name[0] == '|';
    231       1.1       cgd 		if (ispipe)
    232       1.1       cgd 			fname = np->n_name+1;
    233       1.1       cgd 		else
    234       1.1       cgd 			fname = expand(np->n_name);
    235       1.1       cgd 
    236       1.1       cgd 		/*
    237       1.1       cgd 		 * See if we have copied the complete message out yet.
    238       1.1       cgd 		 * If not, do so.
    239       1.1       cgd 		 */
    240       1.1       cgd 
    241       1.1       cgd 		if (image < 0) {
    242      1.16       wiz 			(void)snprintf(tempname, sizeof(tempname),
    243      1.16       wiz 			    "%s/mail.ReXXXXXXXXXXXX", tmpdir);
    244      1.16       wiz 			if ((fd = mkstemp(tempname)) == -1 ||
    245      1.16       wiz 			    (fout = Fdopen(fd, "a")) == NULL) {
    246      1.16       wiz 				if (fd != -1)
    247      1.19  christos 					(void)close(fd);
    248      1.16       wiz 				warn("%s", tempname);
    249       1.1       cgd 				senderr++;
    250       1.1       cgd 				goto cant;
    251       1.1       cgd 			}
    252      1.16       wiz 			image = open(tempname, 2);
    253      1.16       wiz 			(void)unlink(tempname);
    254       1.1       cgd 			if (image < 0) {
    255      1.16       wiz 				warn("%s", tempname);
    256       1.1       cgd 				senderr++;
    257      1.14       wiz 				(void)Fclose(fout);
    258       1.1       cgd 				goto cant;
    259       1.1       cgd 			}
    260      1.14       wiz 			(void)fcntl(image, F_SETFD, 1);
    261      1.19  christos 			(void)fprintf(fout, "From %s %s", myname, date);
    262      1.19  christos 			(void)puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
    263       1.1       cgd 			while ((c = getc(fo)) != EOF)
    264      1.14       wiz 				(void)putc(c, fout);
    265       1.1       cgd 			rewind(fo);
    266      1.14       wiz 			(void)putc('\n', fout);
    267      1.14       wiz 			(void)fflush(fout);
    268       1.7       bad 			if (ferror(fout)) {
    269      1.16       wiz 				warn("%s", tempname);
    270       1.7       bad 				senderr++;
    271      1.14       wiz 				(void)Fclose(fout);
    272       1.7       bad 				goto cant;
    273       1.7       bad 			}
    274      1.14       wiz 			(void)Fclose(fout);
    275       1.1       cgd 		}
    276       1.1       cgd 
    277       1.1       cgd 		/*
    278       1.1       cgd 		 * Now either copy "image" to the desired file
    279       1.1       cgd 		 * or give it as the standard input to the desired
    280       1.1       cgd 		 * program as appropriate.
    281       1.1       cgd 		 */
    282       1.1       cgd 
    283       1.1       cgd 		if (ispipe) {
    284       1.1       cgd 			int pid;
    285      1.18  christos 			const char *shellcmd;
    286       1.5  christos 			sigset_t nset;
    287       1.1       cgd 
    288       1.1       cgd 			/*
    289       1.1       cgd 			 * XXX
    290       1.1       cgd 			 * We can't really reuse the same image file,
    291       1.1       cgd 			 * because multiple piped recipients will
    292       1.1       cgd 			 * share the same lseek location and trample
    293       1.1       cgd 			 * on one another.
    294       1.1       cgd 			 */
    295      1.12       wiz 			if ((shellcmd = value("SHELL")) == NULL)
    296      1.11       wiz 				shellcmd = _PATH_CSHELL;
    297      1.19  christos 			(void)sigemptyset(&nset);
    298      1.19  christos 			(void)sigaddset(&nset, SIGHUP);
    299      1.19  christos 			(void)sigaddset(&nset, SIGINT);
    300      1.19  christos 			(void)sigaddset(&nset, SIGQUIT);
    301      1.11       wiz 			pid = start_command(shellcmd, &nset,
    302      1.12       wiz 				image, -1, "-c", fname, NULL);
    303       1.1       cgd 			if (pid < 0) {
    304       1.1       cgd 				senderr++;
    305       1.1       cgd 				goto cant;
    306       1.1       cgd 			}
    307       1.1       cgd 			free_child(pid);
    308       1.1       cgd 		} else {
    309       1.1       cgd 			int f;
    310       1.1       cgd 			if ((fout = Fopen(fname, "a")) == NULL) {
    311      1.15       wiz 				warn("%s", fname);
    312       1.1       cgd 				senderr++;
    313       1.1       cgd 				goto cant;
    314       1.1       cgd 			}
    315       1.1       cgd 			if ((f = dup(image)) < 0) {
    316      1.15       wiz 				warn("dup");
    317       1.1       cgd 				fin = NULL;
    318       1.1       cgd 			} else
    319       1.1       cgd 				fin = Fdopen(f, "r");
    320       1.1       cgd 			if (fin == NULL) {
    321      1.19  christos 				(void)fprintf(stderr, "Can't reopen image\n");
    322      1.14       wiz 				(void)Fclose(fout);
    323       1.1       cgd 				senderr++;
    324       1.1       cgd 				goto cant;
    325       1.1       cgd 			}
    326       1.1       cgd 			rewind(fin);
    327       1.1       cgd 			while ((c = getc(fin)) != EOF)
    328      1.14       wiz 				(void)putc(c, fout);
    329       1.7       bad 			if (ferror(fout)) {
    330      1.15       wiz 				warn("%s", fname);
    331       1.7       bad 				senderr++;
    332      1.14       wiz 				(void)Fclose(fout);
    333      1.14       wiz 				(void)Fclose(fin);
    334       1.7       bad 				goto cant;
    335       1.7       bad 			}
    336      1.14       wiz 			(void)Fclose(fout);
    337      1.14       wiz 			(void)Fclose(fin);
    338       1.1       cgd 		}
    339       1.1       cgd cant:
    340       1.1       cgd 		/*
    341       1.1       cgd 		 * In days of old we removed the entry from the
    342       1.1       cgd 		 * the list; now for sake of header expansion
    343       1.1       cgd 		 * we leave it in and mark it as deleted.
    344       1.1       cgd 		 */
    345       1.1       cgd 		np->n_type |= GDEL;
    346       1.1       cgd 		np = np->n_flink;
    347       1.1       cgd 	}
    348       1.1       cgd 	if (image >= 0) {
    349      1.14       wiz 		(void)close(image);
    350       1.1       cgd 		image = -1;
    351       1.1       cgd 	}
    352      1.11       wiz 	return(begin);
    353       1.1       cgd }
    354       1.1       cgd 
    355       1.1       cgd /*
    356       1.1       cgd  * Determine if the passed address is a local "send to file" address.
    357       1.1       cgd  * If any of the network metacharacters precedes any slashes, it can't
    358       1.1       cgd  * be a filename.  We cheat with .'s to allow path names like ./...
    359       1.1       cgd  */
    360       1.3   deraadt int
    361      1.10       wiz isfileaddr(char *name)
    362       1.1       cgd {
    363       1.6     lukem 	char *cp;
    364       1.1       cgd 
    365       1.1       cgd 	if (*name == '+')
    366       1.1       cgd 		return 1;
    367       1.1       cgd 	for (cp = name; *cp; cp++) {
    368       1.1       cgd 		if (*cp == '!' || *cp == '%' || *cp == '@')
    369       1.1       cgd 			return 0;
    370       1.1       cgd 		if (*cp == '/')
    371       1.1       cgd 			return 1;
    372       1.1       cgd 	}
    373       1.1       cgd 	return 0;
    374       1.1       cgd }
    375       1.1       cgd 
    376       1.1       cgd /*
    377       1.1       cgd  * Map all of the aliased users in the invoker's mailrc
    378       1.1       cgd  * file and insert them into the list.
    379       1.1       cgd  * Changed after all these months of service to recursively
    380       1.1       cgd  * expand names (2/14/80).
    381       1.1       cgd  */
    382       1.1       cgd 
    383       1.1       cgd struct name *
    384      1.10       wiz usermap(struct name *names)
    385       1.1       cgd {
    386       1.6     lukem 	struct name *new, *np, *cp;
    387       1.1       cgd 	struct grouphead *gh;
    388       1.6     lukem 	int metoo;
    389       1.1       cgd 
    390      1.13       wiz 	new = NULL;
    391       1.1       cgd 	np = names;
    392      1.12       wiz 	metoo = (value("metoo") != NULL);
    393      1.13       wiz 	while (np != NULL) {
    394       1.1       cgd 		if (np->n_name[0] == '\\') {
    395       1.1       cgd 			cp = np->n_flink;
    396       1.1       cgd 			new = put(new, np);
    397       1.1       cgd 			np = cp;
    398       1.1       cgd 			continue;
    399       1.1       cgd 		}
    400       1.1       cgd 		gh = findgroup(np->n_name);
    401       1.1       cgd 		cp = np->n_flink;
    402      1.13       wiz 		if (gh != NULL)
    403       1.1       cgd 			new = gexpand(new, gh, metoo, np->n_type);
    404       1.1       cgd 		else
    405       1.1       cgd 			new = put(new, np);
    406       1.1       cgd 		np = cp;
    407       1.1       cgd 	}
    408       1.1       cgd 	return(new);
    409       1.1       cgd }
    410       1.1       cgd 
    411       1.1       cgd /*
    412       1.1       cgd  * Recursively expand a group name.  We limit the expansion to some
    413       1.1       cgd  * fixed level to keep things from going haywire.
    414       1.1       cgd  * Direct recursion is not expanded for convenience.
    415       1.1       cgd  */
    416       1.1       cgd 
    417       1.1       cgd struct name *
    418      1.10       wiz gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
    419       1.1       cgd {
    420       1.1       cgd 	struct group *gp;
    421       1.1       cgd 	struct grouphead *ngh;
    422       1.1       cgd 	struct name *np;
    423       1.1       cgd 	static int depth;
    424       1.1       cgd 	char *cp;
    425       1.1       cgd 
    426       1.1       cgd 	if (depth > MAXEXP) {
    427      1.19  christos 		(void)printf("Expanding alias to depth larger than %d\n", MAXEXP);
    428       1.1       cgd 		return(nlist);
    429       1.1       cgd 	}
    430       1.1       cgd 	depth++;
    431      1.13       wiz 	for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
    432       1.1       cgd 		cp = gp->ge_name;
    433       1.1       cgd 		if (*cp == '\\')
    434       1.1       cgd 			goto quote;
    435       1.1       cgd 		if (strcmp(cp, gh->g_name) == 0)
    436       1.1       cgd 			goto quote;
    437      1.13       wiz 		if ((ngh = findgroup(cp)) != NULL) {
    438       1.1       cgd 			nlist = gexpand(nlist, ngh, metoo, ntype);
    439       1.1       cgd 			continue;
    440       1.1       cgd 		}
    441       1.1       cgd quote:
    442       1.1       cgd 		np = nalloc(cp, ntype);
    443       1.1       cgd 		/*
    444       1.1       cgd 		 * At this point should allow to expand
    445       1.1       cgd 		 * to self if only person in group
    446       1.1       cgd 		 */
    447      1.13       wiz 		if (gp == gh->g_list && gp->ge_link == NULL)
    448       1.1       cgd 			goto skip;
    449       1.1       cgd 		if (!metoo && strcmp(cp, myname) == 0)
    450       1.1       cgd 			np->n_type |= GDEL;
    451       1.1       cgd skip:
    452       1.1       cgd 		nlist = put(nlist, np);
    453       1.1       cgd 	}
    454       1.1       cgd 	depth--;
    455       1.1       cgd 	return(nlist);
    456       1.1       cgd }
    457       1.1       cgd 
    458       1.1       cgd /*
    459       1.1       cgd  * Concatenate the two passed name lists, return the result.
    460       1.1       cgd  */
    461       1.1       cgd struct name *
    462      1.10       wiz cat(struct name *n1, struct name *n2)
    463       1.1       cgd {
    464       1.6     lukem 	struct name *tail;
    465       1.1       cgd 
    466      1.13       wiz 	if (n1 == NULL)
    467       1.1       cgd 		return(n2);
    468      1.13       wiz 	if (n2 == NULL)
    469       1.1       cgd 		return(n1);
    470       1.1       cgd 	tail = tailof(n1);
    471       1.1       cgd 	tail->n_flink = n2;
    472       1.1       cgd 	n2->n_blink = tail;
    473       1.1       cgd 	return(n1);
    474       1.1       cgd }
    475       1.1       cgd 
    476       1.1       cgd /*
    477       1.1       cgd  * Unpack the name list onto a vector of strings.
    478       1.1       cgd  * Return an error if the name list won't fit.
    479       1.1       cgd  */
    480      1.18  christos const char **
    481      1.10       wiz unpack(struct name *np)
    482       1.1       cgd {
    483      1.18  christos 	const char **ap, **begin;
    484       1.6     lukem 	struct name *n;
    485       1.1       cgd 	int t, extra, metoo, verbose;
    486       1.1       cgd 
    487       1.1       cgd 	n = np;
    488       1.1       cgd 	if ((t = count(n)) == 0)
    489       1.6     lukem 		errx(1, "No names to unpack");
    490       1.1       cgd 	/*
    491       1.1       cgd 	 * Compute the number of extra arguments we will need.
    492       1.1       cgd 	 * We need at least two extra -- one for "mail" and one for
    493       1.1       cgd 	 * the terminating 0 pointer.  Additional spots may be needed
    494       1.1       cgd 	 * to pass along -f to the host mailer.
    495       1.1       cgd 	 */
    496       1.1       cgd 	extra = 2;
    497       1.1       cgd 	extra++;
    498      1.12       wiz 	metoo = value("metoo") != NULL;
    499       1.1       cgd 	if (metoo)
    500       1.1       cgd 		extra++;
    501      1.12       wiz 	verbose = value("verbose") != NULL;
    502       1.1       cgd 	if (verbose)
    503       1.1       cgd 		extra++;
    504      1.18  christos 	begin = salloc((t + extra) * sizeof *begin);
    505      1.11       wiz 	ap = begin;
    506  1.19.2.1      chap 	*ap++ = "sendmail";
    507       1.1       cgd 	*ap++ = "-i";
    508       1.1       cgd 	if (metoo)
    509       1.1       cgd 		*ap++ = "-m";
    510       1.1       cgd 	if (verbose)
    511       1.1       cgd 		*ap++ = "-v";
    512      1.13       wiz 	for (; n != NULL; n = n->n_flink)
    513       1.1       cgd 		if ((n->n_type & GDEL) == 0)
    514       1.1       cgd 			*ap++ = n->n_name;
    515      1.12       wiz 	*ap = NULL;
    516      1.11       wiz 	return(begin);
    517       1.1       cgd }
    518       1.1       cgd 
    519       1.1       cgd /*
    520       1.1       cgd  * Remove all of the duplicates from the passed name list by
    521       1.1       cgd  * insertion sorting them, then checking for dups.
    522       1.1       cgd  * Return the head of the new list.
    523       1.1       cgd  */
    524       1.1       cgd struct name *
    525      1.10       wiz elide(struct name *names)
    526       1.1       cgd {
    527       1.6     lukem 	struct name *np, *t, *new;
    528       1.1       cgd 	struct name *x;
    529       1.1       cgd 
    530      1.13       wiz 	if (names == NULL)
    531      1.13       wiz 		return(NULL);
    532       1.1       cgd 	new = names;
    533       1.1       cgd 	np = names;
    534       1.1       cgd 	np = np->n_flink;
    535      1.13       wiz 	if (np != NULL)
    536      1.13       wiz 		np->n_blink = NULL;
    537      1.13       wiz 	new->n_flink = NULL;
    538      1.13       wiz 	while (np != NULL) {
    539       1.1       cgd 		t = new;
    540       1.1       cgd 		while (strcasecmp(t->n_name, np->n_name) < 0) {
    541      1.13       wiz 			if (t->n_flink == NULL)
    542       1.1       cgd 				break;
    543       1.1       cgd 			t = t->n_flink;
    544       1.1       cgd 		}
    545       1.1       cgd 
    546       1.1       cgd 		/*
    547       1.1       cgd 		 * If we ran out of t's, put the new entry after
    548       1.1       cgd 		 * the current value of t.
    549       1.1       cgd 		 */
    550       1.1       cgd 
    551       1.1       cgd 		if (strcasecmp(t->n_name, np->n_name) < 0) {
    552       1.1       cgd 			t->n_flink = np;
    553       1.1       cgd 			np->n_blink = t;
    554       1.1       cgd 			t = np;
    555       1.1       cgd 			np = np->n_flink;
    556      1.13       wiz 			t->n_flink = NULL;
    557       1.1       cgd 			continue;
    558       1.1       cgd 		}
    559       1.1       cgd 
    560       1.1       cgd 		/*
    561       1.1       cgd 		 * Otherwise, put the new entry in front of the
    562       1.1       cgd 		 * current t.  If at the front of the list,
    563       1.1       cgd 		 * the new guy becomes the new head of the list.
    564       1.1       cgd 		 */
    565       1.1       cgd 
    566       1.1       cgd 		if (t == new) {
    567       1.1       cgd 			t = np;
    568       1.1       cgd 			np = np->n_flink;
    569       1.1       cgd 			t->n_flink = new;
    570       1.1       cgd 			new->n_blink = t;
    571      1.13       wiz 			t->n_blink = NULL;
    572       1.1       cgd 			new = t;
    573       1.1       cgd 			continue;
    574       1.1       cgd 		}
    575       1.1       cgd 
    576       1.1       cgd 		/*
    577       1.1       cgd 		 * The normal case -- we are inserting into the
    578       1.1       cgd 		 * middle of the list.
    579       1.1       cgd 		 */
    580       1.1       cgd 
    581       1.1       cgd 		x = np;
    582       1.1       cgd 		np = np->n_flink;
    583       1.1       cgd 		x->n_flink = t;
    584       1.1       cgd 		x->n_blink = t->n_blink;
    585       1.1       cgd 		t->n_blink->n_flink = x;
    586       1.1       cgd 		t->n_blink = x;
    587       1.1       cgd 	}
    588       1.1       cgd 
    589       1.1       cgd 	/*
    590       1.1       cgd 	 * Now the list headed up by new is sorted.
    591       1.1       cgd 	 * Go through it and remove duplicates.
    592       1.1       cgd 	 */
    593       1.1       cgd 
    594       1.1       cgd 	np = new;
    595      1.13       wiz 	while (np != NULL) {
    596       1.1       cgd 		t = np;
    597      1.13       wiz 		while (t->n_flink != NULL &&
    598       1.1       cgd 		       strcasecmp(np->n_name, t->n_flink->n_name) == 0)
    599       1.1       cgd 			t = t->n_flink;
    600      1.13       wiz 		if (t == np || t == NULL) {
    601       1.1       cgd 			np = np->n_flink;
    602       1.1       cgd 			continue;
    603       1.1       cgd 		}
    604       1.1       cgd 
    605       1.1       cgd 		/*
    606       1.1       cgd 		 * Now t points to the last entry with the same name
    607       1.1       cgd 		 * as np.  Make np point beyond t.
    608       1.1       cgd 		 */
    609       1.1       cgd 
    610       1.1       cgd 		np->n_flink = t->n_flink;
    611      1.13       wiz 		if (t->n_flink != NULL)
    612       1.1       cgd 			t->n_flink->n_blink = np;
    613       1.1       cgd 		np = np->n_flink;
    614       1.1       cgd 	}
    615       1.1       cgd 	return(new);
    616       1.1       cgd }
    617       1.1       cgd 
    618       1.1       cgd /*
    619       1.1       cgd  * Put another node onto a list of names and return
    620       1.1       cgd  * the list.
    621       1.1       cgd  */
    622       1.1       cgd struct name *
    623      1.10       wiz put(struct name *list, struct name *node)
    624       1.1       cgd {
    625       1.1       cgd 	node->n_flink = list;
    626      1.13       wiz 	node->n_blink = NULL;
    627      1.13       wiz 	if (list != NULL)
    628       1.1       cgd 		list->n_blink = node;
    629       1.1       cgd 	return(node);
    630       1.1       cgd }
    631       1.1       cgd 
    632       1.1       cgd /*
    633       1.1       cgd  * Determine the number of undeleted elements in
    634       1.1       cgd  * a name list and return it.
    635       1.1       cgd  */
    636       1.3   deraadt int
    637      1.10       wiz count(struct name *np)
    638       1.1       cgd {
    639       1.6     lukem 	int c;
    640       1.1       cgd 
    641      1.13       wiz 	for (c = 0; np != NULL; np = np->n_flink)
    642       1.1       cgd 		if ((np->n_type & GDEL) == 0)
    643       1.1       cgd 			c++;
    644       1.1       cgd 	return c;
    645       1.1       cgd }
    646       1.1       cgd 
    647       1.1       cgd /*
    648       1.1       cgd  * Delete the given name from a namelist.
    649       1.1       cgd  */
    650       1.1       cgd struct name *
    651      1.10       wiz delname(struct name *np, char name[])
    652       1.1       cgd {
    653       1.6     lukem 	struct name *p;
    654       1.1       cgd 
    655      1.13       wiz 	for (p = np; p != NULL; p = p->n_flink)
    656       1.1       cgd 		if (strcasecmp(p->n_name, name) == 0) {
    657      1.13       wiz 			if (p->n_blink == NULL) {
    658      1.13       wiz 				if (p->n_flink != NULL)
    659      1.13       wiz 					p->n_flink->n_blink = NULL;
    660       1.1       cgd 				np = p->n_flink;
    661       1.1       cgd 				continue;
    662       1.1       cgd 			}
    663      1.13       wiz 			if (p->n_flink == NULL) {
    664      1.13       wiz 				if (p->n_blink != NULL)
    665      1.13       wiz 					p->n_blink->n_flink = NULL;
    666       1.1       cgd 				continue;
    667       1.1       cgd 			}
    668       1.1       cgd 			p->n_blink->n_flink = p->n_flink;
    669       1.1       cgd 			p->n_flink->n_blink = p->n_blink;
    670       1.1       cgd 		}
    671       1.1       cgd 	return np;
    672       1.1       cgd }
    673       1.1       cgd 
    674       1.1       cgd /*
    675       1.1       cgd  * Pretty print a name list
    676       1.1       cgd  * Uncomment it if you need it.
    677       1.1       cgd  */
    678       1.1       cgd 
    679       1.1       cgd /*
    680       1.3   deraadt void
    681       1.1       cgd prettyprint(name)
    682       1.1       cgd 	struct name *name;
    683       1.1       cgd {
    684       1.6     lukem 	struct name *np;
    685       1.1       cgd 
    686       1.1       cgd 	np = name;
    687      1.13       wiz 	while (np != NULL) {
    688      1.19  christos 		(void)fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
    689       1.1       cgd 		np = np->n_flink;
    690       1.1       cgd 	}
    691      1.19  christos 	(void)fprintf(stderr, "\n");
    692       1.1       cgd }
    693       1.1       cgd */
    694