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