xref: /illumos-gate/usr/src/cmd/mailx/names.c (revision 48bbca81)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
246c83d09fSrobbin  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
256c83d09fSrobbin  * Use is subject to license terms.
26*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
296c83d09fSrobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
306c83d09fSrobbin /*	  All Rights Reserved  	*/
316c83d09fSrobbin 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bd9Sstevel@tonic-gate  * The Regents of the University of California
357c478bd9Sstevel@tonic-gate  * All Rights Reserved
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
387c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
397c478bd9Sstevel@tonic-gate  * contributors.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
447c478bd9Sstevel@tonic-gate  *	mail program
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * Handle name lists.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "rcv.h"
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static struct name	*nalloc(char str[]);
537c478bd9Sstevel@tonic-gate static int		isfileaddr(char *name);
547c478bd9Sstevel@tonic-gate static int		lengthof(struct name *name);
55287247a8SAlexander Pyhalov static struct name	*gexpand(struct name *nlist, struct grouphead *gh,
56287247a8SAlexander Pyhalov     int metoo, int arg_ntype);
57287247a8SAlexander Pyhalov static char		*norm(register char *user, register char *ubuf,
58287247a8SAlexander Pyhalov     int nbangs);
597c478bd9Sstevel@tonic-gate static struct name	*put(struct name *list, struct name *node);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Allocate a single element of a name list,
637c478bd9Sstevel@tonic-gate  * initialize its name field to the passed
647c478bd9Sstevel@tonic-gate  * name and return it.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static struct name *
nalloc(char str[])687c478bd9Sstevel@tonic-gate nalloc(char str[])
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	register struct name *np;
717c478bd9Sstevel@tonic-gate 
72287247a8SAlexander Pyhalov 	np = (struct name *)salloc(sizeof (*np));
737c478bd9Sstevel@tonic-gate 	np->n_flink = NIL;
747c478bd9Sstevel@tonic-gate 	np->n_blink = NIL;
757c478bd9Sstevel@tonic-gate 	np->n_type = -1;
767c478bd9Sstevel@tonic-gate 	np->n_full = savestr(str);
777c478bd9Sstevel@tonic-gate 	np->n_name = skin(np->n_full);
78287247a8SAlexander Pyhalov 	return (np);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Find the tail of a list and return it.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct name *
tailof(struct name * name)867c478bd9Sstevel@tonic-gate tailof(struct name *name)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	register struct name *np;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	np = name;
917c478bd9Sstevel@tonic-gate 	if (np == NIL)
92287247a8SAlexander Pyhalov 		return (NIL);
937c478bd9Sstevel@tonic-gate 	while (np->n_flink != NIL)
947c478bd9Sstevel@tonic-gate 		np = np->n_flink;
95287247a8SAlexander Pyhalov 	return (np);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Extract a list of names from a line,
1007c478bd9Sstevel@tonic-gate  * and make a list of names from it.
1017c478bd9Sstevel@tonic-gate  * Return the list or NIL if none found.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate struct name *
extract(char line[],int arg_ntype)1057c478bd9Sstevel@tonic-gate extract(char line[], int arg_ntype)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	short ntype = (short)arg_ntype;
1087c478bd9Sstevel@tonic-gate 	register char *cp;
1097c478bd9Sstevel@tonic-gate 	register struct name *top, *np, *t;
1107c478bd9Sstevel@tonic-gate 	char nbuf[BUFSIZ], abuf[BUFSIZ];
1117c478bd9Sstevel@tonic-gate 	int comma;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (line == NOSTR || strlen(line) == 0)
114287247a8SAlexander Pyhalov 		return (NIL);
1157c478bd9Sstevel@tonic-gate 	comma = docomma(line);
1167c478bd9Sstevel@tonic-gate 	top = NIL;
1177c478bd9Sstevel@tonic-gate 	np = NIL;
1187c478bd9Sstevel@tonic-gate 	cp = line;
1197c478bd9Sstevel@tonic-gate 	while ((cp = yankword(cp, nbuf, sizeof (nbuf), comma)) != NOSTR) {
1207c478bd9Sstevel@tonic-gate 		if (np != NIL && equal(nbuf, "at")) {
1217c478bd9Sstevel@tonic-gate 			nstrcpy(abuf, sizeof (abuf), nbuf);
1227c478bd9Sstevel@tonic-gate 			if ((cp = yankword(cp, nbuf, sizeof (nbuf),
123287247a8SAlexander Pyhalov 			    comma)) == NOSTR) {
1247c478bd9Sstevel@tonic-gate 				nstrcpy(nbuf, sizeof (nbuf), abuf);
1257c478bd9Sstevel@tonic-gate 				goto normal;
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 			snprintf(abuf, sizeof (abuf), "%s@%s", np->n_name,
128287247a8SAlexander Pyhalov 			    nbuf);
1297c478bd9Sstevel@tonic-gate 			np->n_name = savestr(abuf);
1307c478bd9Sstevel@tonic-gate 			continue;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate normal:
1337c478bd9Sstevel@tonic-gate 		t = nalloc(nbuf);
1347c478bd9Sstevel@tonic-gate 		t->n_type = ntype;
1357c478bd9Sstevel@tonic-gate 		if (top == NIL)
1367c478bd9Sstevel@tonic-gate 			top = t;
1377c478bd9Sstevel@tonic-gate 		else
1387c478bd9Sstevel@tonic-gate 			np->n_flink = t;
1397c478bd9Sstevel@tonic-gate 		t->n_blink = np;
1407c478bd9Sstevel@tonic-gate 		np = t;
1417c478bd9Sstevel@tonic-gate 	}
142287247a8SAlexander Pyhalov 	return (top);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Turn a list of names into a string of the same names.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate char *
detract(register struct name * np,int ntype)1507c478bd9Sstevel@tonic-gate detract(register struct name *np, int ntype)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	register int s;
1537c478bd9Sstevel@tonic-gate 	register char *cp, *top;
1547c478bd9Sstevel@tonic-gate 	register struct name *p;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (np == NIL)
157287247a8SAlexander Pyhalov 		return (NOSTR);
1587c478bd9Sstevel@tonic-gate 	s = 0;
1597c478bd9Sstevel@tonic-gate 	for (p = np; p != NIL; p = p->n_flink) {
160287247a8SAlexander Pyhalov 		if ((ntype && (p->n_type & GMASK) != ntype) ||
161287247a8SAlexander Pyhalov 		    (p->n_type & GDEL))
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 		s += strlen(p->n_full) + 2;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	if (s == 0)
166287247a8SAlexander Pyhalov 		return (NOSTR);
1677c478bd9Sstevel@tonic-gate 	top = (char *)salloc((unsigned)(++s));
1687c478bd9Sstevel@tonic-gate 	cp = top;
1697c478bd9Sstevel@tonic-gate 	for (p = np; p != NIL; p = p->n_flink) {
170287247a8SAlexander Pyhalov 		if ((ntype && (p->n_type & GMASK) != ntype) ||
171287247a8SAlexander Pyhalov 		    (p->n_type & GDEL))
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 		cp = copy(p->n_full, cp);
1747c478bd9Sstevel@tonic-gate 		*cp++ = ',';
1757c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	*cp = 0;
178287247a8SAlexander Pyhalov 	return (top);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate struct name *
outpre(struct name * to)1827c478bd9Sstevel@tonic-gate outpre(struct name *to)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	register struct name *np;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	for (np = to; np; np = np->n_flink)
1877c478bd9Sstevel@tonic-gate 		if (isfileaddr(np->n_name))
1887c478bd9Sstevel@tonic-gate 			np->n_type |= GDEL;
189287247a8SAlexander Pyhalov 	return (to);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * For each recipient in the passed name list with a /
1947c478bd9Sstevel@tonic-gate  * in the name, append the message to the end of the named file
1955422785dSRobert Mustacchi  * and remove them from the recipient list.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Recipients whose name begins with | are piped through the given
1987c478bd9Sstevel@tonic-gate  * program and removed.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
outof(struct name * names,FILE * fo)2027c478bd9Sstevel@tonic-gate outof(struct name *names, FILE *fo)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	register int c;
2057c478bd9Sstevel@tonic-gate 	register struct name *np;
2067c478bd9Sstevel@tonic-gate 	time_t now;
2077c478bd9Sstevel@tonic-gate 	char *date, *fname, *shell;
2087c478bd9Sstevel@tonic-gate 	FILE *fout, *fin;
2097c478bd9Sstevel@tonic-gate 	int ispipe;
2107c478bd9Sstevel@tonic-gate 	int nout = 0;
211287247a8SAlexander Pyhalov 	int fd = 0;
2127c478bd9Sstevel@tonic-gate #ifdef preSVr4
2137c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
2147c478bd9Sstevel@tonic-gate #endif
2157c478bd9Sstevel@tonic-gate 
2165422785dSRobert Mustacchi 	if (value("expandaddr") == NOSTR)
2175422785dSRobert Mustacchi 		return (nout);
2185422785dSRobert Mustacchi 
2197c478bd9Sstevel@tonic-gate 	for (np = names; np != NIL; np = np->n_flink) {
2207c478bd9Sstevel@tonic-gate 		if (!isfileaddr(np->n_name) && np->n_name[0] != '|')
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 		nout++;
2237c478bd9Sstevel@tonic-gate 		ispipe = np->n_name[0] == '|';
2247c478bd9Sstevel@tonic-gate 		if (ispipe)
2257c478bd9Sstevel@tonic-gate 			fname = np->n_name+1;
2267c478bd9Sstevel@tonic-gate 		else
2277c478bd9Sstevel@tonic-gate 			fname = safeexpand(np->n_name);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		/*
2307c478bd9Sstevel@tonic-gate 		 * See if we have copied the complete message out yet.
2317c478bd9Sstevel@tonic-gate 		 * If not, do so.
2327c478bd9Sstevel@tonic-gate 		 */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		if (image < 0) {
2357c478bd9Sstevel@tonic-gate 			fd = open(tempEdit, O_CREAT|O_EXCL|O_APPEND|O_WRONLY,
236287247a8SAlexander Pyhalov 			    0600);
2377c478bd9Sstevel@tonic-gate 			if ((fd  < 0) && (errno == EEXIST)) {
2387c478bd9Sstevel@tonic-gate 				if ((fd = open(tempEdit, O_APPEND|O_WRONLY,
239287247a8SAlexander Pyhalov 				    0600)) < 0) {
2407c478bd9Sstevel@tonic-gate 					perror(tempEdit);
2417c478bd9Sstevel@tonic-gate 					senderr++;
2427c478bd9Sstevel@tonic-gate 					goto cant;
2437c478bd9Sstevel@tonic-gate 				}
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 			if ((fout = fdopen(fd, "a")) == NULL) {
2467c478bd9Sstevel@tonic-gate 				perror(tempEdit);
2477c478bd9Sstevel@tonic-gate 				senderr++;
2487c478bd9Sstevel@tonic-gate 				goto cant;
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 			image = open(tempEdit, O_RDWR);
2517c478bd9Sstevel@tonic-gate 			unlink(tempEdit);
2527c478bd9Sstevel@tonic-gate 			if (image < 0) {
2537c478bd9Sstevel@tonic-gate 				perror(tempEdit);
2547c478bd9Sstevel@tonic-gate 				senderr++;
2557c478bd9Sstevel@tonic-gate 				goto cant;
2567c478bd9Sstevel@tonic-gate 			} else {
2577c478bd9Sstevel@tonic-gate 				rewind(fo);
2587c478bd9Sstevel@tonic-gate 				time(&now);
2597c478bd9Sstevel@tonic-gate 				date = ctime(&now);
2607c478bd9Sstevel@tonic-gate 				fprintf(fout, "From %s %s", myname, date);
2617c478bd9Sstevel@tonic-gate 				while ((c = getc(fo)) != EOF)
2627c478bd9Sstevel@tonic-gate 					putc(c, fout);
2637c478bd9Sstevel@tonic-gate 				rewind(fo);
2647c478bd9Sstevel@tonic-gate 				fflush(fout);
2657c478bd9Sstevel@tonic-gate 				if (fferror(fout))
2667c478bd9Sstevel@tonic-gate 					perror(tempEdit);
2677c478bd9Sstevel@tonic-gate 				fclose(fout);
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		/*
2727c478bd9Sstevel@tonic-gate 		 * Now either copy "image" to the desired file
2737c478bd9Sstevel@tonic-gate 		 * or give it as the standard input to the desired
2747c478bd9Sstevel@tonic-gate 		 * program as appropriate.
2757c478bd9Sstevel@tonic-gate 		 */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if (ispipe) {
2787c478bd9Sstevel@tonic-gate 			wait((int *)NULL);
2797c478bd9Sstevel@tonic-gate 			switch (fork()) {
2807c478bd9Sstevel@tonic-gate 			case 0:
2817c478bd9Sstevel@tonic-gate 				sigchild();
2827c478bd9Sstevel@tonic-gate 				sigset(SIGHUP, SIG_IGN);
2837c478bd9Sstevel@tonic-gate 				sigset(SIGINT, SIG_IGN);
2847c478bd9Sstevel@tonic-gate 				sigset(SIGQUIT, SIG_IGN);
2857c478bd9Sstevel@tonic-gate 				close(0);
2867c478bd9Sstevel@tonic-gate 				dup(image);
2877c478bd9Sstevel@tonic-gate 				close(image);
2887c478bd9Sstevel@tonic-gate 				lseek(0, 0L, 0);
289287247a8SAlexander Pyhalov 				if ((shell = value("SHELL")) == NOSTR ||
290287247a8SAlexander Pyhalov 				    *shell == '\0')
2917c478bd9Sstevel@tonic-gate 					shell = SHELL;
292287247a8SAlexander Pyhalov 				(void) execlp(shell, shell, "-c", fname,
293287247a8SAlexander Pyhalov 				    (char *)0);
2947c478bd9Sstevel@tonic-gate 				perror(shell);
2957c478bd9Sstevel@tonic-gate 				exit(1);
2967c478bd9Sstevel@tonic-gate 				break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			case (pid_t)-1:
2997c478bd9Sstevel@tonic-gate 				perror("fork");
3007c478bd9Sstevel@tonic-gate 				senderr++;
3017c478bd9Sstevel@tonic-gate 				goto cant;
3027c478bd9Sstevel@tonic-gate 			}
303287247a8SAlexander Pyhalov 		} else {
3047c478bd9Sstevel@tonic-gate 			if ((fout = fopen(fname, "a")) == NULL) {
3057c478bd9Sstevel@tonic-gate 				perror(fname);
3067c478bd9Sstevel@tonic-gate 				senderr++;
3077c478bd9Sstevel@tonic-gate 				goto cant;
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 			fin = Fdopen(image, "r");
3107c478bd9Sstevel@tonic-gate 			if (fin == NULL) {
3117c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3127c478bd9Sstevel@tonic-gate 				    gettext("Can't reopen image\n"));
3137c478bd9Sstevel@tonic-gate 				fclose(fout);
3147c478bd9Sstevel@tonic-gate 				senderr++;
3157c478bd9Sstevel@tonic-gate 				goto cant;
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 			rewind(fin);
3187c478bd9Sstevel@tonic-gate #ifdef preSVr4
3197c478bd9Sstevel@tonic-gate 			putc(getc(fin), fout);
320287247a8SAlexander Pyhalov 			while (fgets(line, sizeof (line), fin)) {
321287247a8SAlexander Pyhalov 				if (strncmp(line, "From ", 5) == 0)
3227c478bd9Sstevel@tonic-gate 					putc('>', fout);
3237c478bd9Sstevel@tonic-gate 				fputs(line, fout);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate #else
3267c478bd9Sstevel@tonic-gate 			while ((c = getc(fin)) != EOF)
3277c478bd9Sstevel@tonic-gate 				putc(c, fout);
3287c478bd9Sstevel@tonic-gate #endif
3297c478bd9Sstevel@tonic-gate 			putc('\n', fout);
3307c478bd9Sstevel@tonic-gate 			fflush(fout);
3317c478bd9Sstevel@tonic-gate 			if (fferror(fout))
3327c478bd9Sstevel@tonic-gate 				senderr++, perror(fname);
3337c478bd9Sstevel@tonic-gate 			fclose(fout);
3347c478bd9Sstevel@tonic-gate 			fclose(fin);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate cant:
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * In days of old we removed the entry from the
3397c478bd9Sstevel@tonic-gate 		 * the list; now for sake of header expansion
3407c478bd9Sstevel@tonic-gate 		 * we leave it in and mark it as deleted.
3417c478bd9Sstevel@tonic-gate 		 */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #ifdef CRAZYWOW
3447c478bd9Sstevel@tonic-gate 		{
3457c478bd9Sstevel@tonic-gate 		register struct name *t, *x;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		if (np == top) {
3487c478bd9Sstevel@tonic-gate 			top = np->n_flink;
3497c478bd9Sstevel@tonic-gate 			if (top != NIL)
3507c478bd9Sstevel@tonic-gate 				top->n_blink = NIL;
3517c478bd9Sstevel@tonic-gate 			np = top;
3527c478bd9Sstevel@tonic-gate 			continue;
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		x = np->n_blink;
3557c478bd9Sstevel@tonic-gate 		t = np->n_flink;
3567c478bd9Sstevel@tonic-gate 		x->n_flink = t;
3577c478bd9Sstevel@tonic-gate 		if (t != NIL)
3587c478bd9Sstevel@tonic-gate 			t->n_blink = x;
3597c478bd9Sstevel@tonic-gate 		np = t;
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate #endif
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		np->n_type |= GDEL;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	if (image >= 0) {
3667c478bd9Sstevel@tonic-gate 		close(image);
3677c478bd9Sstevel@tonic-gate 		image = -1;
3687c478bd9Sstevel@tonic-gate 	}
369287247a8SAlexander Pyhalov 	return (nout);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Determine if the passed address is a local "send to file" address.
3747c478bd9Sstevel@tonic-gate  * If any of the network metacharacters precedes any slashes, it can't
3757c478bd9Sstevel@tonic-gate  * be a filename.  We cheat with .'s to allow path names like ./...
3767c478bd9Sstevel@tonic-gate  * If "fcc" has been unset, then short-circuit those tests, but not
3777c478bd9Sstevel@tonic-gate  * the +... test.
3787c478bd9Sstevel@tonic-gate  */
379287247a8SAlexander Pyhalov static int
isfileaddr(char * name)3807c478bd9Sstevel@tonic-gate isfileaddr(char *name)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	register char *cp;
3837c478bd9Sstevel@tonic-gate 	char *fcc = value("fcc");
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (any('@', name))
386287247a8SAlexander Pyhalov 		return (0);
3877c478bd9Sstevel@tonic-gate 	if (*name == '+')
388287247a8SAlexander Pyhalov 		return (1);
3897c478bd9Sstevel@tonic-gate 	if (fcc == NOSTR)
390287247a8SAlexander Pyhalov 		return (0);
3917c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; cp++) {
3927c478bd9Sstevel@tonic-gate 		if (*cp == '.')
3937c478bd9Sstevel@tonic-gate 			continue;
3947c478bd9Sstevel@tonic-gate 		if (any(*cp, metanet))
395287247a8SAlexander Pyhalov 			return (0);
3967c478bd9Sstevel@tonic-gate 		if (*cp == '/')
397287247a8SAlexander Pyhalov 			return (1);
3987c478bd9Sstevel@tonic-gate 	}
399287247a8SAlexander Pyhalov 	return (0);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Map all of the aliased users in the invoker's mailrc
4047c478bd9Sstevel@tonic-gate  * file and insert them into the list.
4057c478bd9Sstevel@tonic-gate  * Changed after all these months of service to recursively
4067c478bd9Sstevel@tonic-gate  * expand names (2/14/80).
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate struct name *
usermap(struct name * names)4107c478bd9Sstevel@tonic-gate usermap(struct name *names)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	register struct name *newnames, *np, *cp;
4137c478bd9Sstevel@tonic-gate 	struct grouphead *gh;
4147c478bd9Sstevel@tonic-gate 	register int metoo;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	newnames = NIL;
4177c478bd9Sstevel@tonic-gate 	np = names;
4187c478bd9Sstevel@tonic-gate 	metoo = (value("metoo") != NOSTR);
4197c478bd9Sstevel@tonic-gate 	while (np != NIL) {
4207c478bd9Sstevel@tonic-gate 		if (np->n_name[0] == '\\') {
4217c478bd9Sstevel@tonic-gate 			cp = np->n_flink;
4227c478bd9Sstevel@tonic-gate 			newnames = put(newnames, np);
4237c478bd9Sstevel@tonic-gate 			np = cp;
4247c478bd9Sstevel@tonic-gate 			continue;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 		gh = findgroup(np->n_name);
4277c478bd9Sstevel@tonic-gate 		cp = np->n_flink;
4287c478bd9Sstevel@tonic-gate 		if (gh != NOGRP)
4297c478bd9Sstevel@tonic-gate 			newnames = gexpand(newnames, gh, metoo, np->n_type);
4307c478bd9Sstevel@tonic-gate 		else
4317c478bd9Sstevel@tonic-gate 			newnames = put(newnames, np);
4327c478bd9Sstevel@tonic-gate 		np = cp;
4337c478bd9Sstevel@tonic-gate 	}
434287247a8SAlexander Pyhalov 	return (newnames);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * Recursively expand a group name.  We limit the expansion to some
4397c478bd9Sstevel@tonic-gate  * fixed level to keep things from going haywire.
4407c478bd9Sstevel@tonic-gate  * Direct recursion is not expanded for convenience.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate static struct name *
gexpand(struct name * nlist,struct grouphead * gh,int metoo,int arg_ntype)4447c478bd9Sstevel@tonic-gate gexpand(struct name *nlist, struct grouphead *gh, int metoo, int arg_ntype)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	short ntype = (short)arg_ntype;
4477c478bd9Sstevel@tonic-gate 	struct mgroup *gp;
4487c478bd9Sstevel@tonic-gate 	struct grouphead *ngh;
4497c478bd9Sstevel@tonic-gate 	struct name *np;
4507c478bd9Sstevel@tonic-gate 	static int depth;
4517c478bd9Sstevel@tonic-gate 	register char *cp;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (depth > MAXEXP) {
4547c478bd9Sstevel@tonic-gate 		printf(gettext("Expanding alias to depth larger than %d\n"),
4557c478bd9Sstevel@tonic-gate 		    MAXEXP);
456287247a8SAlexander Pyhalov 		return (nlist);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	depth++;
4597c478bd9Sstevel@tonic-gate 	for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) {
4607c478bd9Sstevel@tonic-gate 		cp = gp->ge_name;
4617c478bd9Sstevel@tonic-gate 		if (*cp == '\\')
4627c478bd9Sstevel@tonic-gate 			goto quote;
4637c478bd9Sstevel@tonic-gate 		if (strcmp(cp, gh->g_name) == 0)
4647c478bd9Sstevel@tonic-gate 			goto quote;
4657c478bd9Sstevel@tonic-gate 		if ((ngh = findgroup(cp)) != NOGRP) {
4667c478bd9Sstevel@tonic-gate 			nlist = gexpand(nlist, ngh, metoo, ntype);
4677c478bd9Sstevel@tonic-gate 			continue;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate quote:
4707c478bd9Sstevel@tonic-gate 		np = nalloc(cp);
4717c478bd9Sstevel@tonic-gate 		np->n_type = ntype;
4727c478bd9Sstevel@tonic-gate 		/*
4737c478bd9Sstevel@tonic-gate 		 * At this point should allow to expand
4747c478bd9Sstevel@tonic-gate 		 * to self if only person in group
4757c478bd9Sstevel@tonic-gate 		 */
4767c478bd9Sstevel@tonic-gate 		if (gp == gh->g_list && gp->ge_link == NOGE)
4777c478bd9Sstevel@tonic-gate 			goto skip;
4787c478bd9Sstevel@tonic-gate 		if (!metoo && samebody(myname, gp->ge_name, FALSE))
4797c478bd9Sstevel@tonic-gate 			np->n_type |= GDEL;
4807c478bd9Sstevel@tonic-gate skip:
4817c478bd9Sstevel@tonic-gate 		nlist = put(nlist, np);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	depth--;
484287247a8SAlexander Pyhalov 	return (nlist);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * Normalize a network name for comparison purposes.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate static char *
norm(register char * user,register char * ubuf,int nbangs)4917c478bd9Sstevel@tonic-gate norm(register char *user, register char *ubuf, int nbangs)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	register char *cp;
4947c478bd9Sstevel@tonic-gate 	int inubuf = 0;
4957c478bd9Sstevel@tonic-gate 
496287247a8SAlexander Pyhalov 	while (*user++ == '!')
497287247a8SAlexander Pyhalov 		;
4987c478bd9Sstevel@tonic-gate 	user--;
4997c478bd9Sstevel@tonic-gate 	if (!strchr(user, '!')) {
5007c478bd9Sstevel@tonic-gate 		snprintf(ubuf, BUFSIZ, "%s!%s", host, user);
5017c478bd9Sstevel@tonic-gate 		user = ubuf;
5027c478bd9Sstevel@tonic-gate 		inubuf++;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	if (nbangs) {
5057c478bd9Sstevel@tonic-gate 		cp = user + strlen(user);
5067c478bd9Sstevel@tonic-gate 		while (nbangs--)
507287247a8SAlexander Pyhalov 			while (cp > user && *--cp != '!')
508287247a8SAlexander Pyhalov 				;
5097c478bd9Sstevel@tonic-gate 		user = (cp > user) ? ++cp : cp;
5107c478bd9Sstevel@tonic-gate 		/*
5117c478bd9Sstevel@tonic-gate 		 * Now strip off all Internet-type
5127c478bd9Sstevel@tonic-gate 		 * hosts.
5137c478bd9Sstevel@tonic-gate 		 */
5147c478bd9Sstevel@tonic-gate 		if ((cp = strchr(user, '%')) == NOSTR)
5157c478bd9Sstevel@tonic-gate 			cp = strchr(user, '@');
5167c478bd9Sstevel@tonic-gate 		if (cp != NOSTR) {
5177c478bd9Sstevel@tonic-gate 			if (!inubuf) {
5187c478bd9Sstevel@tonic-gate 				strncpy(ubuf, user, cp - user);
5197c478bd9Sstevel@tonic-gate 				ubuf[cp - user] = '\0';
5207c478bd9Sstevel@tonic-gate 				user = ubuf;
5217c478bd9Sstevel@tonic-gate 			} else
5227c478bd9Sstevel@tonic-gate 				*cp = '\0';
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	}
525287247a8SAlexander Pyhalov 	return (user);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Implement allnet options.
5307c478bd9Sstevel@tonic-gate  */
531287247a8SAlexander Pyhalov int
samebody(register char * user,register char * addr,int fuzzy)5327c478bd9Sstevel@tonic-gate samebody(register char *user, register char *addr, int fuzzy)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	char ubuf[BUFSIZ], abuf[BUFSIZ];
5357c478bd9Sstevel@tonic-gate 	char *allnet = value("allnet");
536287247a8SAlexander Pyhalov 	int nbangs = allnet ? (strcmp(allnet, "uucp") == 0) ? 2 : 1 : 0;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (fuzzy && value("fuzzymatch")) {
5397c478bd9Sstevel@tonic-gate 		int i;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		(void) strlcpy(ubuf, user, BUFSIZ);
5427c478bd9Sstevel@tonic-gate 		for (i = 0; ubuf[i]; i++)
5437c478bd9Sstevel@tonic-gate 			ubuf[i] = tolower(ubuf[i]);
5447c478bd9Sstevel@tonic-gate 		(void) strlcpy(abuf, addr, BUFSIZ);
5457c478bd9Sstevel@tonic-gate 		for (i = 0; abuf[i]; i++)
5467c478bd9Sstevel@tonic-gate 			abuf[i] = tolower(abuf[i]);
5477c478bd9Sstevel@tonic-gate 		return (strstr(abuf, ubuf) != NOSTR);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	user = norm(user, ubuf, nbangs);
5507c478bd9Sstevel@tonic-gate 	addr = norm(addr, abuf, nbangs);
551287247a8SAlexander Pyhalov 	return (strcmp(user, addr) == 0);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Compute the length of the passed name list and
5567c478bd9Sstevel@tonic-gate  * return it.
5577c478bd9Sstevel@tonic-gate  */
558287247a8SAlexander Pyhalov static int
lengthof(struct name * name)5597c478bd9Sstevel@tonic-gate lengthof(struct name *name)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	register struct name *np;
5627c478bd9Sstevel@tonic-gate 	register int c;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	for (c = 0, np = name; np != NIL; c++, np = np->n_flink)
5657c478bd9Sstevel@tonic-gate 		;
566287247a8SAlexander Pyhalov 	return (c);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * Concatenate the two passed name lists, return the result.
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate struct name *
cat(struct name * n1,struct name * n2)5747c478bd9Sstevel@tonic-gate cat(struct name *n1, struct name *n2)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	register struct name *tail;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (n1 == NIL)
579287247a8SAlexander Pyhalov 		return (n2);
5807c478bd9Sstevel@tonic-gate 	if (n2 == NIL)
581287247a8SAlexander Pyhalov 		return (n1);
5827c478bd9Sstevel@tonic-gate 	tail = tailof(n1);
5837c478bd9Sstevel@tonic-gate 	tail->n_flink = n2;
5847c478bd9Sstevel@tonic-gate 	n2->n_blink = tail;
585287247a8SAlexander Pyhalov 	return (n1);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * Unpack the name list onto a vector of strings.
5907c478bd9Sstevel@tonic-gate  * Return an error if the name list won't fit.
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate char **
unpack(struct name * np)5947c478bd9Sstevel@tonic-gate unpack(struct name *np)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	register char **ap, **top;
5977c478bd9Sstevel@tonic-gate 	register struct name *n;
5987c478bd9Sstevel@tonic-gate 	char hbuf[10];
5997c478bd9Sstevel@tonic-gate 	int t, extra, metoo, verbose;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	n = np;
6027c478bd9Sstevel@tonic-gate 	if ((t = lengthof(n)) == 0)
6037c478bd9Sstevel@tonic-gate 		panic("No names to unpack");
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6065422785dSRobert Mustacchi 	 * Compute the number of extra arguments we will need.  We need at least
6075422785dSRobert Mustacchi 	 * 3 extra -- one for "mail", one for a terminating -- to stop sendmail
6085422785dSRobert Mustacchi 	 * option processing, and one for the terminating 0 pointer.
6095422785dSRobert Mustacchi 	 *
6105422785dSRobert Mustacchi 	 * Additional spots may be needed to pass along -r and -f to the host
6115422785dSRobert Mustacchi 	 * mailer.
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 
6145422785dSRobert Mustacchi 	extra = 3;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (rflag != NOSTR)
6177c478bd9Sstevel@tonic-gate 		extra += 2;
6187c478bd9Sstevel@tonic-gate 	extra++;
6197c478bd9Sstevel@tonic-gate 	metoo = value("metoo") != NOSTR;
6207c478bd9Sstevel@tonic-gate 	if (metoo)
6217c478bd9Sstevel@tonic-gate 		extra++;
6227c478bd9Sstevel@tonic-gate 	verbose = value("verbose") != NOSTR;
6237c478bd9Sstevel@tonic-gate 	if (verbose)
6247c478bd9Sstevel@tonic-gate 		extra++;
6257c478bd9Sstevel@tonic-gate 	if (hflag)
6267c478bd9Sstevel@tonic-gate 		extra += 2;
627287247a8SAlexander Pyhalov 	top = (char **)salloc((t + extra) * sizeof (char *));
6287c478bd9Sstevel@tonic-gate 	ap = top;
629287247a8SAlexander Pyhalov 	*ap++ = "sendmail";
6307c478bd9Sstevel@tonic-gate 	if (rflag != NOSTR) {
6317c478bd9Sstevel@tonic-gate 		*ap++ = "-r";
6327c478bd9Sstevel@tonic-gate 		*ap++ = rflag;
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	*ap++ = "-i";
6357c478bd9Sstevel@tonic-gate 	if (metoo)
6367c478bd9Sstevel@tonic-gate 		*ap++ = "-m";
6377c478bd9Sstevel@tonic-gate 	if (verbose)
6387c478bd9Sstevel@tonic-gate 		*ap++ = "-v";
6397c478bd9Sstevel@tonic-gate 	if (hflag) {
6407c478bd9Sstevel@tonic-gate 		*ap++ = "-h";
6417c478bd9Sstevel@tonic-gate 		snprintf(hbuf, sizeof (hbuf), "%d", hflag);
6427c478bd9Sstevel@tonic-gate 		*ap++ = savestr(hbuf);
6437c478bd9Sstevel@tonic-gate 	}
6445422785dSRobert Mustacchi 	*ap++ = "--";
6457c478bd9Sstevel@tonic-gate 	while (n != NIL) {
6467c478bd9Sstevel@tonic-gate 		if (n->n_type & GDEL) {
6477c478bd9Sstevel@tonic-gate 			n = n->n_flink;
6487c478bd9Sstevel@tonic-gate 			continue;
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		*ap++ = n->n_name;
6517c478bd9Sstevel@tonic-gate 		n = n->n_flink;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	*ap = NOSTR;
654287247a8SAlexander Pyhalov 	return (top);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
658*48bbca81SDaniel Hoffman  * See if the user named themself as a destination
659*48bbca81SDaniel Hoffman  * for outgoing mail. If so, set the global flag
660*48bbca81SDaniel Hoffman  * selfsent so that we avoid removing their mailbox.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate 
663287247a8SAlexander Pyhalov void
mechk(struct name * names)6647c478bd9Sstevel@tonic-gate mechk(struct name *names)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	register struct name *np;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	for (np = names; np != NIL; np = np->n_flink)
6697c478bd9Sstevel@tonic-gate 		if ((np->n_type & GDEL) == 0 &&
6707c478bd9Sstevel@tonic-gate 		    samebody(np->n_name, myname, FALSE)) {
6717c478bd9Sstevel@tonic-gate 			selfsent++;
6727c478bd9Sstevel@tonic-gate 			return;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Remove all of the duplicates from the passed name list by
6787c478bd9Sstevel@tonic-gate  * insertion sorting them, then checking for dups.
6797c478bd9Sstevel@tonic-gate  * Return the head of the new list.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate struct name *
elide(struct name * names)6837c478bd9Sstevel@tonic-gate elide(struct name *names)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	register struct name *np, *t, *newnames;
6867c478bd9Sstevel@tonic-gate 	struct name *x;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (names == NIL)
689287247a8SAlexander Pyhalov 		return (NIL);
6907c478bd9Sstevel@tonic-gate 	newnames = names;
6917c478bd9Sstevel@tonic-gate 	np = names;
6927c478bd9Sstevel@tonic-gate 	np = np->n_flink;
6937c478bd9Sstevel@tonic-gate 	if (np != NIL)
6947c478bd9Sstevel@tonic-gate 		np->n_blink = NIL;
6957c478bd9Sstevel@tonic-gate 	newnames->n_flink = NIL;
6967c478bd9Sstevel@tonic-gate 	while (np != NIL) {
6977c478bd9Sstevel@tonic-gate 		t = newnames;
6987c478bd9Sstevel@tonic-gate 		while (strcmp(t->n_name, np->n_name) < 0) {
6997c478bd9Sstevel@tonic-gate 			if (t->n_flink == NIL)
7007c478bd9Sstevel@tonic-gate 				break;
7017c478bd9Sstevel@tonic-gate 			t = t->n_flink;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		/*
7057c478bd9Sstevel@tonic-gate 		 * If we ran out of t's, put the new entry after
7067c478bd9Sstevel@tonic-gate 		 * the current value of t.
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		if (strcmp(t->n_name, np->n_name) < 0) {
7107c478bd9Sstevel@tonic-gate 			t->n_flink = np;
7117c478bd9Sstevel@tonic-gate 			np->n_blink = t;
7127c478bd9Sstevel@tonic-gate 			t = np;
7137c478bd9Sstevel@tonic-gate 			np = np->n_flink;
7147c478bd9Sstevel@tonic-gate 			t->n_flink = NIL;
7157c478bd9Sstevel@tonic-gate 			continue;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		/*
7197c478bd9Sstevel@tonic-gate 		 * Otherwise, put the new entry in front of the
7207c478bd9Sstevel@tonic-gate 		 * current t.  If at the front of the list,
7217c478bd9Sstevel@tonic-gate 		 * the new guy becomes the new head of the list.
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		if (t == newnames) {
7257c478bd9Sstevel@tonic-gate 			t = np;
7267c478bd9Sstevel@tonic-gate 			np = np->n_flink;
7277c478bd9Sstevel@tonic-gate 			t->n_flink = newnames;
7287c478bd9Sstevel@tonic-gate 			newnames->n_blink = t;
7297c478bd9Sstevel@tonic-gate 			t->n_blink = NIL;
7307c478bd9Sstevel@tonic-gate 			newnames = t;
7317c478bd9Sstevel@tonic-gate 			continue;
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/*
7357c478bd9Sstevel@tonic-gate 		 * The normal case -- we are inserting into the
7367c478bd9Sstevel@tonic-gate 		 * middle of the list.
7377c478bd9Sstevel@tonic-gate 		 */
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		x = np;
7407c478bd9Sstevel@tonic-gate 		np = np->n_flink;
7417c478bd9Sstevel@tonic-gate 		x->n_flink = t;
7427c478bd9Sstevel@tonic-gate 		x->n_blink = t->n_blink;
7437c478bd9Sstevel@tonic-gate 		t->n_blink->n_flink = x;
7447c478bd9Sstevel@tonic-gate 		t->n_blink = x;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 * Now the list headed up by new is sorted.
7497c478bd9Sstevel@tonic-gate 	 * Go through it and remove duplicates.
7507c478bd9Sstevel@tonic-gate 	 * Remember the best "type" among all the
7517c478bd9Sstevel@tonic-gate 	 * duplicates of a name.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	np = newnames;
7557c478bd9Sstevel@tonic-gate 	while (np != NIL) {
7567c478bd9Sstevel@tonic-gate 		int type;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		t = np;
7597c478bd9Sstevel@tonic-gate 		type = np->n_type;
760287247a8SAlexander Pyhalov 		while (t->n_flink != NIL &&
7617c478bd9Sstevel@tonic-gate 		    strcmp(np->n_name, t->n_flink->n_name) == 0) {
7627c478bd9Sstevel@tonic-gate 			t = t->n_flink;
7637c478bd9Sstevel@tonic-gate 			/* "To" before "Cc" before "Bcc" */
7647c478bd9Sstevel@tonic-gate 			if (t->n_type < type)
7657c478bd9Sstevel@tonic-gate 				type = t->n_type;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 		if (t == np || t == NIL) {
7687c478bd9Sstevel@tonic-gate 			np = np->n_flink;
7697c478bd9Sstevel@tonic-gate 			continue;
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * Now t points to the last entry with the same name
7747c478bd9Sstevel@tonic-gate 		 * as np.  Make np point beyond t.
7757c478bd9Sstevel@tonic-gate 		 */
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		np->n_flink = t->n_flink;
7787c478bd9Sstevel@tonic-gate 		if (t->n_flink != NIL)
7797c478bd9Sstevel@tonic-gate 			t->n_flink->n_blink = np;
7807c478bd9Sstevel@tonic-gate 		np->n_type = type;
7817c478bd9Sstevel@tonic-gate 		np = np->n_flink;
7827c478bd9Sstevel@tonic-gate 	}
783287247a8SAlexander Pyhalov 	return (newnames);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * Put another node onto a list of names and return
7887c478bd9Sstevel@tonic-gate  * the list.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate static struct name *
put(struct name * list,struct name * node)7927c478bd9Sstevel@tonic-gate put(struct name *list, struct name *node)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	node->n_flink = list;
7957c478bd9Sstevel@tonic-gate 	node->n_blink = NIL;
7967c478bd9Sstevel@tonic-gate 	if (list != NIL)
7977c478bd9Sstevel@tonic-gate 		list->n_blink = node;
798287247a8SAlexander Pyhalov 	return (node);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * Delete the given name from a namelist.
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate struct name *
delname(register struct name * np,char name[])8067c478bd9Sstevel@tonic-gate delname(register struct name *np, char name[])
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	register struct name *p;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	for (p = np; p != NIL; p = p->n_flink)
8117c478bd9Sstevel@tonic-gate 		if (samebody(name, p->n_name, FALSE)) {
8127c478bd9Sstevel@tonic-gate 			if (p->n_blink == NIL) {
8137c478bd9Sstevel@tonic-gate 				if (p->n_flink != NIL)
8147c478bd9Sstevel@tonic-gate 					p->n_flink->n_blink = NIL;
8157c478bd9Sstevel@tonic-gate 				np = p->n_flink;
8167c478bd9Sstevel@tonic-gate 				continue;
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 			if (p->n_flink == NIL) {
8197c478bd9Sstevel@tonic-gate 				if (p->n_blink != NIL)
8207c478bd9Sstevel@tonic-gate 					p->n_blink->n_flink = NIL;
8217c478bd9Sstevel@tonic-gate 				continue;
8227c478bd9Sstevel@tonic-gate 			}
8237c478bd9Sstevel@tonic-gate 			p->n_blink->n_flink = p->n_flink;
8247c478bd9Sstevel@tonic-gate 			p->n_flink->n_blink = p->n_blink;
8257c478bd9Sstevel@tonic-gate 		}
826287247a8SAlexander Pyhalov 	return (np);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * Call the given routine on each element of the name
8317c478bd9Sstevel@tonic-gate  * list, replacing said value if need be.
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate 
834287247a8SAlexander Pyhalov void
mapf(register struct name * np,char * from)8357c478bd9Sstevel@tonic-gate mapf(register struct name *np, char *from)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	register struct name *p;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (debug) fprintf(stderr, "mapf %lx, %s\n", (long)np, from);
8407c478bd9Sstevel@tonic-gate 	for (p = np; p != NIL; p = p->n_flink)
8417c478bd9Sstevel@tonic-gate 		if ((p->n_type & GDEL) == 0) {
8427c478bd9Sstevel@tonic-gate 			p->n_name = netmap(p->n_name, from);
8437c478bd9Sstevel@tonic-gate 			p->n_full = splice(p->n_name, p->n_full);
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 	if (debug) fprintf(stderr, "mapf %s done\n", from);
8467c478bd9Sstevel@tonic-gate }
847