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