19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 350c3a8314SMike Heffner #if 0 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93"; 370c3a8314SMike Heffner #endif 380c3a8314SMike Heffner static const char rcsid[] = 390c3a8314SMike Heffner "$FreeBSD$"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes /* 439b50d902SRodney W. Grimes * Mail -- a mail program 449b50d902SRodney W. Grimes * 459b50d902SRodney W. Grimes * Handle name lists. 469b50d902SRodney W. Grimes */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include "rcv.h" 499b50d902SRodney W. Grimes #include <fcntl.h> 509b50d902SRodney W. Grimes #include "extern.h" 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes /* 539b50d902SRodney W. Grimes * Allocate a single element of a name list, 549b50d902SRodney W. Grimes * initialize its name field to the passed 559b50d902SRodney W. Grimes * name and return it. 569b50d902SRodney W. Grimes */ 579b50d902SRodney W. Grimes struct name * 589b50d902SRodney W. Grimes nalloc(str, ntype) 599b50d902SRodney W. Grimes char str[]; 609b50d902SRodney W. Grimes int ntype; 619b50d902SRodney W. Grimes { 629b50d902SRodney W. Grimes register struct name *np; 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes np = (struct name *) salloc(sizeof *np); 659b50d902SRodney W. Grimes np->n_flink = NIL; 669b50d902SRodney W. Grimes np->n_blink = NIL; 679b50d902SRodney W. Grimes np->n_type = ntype; 689b50d902SRodney W. Grimes np->n_name = savestr(str); 699b50d902SRodney W. Grimes return(np); 709b50d902SRodney W. Grimes } 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes /* 739b50d902SRodney W. Grimes * Find the tail of a list and return it. 749b50d902SRodney W. Grimes */ 759b50d902SRodney W. Grimes struct name * 769b50d902SRodney W. Grimes tailof(name) 779b50d902SRodney W. Grimes struct name *name; 789b50d902SRodney W. Grimes { 799b50d902SRodney W. Grimes register struct name *np; 809b50d902SRodney W. Grimes 819b50d902SRodney W. Grimes np = name; 829b50d902SRodney W. Grimes if (np == NIL) 839b50d902SRodney W. Grimes return(NIL); 849b50d902SRodney W. Grimes while (np->n_flink != NIL) 859b50d902SRodney W. Grimes np = np->n_flink; 869b50d902SRodney W. Grimes return(np); 879b50d902SRodney W. Grimes } 889b50d902SRodney W. Grimes 899b50d902SRodney W. Grimes /* 909b50d902SRodney W. Grimes * Extract a list of names from a line, 919b50d902SRodney W. Grimes * and make a list of names from it. 929b50d902SRodney W. Grimes * Return the list or NIL if none found. 939b50d902SRodney W. Grimes */ 949b50d902SRodney W. Grimes struct name * 959b50d902SRodney W. Grimes extract(line, ntype) 969b50d902SRodney W. Grimes char line[]; 979b50d902SRodney W. Grimes int ntype; 989b50d902SRodney W. Grimes { 999b50d902SRodney W. Grimes register char *cp; 1009b50d902SRodney W. Grimes register struct name *top, *np, *t; 1010c3a8314SMike Heffner char *nbuf; 1029b50d902SRodney W. Grimes 1039b50d902SRodney W. Grimes if (line == NOSTR || *line == '\0') 1049b50d902SRodney W. Grimes return NIL; 1050c3a8314SMike Heffner if ((nbuf = (char *)malloc(strlen(line) + 1)) == NULL) 1060c3a8314SMike Heffner err(1, "Out of memory"); 1079b50d902SRodney W. Grimes top = NIL; 1089b50d902SRodney W. Grimes np = NIL; 1099b50d902SRodney W. Grimes cp = line; 1109b50d902SRodney W. Grimes while ((cp = yankword(cp, nbuf)) != NOSTR) { 1119b50d902SRodney W. Grimes t = nalloc(nbuf, ntype); 1129b50d902SRodney W. Grimes if (top == NIL) 1139b50d902SRodney W. Grimes top = t; 1149b50d902SRodney W. Grimes else 1159b50d902SRodney W. Grimes np->n_flink = t; 1169b50d902SRodney W. Grimes t->n_blink = np; 1179b50d902SRodney W. Grimes np = t; 1189b50d902SRodney W. Grimes } 1190c3a8314SMike Heffner free(nbuf); 1209b50d902SRodney W. Grimes return top; 1219b50d902SRodney W. Grimes } 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes /* 1249b50d902SRodney W. Grimes * Turn a list of names into a string of the same names. 1259b50d902SRodney W. Grimes */ 1269b50d902SRodney W. Grimes char * 1279b50d902SRodney W. Grimes detract(np, ntype) 1289b50d902SRodney W. Grimes register struct name *np; 1299b50d902SRodney W. Grimes int ntype; 1309b50d902SRodney W. Grimes { 1319b50d902SRodney W. Grimes register int s; 1329b50d902SRodney W. Grimes register char *cp, *top; 1339b50d902SRodney W. Grimes register struct name *p; 1349b50d902SRodney W. Grimes register int comma; 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes comma = ntype & GCOMMA; 1379b50d902SRodney W. Grimes if (np == NIL) 1389b50d902SRodney W. Grimes return(NOSTR); 1399b50d902SRodney W. Grimes ntype &= ~GCOMMA; 1409b50d902SRodney W. Grimes s = 0; 1419b50d902SRodney W. Grimes if (debug && comma) 1429b50d902SRodney W. Grimes fprintf(stderr, "detract asked to insert commas\n"); 1439b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) { 1449b50d902SRodney W. Grimes if (ntype && (p->n_type & GMASK) != ntype) 1459b50d902SRodney W. Grimes continue; 1469b50d902SRodney W. Grimes s += strlen(p->n_name) + 1; 1479b50d902SRodney W. Grimes if (comma) 1489b50d902SRodney W. Grimes s++; 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes if (s == 0) 1519b50d902SRodney W. Grimes return(NOSTR); 1529b50d902SRodney W. Grimes s += 2; 1539b50d902SRodney W. Grimes top = salloc(s); 1549b50d902SRodney W. Grimes cp = top; 1559b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) { 1569b50d902SRodney W. Grimes if (ntype && (p->n_type & GMASK) != ntype) 1579b50d902SRodney W. Grimes continue; 1580c3a8314SMike Heffner cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1); 1599b50d902SRodney W. Grimes if (comma && p->n_flink != NIL) 1609b50d902SRodney W. Grimes *cp++ = ','; 1619b50d902SRodney W. Grimes *cp++ = ' '; 1629b50d902SRodney W. Grimes } 1630c3a8314SMike Heffner *--cp = '\0'; 1649b50d902SRodney W. Grimes if (comma && *--cp == ',') 1650c3a8314SMike Heffner *cp = '\0'; 1669b50d902SRodney W. Grimes return(top); 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes 1699b50d902SRodney W. Grimes /* 1709b50d902SRodney W. Grimes * Grab a single word (liberal word) 1719b50d902SRodney W. Grimes * Throw away things between ()'s, and take anything between <>. 1729b50d902SRodney W. Grimes */ 1739b50d902SRodney W. Grimes char * 1749b50d902SRodney W. Grimes yankword(ap, wbuf) 1759b50d902SRodney W. Grimes char *ap, wbuf[]; 1769b50d902SRodney W. Grimes { 1779b50d902SRodney W. Grimes register char *cp, *cp2; 1789b50d902SRodney W. Grimes 1799b50d902SRodney W. Grimes cp = ap; 1809b50d902SRodney W. Grimes for (;;) { 1819b50d902SRodney W. Grimes if (*cp == '\0') 1829b50d902SRodney W. Grimes return NOSTR; 1839b50d902SRodney W. Grimes if (*cp == '(') { 1849b50d902SRodney W. Grimes register int nesting = 0; 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes while (*cp != '\0') { 1879b50d902SRodney W. Grimes switch (*cp++) { 1889b50d902SRodney W. Grimes case '(': 1899b50d902SRodney W. Grimes nesting++; 1909b50d902SRodney W. Grimes break; 1919b50d902SRodney W. Grimes case ')': 1929b50d902SRodney W. Grimes --nesting; 1939b50d902SRodney W. Grimes break; 1949b50d902SRodney W. Grimes } 1959b50d902SRodney W. Grimes if (nesting <= 0) 1969b50d902SRodney W. Grimes break; 1979b50d902SRodney W. Grimes } 1989b50d902SRodney W. Grimes } else if (*cp == ' ' || *cp == '\t' || *cp == ',') 1999b50d902SRodney W. Grimes cp++; 2009b50d902SRodney W. Grimes else 2019b50d902SRodney W. Grimes break; 2029b50d902SRodney W. Grimes } 2039b50d902SRodney W. Grimes if (*cp == '<') 2049b50d902SRodney W. Grimes for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';) 2059b50d902SRodney W. Grimes ; 2069b50d902SRodney W. Grimes else 2070c3a8314SMike Heffner for (cp2 = wbuf; *cp && !strchr(" \t,(", *cp); *cp2++ = *cp++) 2089b50d902SRodney W. Grimes ; 2099b50d902SRodney W. Grimes *cp2 = '\0'; 2109b50d902SRodney W. Grimes return cp; 2119b50d902SRodney W. Grimes } 2129b50d902SRodney W. Grimes 2139b50d902SRodney W. Grimes /* 2149b50d902SRodney W. Grimes * For each recipient in the passed name list with a / 2159b50d902SRodney W. Grimes * in the name, append the message to the end of the named file 2169b50d902SRodney W. Grimes * and remove him from the recipient list. 2179b50d902SRodney W. Grimes * 2189b50d902SRodney W. Grimes * Recipients whose name begins with | are piped through the given 2199b50d902SRodney W. Grimes * program and removed. 2209b50d902SRodney W. Grimes */ 2219b50d902SRodney W. Grimes struct name * 2229b50d902SRodney W. Grimes outof(names, fo, hp) 2239b50d902SRodney W. Grimes struct name *names; 2249b50d902SRodney W. Grimes FILE *fo; 2259b50d902SRodney W. Grimes struct header *hp; 2269b50d902SRodney W. Grimes { 2279b50d902SRodney W. Grimes register int c; 2289b50d902SRodney W. Grimes register struct name *np, *top; 2299b50d902SRodney W. Grimes time_t now, time(); 2309b50d902SRodney W. Grimes char *date, *fname, *ctime(); 2319b50d902SRodney W. Grimes FILE *fout, *fin; 2329b50d902SRodney W. Grimes int ispipe; 2339b50d902SRodney W. Grimes 2349b50d902SRodney W. Grimes top = names; 2359b50d902SRodney W. Grimes np = names; 2369b50d902SRodney W. Grimes (void) time(&now); 2379b50d902SRodney W. Grimes date = ctime(&now); 2389b50d902SRodney W. Grimes while (np != NIL) { 2399b50d902SRodney W. Grimes if (!isfileaddr(np->n_name) && np->n_name[0] != '|') { 2409b50d902SRodney W. Grimes np = np->n_flink; 2419b50d902SRodney W. Grimes continue; 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes ispipe = np->n_name[0] == '|'; 2449b50d902SRodney W. Grimes if (ispipe) 2459b50d902SRodney W. Grimes fname = np->n_name+1; 2469b50d902SRodney W. Grimes else 2479b50d902SRodney W. Grimes fname = expand(np->n_name); 2489b50d902SRodney W. Grimes 2499b50d902SRodney W. Grimes /* 2509b50d902SRodney W. Grimes * See if we have copied the complete message out yet. 2519b50d902SRodney W. Grimes * If not, do so. 2529b50d902SRodney W. Grimes */ 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes if (image < 0) { 2550c3a8314SMike Heffner int fd; 2560c3a8314SMike Heffner char tempname[PATHSIZE]; 2570c3a8314SMike Heffner 2580c3a8314SMike Heffner snprintf(tempname, sizeof(tempname), 2590c3a8314SMike Heffner "%s/mail.ReXXXXXXXXXX", tmpdir); 2600c3a8314SMike Heffner if ((fd = mkstemp(tempname)) == -1 || 2610c3a8314SMike Heffner (fout = Fdopen(fd, "a")) == NULL) { 2620c3a8314SMike Heffner warn("%s", tempname); 2639b50d902SRodney W. Grimes senderr++; 2649b50d902SRodney W. Grimes goto cant; 2659b50d902SRodney W. Grimes } 2660c3a8314SMike Heffner image = open(tempname, O_RDWR); 2670c3a8314SMike Heffner (void) rm(tempname); 2689b50d902SRodney W. Grimes if (image < 0) { 2690c3a8314SMike Heffner warn("%s", tempname); 2709b50d902SRodney W. Grimes senderr++; 2719b50d902SRodney W. Grimes (void) Fclose(fout); 2729b50d902SRodney W. Grimes goto cant; 2739b50d902SRodney W. Grimes } 2749b50d902SRodney W. Grimes (void) fcntl(image, F_SETFD, 1); 2759b50d902SRodney W. Grimes fprintf(fout, "From %s %s", myname, date); 27699bd6601SJoerg Wunsch puthead(hp, fout, 27799bd6601SJoerg Wunsch GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL); 2789b50d902SRodney W. Grimes while ((c = getc(fo)) != EOF) 2799b50d902SRodney W. Grimes (void) putc(c, fout); 2809b50d902SRodney W. Grimes rewind(fo); 2819b50d902SRodney W. Grimes (void) putc('\n', fout); 2829b50d902SRodney W. Grimes (void) fflush(fout); 2830c3a8314SMike Heffner if (ferror(fout)) { 2840c3a8314SMike Heffner warn("%s", tempname); 2850c3a8314SMike Heffner senderr++; 2860c3a8314SMike Heffner Fclose(fout); 2870c3a8314SMike Heffner goto cant; 2880c3a8314SMike Heffner } 2899b50d902SRodney W. Grimes (void) Fclose(fout); 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes 2929b50d902SRodney W. Grimes /* 2939b50d902SRodney W. Grimes * Now either copy "image" to the desired file 2949b50d902SRodney W. Grimes * or give it as the standard input to the desired 2959b50d902SRodney W. Grimes * program as appropriate. 2969b50d902SRodney W. Grimes */ 2979b50d902SRodney W. Grimes 2989b50d902SRodney W. Grimes if (ispipe) { 2999b50d902SRodney W. Grimes int pid; 3009b50d902SRodney W. Grimes char *shell; 3019b50d902SRodney W. Grimes 3029b50d902SRodney W. Grimes /* 3039b50d902SRodney W. Grimes * XXX 3049b50d902SRodney W. Grimes * We can't really reuse the same image file, 3059b50d902SRodney W. Grimes * because multiple piped recipients will 3069b50d902SRodney W. Grimes * share the same lseek location and trample 3079b50d902SRodney W. Grimes * on one another. 3089b50d902SRodney W. Grimes */ 3099b50d902SRodney W. Grimes if ((shell = value("SHELL")) == NOSTR) 3109b50d902SRodney W. Grimes shell = _PATH_CSHELL; 3119b50d902SRodney W. Grimes pid = start_command(shell, sigmask(SIGHUP)| 3129b50d902SRodney W. Grimes sigmask(SIGINT)|sigmask(SIGQUIT), 3139b50d902SRodney W. Grimes image, -1, "-c", fname, NOSTR); 3149b50d902SRodney W. Grimes if (pid < 0) { 3159b50d902SRodney W. Grimes senderr++; 3169b50d902SRodney W. Grimes goto cant; 3179b50d902SRodney W. Grimes } 3189b50d902SRodney W. Grimes free_child(pid); 3199b50d902SRodney W. Grimes } else { 3209b50d902SRodney W. Grimes int f; 3219b50d902SRodney W. Grimes if ((fout = Fopen(fname, "a")) == NULL) { 3220c3a8314SMike Heffner warn("%s", fname); 3239b50d902SRodney W. Grimes senderr++; 3249b50d902SRodney W. Grimes goto cant; 3259b50d902SRodney W. Grimes } 3269b50d902SRodney W. Grimes if ((f = dup(image)) < 0) { 3270c3a8314SMike Heffner warn("dup"); 3289b50d902SRodney W. Grimes fin = NULL; 3299b50d902SRodney W. Grimes } else 3309b50d902SRodney W. Grimes fin = Fdopen(f, "r"); 3319b50d902SRodney W. Grimes if (fin == NULL) { 3329b50d902SRodney W. Grimes fprintf(stderr, "Can't reopen image\n"); 3339b50d902SRodney W. Grimes (void) Fclose(fout); 3349b50d902SRodney W. Grimes senderr++; 3359b50d902SRodney W. Grimes goto cant; 3369b50d902SRodney W. Grimes } 3379b50d902SRodney W. Grimes rewind(fin); 3389b50d902SRodney W. Grimes while ((c = getc(fin)) != EOF) 3399b50d902SRodney W. Grimes (void) putc(c, fout); 3400c3a8314SMike Heffner if (ferror(fout)) { 3410c3a8314SMike Heffner warnx("%s", fname); 3420c3a8314SMike Heffner senderr++; 3430c3a8314SMike Heffner Fclose(fout); 3440c3a8314SMike Heffner Fclose(fin); 3450c3a8314SMike Heffner goto cant; 3460c3a8314SMike Heffner } 3479b50d902SRodney W. Grimes (void) Fclose(fout); 3489b50d902SRodney W. Grimes (void) Fclose(fin); 3499b50d902SRodney W. Grimes } 3509b50d902SRodney W. Grimes cant: 3519b50d902SRodney W. Grimes /* 3529b50d902SRodney W. Grimes * In days of old we removed the entry from the 3539b50d902SRodney W. Grimes * the list; now for sake of header expansion 3549b50d902SRodney W. Grimes * we leave it in and mark it as deleted. 3559b50d902SRodney W. Grimes */ 3569b50d902SRodney W. Grimes np->n_type |= GDEL; 3579b50d902SRodney W. Grimes np = np->n_flink; 3589b50d902SRodney W. Grimes } 3599b50d902SRodney W. Grimes if (image >= 0) { 3609b50d902SRodney W. Grimes (void) close(image); 3619b50d902SRodney W. Grimes image = -1; 3629b50d902SRodney W. Grimes } 3639b50d902SRodney W. Grimes return(top); 3649b50d902SRodney W. Grimes } 3659b50d902SRodney W. Grimes 3669b50d902SRodney W. Grimes /* 3679b50d902SRodney W. Grimes * Determine if the passed address is a local "send to file" address. 3689b50d902SRodney W. Grimes * If any of the network metacharacters precedes any slashes, it can't 3699b50d902SRodney W. Grimes * be a filename. We cheat with .'s to allow path names like ./... 3709b50d902SRodney W. Grimes */ 3719b50d902SRodney W. Grimes int 3729b50d902SRodney W. Grimes isfileaddr(name) 3739b50d902SRodney W. Grimes char *name; 3749b50d902SRodney W. Grimes { 3759b50d902SRodney W. Grimes register char *cp; 3769b50d902SRodney W. Grimes 3779b50d902SRodney W. Grimes if (*name == '+') 3789b50d902SRodney W. Grimes return 1; 3799b50d902SRodney W. Grimes for (cp = name; *cp; cp++) { 3809b50d902SRodney W. Grimes if (*cp == '!' || *cp == '%' || *cp == '@') 3819b50d902SRodney W. Grimes return 0; 3829b50d902SRodney W. Grimes if (*cp == '/') 3839b50d902SRodney W. Grimes return 1; 3849b50d902SRodney W. Grimes } 3859b50d902SRodney W. Grimes return 0; 3869b50d902SRodney W. Grimes } 3879b50d902SRodney W. Grimes 3889b50d902SRodney W. Grimes /* 3899b50d902SRodney W. Grimes * Map all of the aliased users in the invoker's mailrc 3909b50d902SRodney W. Grimes * file and insert them into the list. 3919b50d902SRodney W. Grimes * Changed after all these months of service to recursively 3929b50d902SRodney W. Grimes * expand names (2/14/80). 3939b50d902SRodney W. Grimes */ 3949b50d902SRodney W. Grimes 3959b50d902SRodney W. Grimes struct name * 3969b50d902SRodney W. Grimes usermap(names) 3979b50d902SRodney W. Grimes struct name *names; 3989b50d902SRodney W. Grimes { 3999b50d902SRodney W. Grimes register struct name *new, *np, *cp; 4009b50d902SRodney W. Grimes struct grouphead *gh; 4019b50d902SRodney W. Grimes register int metoo; 4029b50d902SRodney W. Grimes 4039b50d902SRodney W. Grimes new = NIL; 4049b50d902SRodney W. Grimes np = names; 4059b50d902SRodney W. Grimes metoo = (value("metoo") != NOSTR); 4069b50d902SRodney W. Grimes while (np != NIL) { 4079b50d902SRodney W. Grimes if (np->n_name[0] == '\\') { 4089b50d902SRodney W. Grimes cp = np->n_flink; 4099b50d902SRodney W. Grimes new = put(new, np); 4109b50d902SRodney W. Grimes np = cp; 4119b50d902SRodney W. Grimes continue; 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes gh = findgroup(np->n_name); 4149b50d902SRodney W. Grimes cp = np->n_flink; 4159b50d902SRodney W. Grimes if (gh != NOGRP) 4169b50d902SRodney W. Grimes new = gexpand(new, gh, metoo, np->n_type); 4179b50d902SRodney W. Grimes else 4189b50d902SRodney W. Grimes new = put(new, np); 4199b50d902SRodney W. Grimes np = cp; 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes return(new); 4229b50d902SRodney W. Grimes } 4239b50d902SRodney W. Grimes 4249b50d902SRodney W. Grimes /* 4259b50d902SRodney W. Grimes * Recursively expand a group name. We limit the expansion to some 4269b50d902SRodney W. Grimes * fixed level to keep things from going haywire. 4279b50d902SRodney W. Grimes * Direct recursion is not expanded for convenience. 4289b50d902SRodney W. Grimes */ 4299b50d902SRodney W. Grimes 4309b50d902SRodney W. Grimes struct name * 4319b50d902SRodney W. Grimes gexpand(nlist, gh, metoo, ntype) 4329b50d902SRodney W. Grimes struct name *nlist; 4339b50d902SRodney W. Grimes struct grouphead *gh; 4349b50d902SRodney W. Grimes int metoo, ntype; 4359b50d902SRodney W. Grimes { 4369b50d902SRodney W. Grimes struct group *gp; 4379b50d902SRodney W. Grimes struct grouphead *ngh; 4389b50d902SRodney W. Grimes struct name *np; 4399b50d902SRodney W. Grimes static int depth; 4409b50d902SRodney W. Grimes char *cp; 4419b50d902SRodney W. Grimes 4429b50d902SRodney W. Grimes if (depth > MAXEXP) { 4439b50d902SRodney W. Grimes printf("Expanding alias to depth larger than %d\n", MAXEXP); 4449b50d902SRodney W. Grimes return(nlist); 4459b50d902SRodney W. Grimes } 4469b50d902SRodney W. Grimes depth++; 4479b50d902SRodney W. Grimes for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) { 4489b50d902SRodney W. Grimes cp = gp->ge_name; 4499b50d902SRodney W. Grimes if (*cp == '\\') 4509b50d902SRodney W. Grimes goto quote; 4519b50d902SRodney W. Grimes if (strcmp(cp, gh->g_name) == 0) 4529b50d902SRodney W. Grimes goto quote; 4539b50d902SRodney W. Grimes if ((ngh = findgroup(cp)) != NOGRP) { 4549b50d902SRodney W. Grimes nlist = gexpand(nlist, ngh, metoo, ntype); 4559b50d902SRodney W. Grimes continue; 4569b50d902SRodney W. Grimes } 4579b50d902SRodney W. Grimes quote: 4589b50d902SRodney W. Grimes np = nalloc(cp, ntype); 4599b50d902SRodney W. Grimes /* 4609b50d902SRodney W. Grimes * At this point should allow to expand 4619b50d902SRodney W. Grimes * to self if only person in group 4629b50d902SRodney W. Grimes */ 4639b50d902SRodney W. Grimes if (gp == gh->g_list && gp->ge_link == NOGE) 4649b50d902SRodney W. Grimes goto skip; 4659b50d902SRodney W. Grimes if (!metoo && strcmp(cp, myname) == 0) 4669b50d902SRodney W. Grimes np->n_type |= GDEL; 4679b50d902SRodney W. Grimes skip: 4689b50d902SRodney W. Grimes nlist = put(nlist, np); 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes depth--; 4719b50d902SRodney W. Grimes return(nlist); 4729b50d902SRodney W. Grimes } 4739b50d902SRodney W. Grimes 4749b50d902SRodney W. Grimes /* 4759b50d902SRodney W. Grimes * Concatenate the two passed name lists, return the result. 4769b50d902SRodney W. Grimes */ 4779b50d902SRodney W. Grimes struct name * 4789b50d902SRodney W. Grimes cat(n1, n2) 4799b50d902SRodney W. Grimes struct name *n1, *n2; 4809b50d902SRodney W. Grimes { 4819b50d902SRodney W. Grimes register struct name *tail; 4829b50d902SRodney W. Grimes 4839b50d902SRodney W. Grimes if (n1 == NIL) 4849b50d902SRodney W. Grimes return(n2); 4859b50d902SRodney W. Grimes if (n2 == NIL) 4869b50d902SRodney W. Grimes return(n1); 4879b50d902SRodney W. Grimes tail = tailof(n1); 4889b50d902SRodney W. Grimes tail->n_flink = n2; 4899b50d902SRodney W. Grimes n2->n_blink = tail; 4909b50d902SRodney W. Grimes return(n1); 4919b50d902SRodney W. Grimes } 4929b50d902SRodney W. Grimes 4939b50d902SRodney W. Grimes /* 4949b50d902SRodney W. Grimes * Unpack the name list onto a vector of strings. 4959b50d902SRodney W. Grimes * Return an error if the name list won't fit. 4969b50d902SRodney W. Grimes */ 4979b50d902SRodney W. Grimes char ** 4989b50d902SRodney W. Grimes unpack(np) 4999b50d902SRodney W. Grimes struct name *np; 5009b50d902SRodney W. Grimes { 5019b50d902SRodney W. Grimes register char **ap, **top; 5029b50d902SRodney W. Grimes register struct name *n; 5039b50d902SRodney W. Grimes int t, extra, metoo, verbose; 5049b50d902SRodney W. Grimes 5059b50d902SRodney W. Grimes n = np; 5069b50d902SRodney W. Grimes if ((t = count(n)) == 0) 5070c3a8314SMike Heffner errx(1, "No names to unpack"); 5089b50d902SRodney W. Grimes /* 5099b50d902SRodney W. Grimes * Compute the number of extra arguments we will need. 5109b50d902SRodney W. Grimes * We need at least two extra -- one for "mail" and one for 5119b50d902SRodney W. Grimes * the terminating 0 pointer. Additional spots may be needed 5129b50d902SRodney W. Grimes * to pass along -f to the host mailer. 5139b50d902SRodney W. Grimes */ 5149b50d902SRodney W. Grimes extra = 2; 5159b50d902SRodney W. Grimes extra++; 5169b50d902SRodney W. Grimes metoo = value("metoo") != NOSTR; 5179b50d902SRodney W. Grimes if (metoo) 5189b50d902SRodney W. Grimes extra++; 5199b50d902SRodney W. Grimes verbose = value("verbose") != NOSTR; 5209b50d902SRodney W. Grimes if (verbose) 5219b50d902SRodney W. Grimes extra++; 5229b50d902SRodney W. Grimes top = (char **) salloc((t + extra) * sizeof *top); 5239b50d902SRodney W. Grimes ap = top; 5249b50d902SRodney W. Grimes *ap++ = "send-mail"; 5259b50d902SRodney W. Grimes *ap++ = "-i"; 5269b50d902SRodney W. Grimes if (metoo) 5279b50d902SRodney W. Grimes *ap++ = "-m"; 5289b50d902SRodney W. Grimes if (verbose) 5299b50d902SRodney W. Grimes *ap++ = "-v"; 5309b50d902SRodney W. Grimes for (; n != NIL; n = n->n_flink) 5319b50d902SRodney W. Grimes if ((n->n_type & GDEL) == 0) 5329b50d902SRodney W. Grimes *ap++ = n->n_name; 5339b50d902SRodney W. Grimes *ap = NOSTR; 5349b50d902SRodney W. Grimes return(top); 5359b50d902SRodney W. Grimes } 5369b50d902SRodney W. Grimes 5379b50d902SRodney W. Grimes /* 5389b50d902SRodney W. Grimes * Remove all of the duplicates from the passed name list by 5399b50d902SRodney W. Grimes * insertion sorting them, then checking for dups. 5409b50d902SRodney W. Grimes * Return the head of the new list. 5419b50d902SRodney W. Grimes */ 5429b50d902SRodney W. Grimes struct name * 5439b50d902SRodney W. Grimes elide(names) 5449b50d902SRodney W. Grimes struct name *names; 5459b50d902SRodney W. Grimes { 5469b50d902SRodney W. Grimes register struct name *np, *t, *new; 5479b50d902SRodney W. Grimes struct name *x; 5489b50d902SRodney W. Grimes 5499b50d902SRodney W. Grimes if (names == NIL) 5509b50d902SRodney W. Grimes return(NIL); 5519b50d902SRodney W. Grimes new = names; 5529b50d902SRodney W. Grimes np = names; 5539b50d902SRodney W. Grimes np = np->n_flink; 5549b50d902SRodney W. Grimes if (np != NIL) 5559b50d902SRodney W. Grimes np->n_blink = NIL; 5569b50d902SRodney W. Grimes new->n_flink = NIL; 5579b50d902SRodney W. Grimes while (np != NIL) { 5589b50d902SRodney W. Grimes t = new; 5599b50d902SRodney W. Grimes while (strcasecmp(t->n_name, np->n_name) < 0) { 5609b50d902SRodney W. Grimes if (t->n_flink == NIL) 5619b50d902SRodney W. Grimes break; 5629b50d902SRodney W. Grimes t = t->n_flink; 5639b50d902SRodney W. Grimes } 5649b50d902SRodney W. Grimes 5659b50d902SRodney W. Grimes /* 5669b50d902SRodney W. Grimes * If we ran out of t's, put the new entry after 5679b50d902SRodney W. Grimes * the current value of t. 5689b50d902SRodney W. Grimes */ 5699b50d902SRodney W. Grimes 5709b50d902SRodney W. Grimes if (strcasecmp(t->n_name, np->n_name) < 0) { 5719b50d902SRodney W. Grimes t->n_flink = np; 5729b50d902SRodney W. Grimes np->n_blink = t; 5739b50d902SRodney W. Grimes t = np; 5749b50d902SRodney W. Grimes np = np->n_flink; 5759b50d902SRodney W. Grimes t->n_flink = NIL; 5769b50d902SRodney W. Grimes continue; 5779b50d902SRodney W. Grimes } 5789b50d902SRodney W. Grimes 5799b50d902SRodney W. Grimes /* 5809b50d902SRodney W. Grimes * Otherwise, put the new entry in front of the 5819b50d902SRodney W. Grimes * current t. If at the front of the list, 5829b50d902SRodney W. Grimes * the new guy becomes the new head of the list. 5839b50d902SRodney W. Grimes */ 5849b50d902SRodney W. Grimes 5859b50d902SRodney W. Grimes if (t == new) { 5869b50d902SRodney W. Grimes t = np; 5879b50d902SRodney W. Grimes np = np->n_flink; 5889b50d902SRodney W. Grimes t->n_flink = new; 5899b50d902SRodney W. Grimes new->n_blink = t; 5909b50d902SRodney W. Grimes t->n_blink = NIL; 5919b50d902SRodney W. Grimes new = t; 5929b50d902SRodney W. Grimes continue; 5939b50d902SRodney W. Grimes } 5949b50d902SRodney W. Grimes 5959b50d902SRodney W. Grimes /* 5969b50d902SRodney W. Grimes * The normal case -- we are inserting into the 5979b50d902SRodney W. Grimes * middle of the list. 5989b50d902SRodney W. Grimes */ 5999b50d902SRodney W. Grimes 6009b50d902SRodney W. Grimes x = np; 6019b50d902SRodney W. Grimes np = np->n_flink; 6029b50d902SRodney W. Grimes x->n_flink = t; 6039b50d902SRodney W. Grimes x->n_blink = t->n_blink; 6049b50d902SRodney W. Grimes t->n_blink->n_flink = x; 6059b50d902SRodney W. Grimes t->n_blink = x; 6069b50d902SRodney W. Grimes } 6079b50d902SRodney W. Grimes 6089b50d902SRodney W. Grimes /* 6099b50d902SRodney W. Grimes * Now the list headed up by new is sorted. 6109b50d902SRodney W. Grimes * Go through it and remove duplicates. 6119b50d902SRodney W. Grimes */ 6129b50d902SRodney W. Grimes 6139b50d902SRodney W. Grimes np = new; 6149b50d902SRodney W. Grimes while (np != NIL) { 6159b50d902SRodney W. Grimes t = np; 6169b50d902SRodney W. Grimes while (t->n_flink != NIL && 6179b50d902SRodney W. Grimes strcasecmp(np->n_name, t->n_flink->n_name) == 0) 6189b50d902SRodney W. Grimes t = t->n_flink; 6199b50d902SRodney W. Grimes if (t == np || t == NIL) { 6209b50d902SRodney W. Grimes np = np->n_flink; 6219b50d902SRodney W. Grimes continue; 6229b50d902SRodney W. Grimes } 6239b50d902SRodney W. Grimes 6249b50d902SRodney W. Grimes /* 6259b50d902SRodney W. Grimes * Now t points to the last entry with the same name 6269b50d902SRodney W. Grimes * as np. Make np point beyond t. 6279b50d902SRodney W. Grimes */ 6289b50d902SRodney W. Grimes 6299b50d902SRodney W. Grimes np->n_flink = t->n_flink; 6309b50d902SRodney W. Grimes if (t->n_flink != NIL) 6319b50d902SRodney W. Grimes t->n_flink->n_blink = np; 6329b50d902SRodney W. Grimes np = np->n_flink; 6339b50d902SRodney W. Grimes } 6349b50d902SRodney W. Grimes return(new); 6359b50d902SRodney W. Grimes } 6369b50d902SRodney W. Grimes 6379b50d902SRodney W. Grimes /* 6389b50d902SRodney W. Grimes * Put another node onto a list of names and return 6399b50d902SRodney W. Grimes * the list. 6409b50d902SRodney W. Grimes */ 6419b50d902SRodney W. Grimes struct name * 6429b50d902SRodney W. Grimes put(list, node) 6439b50d902SRodney W. Grimes struct name *list, *node; 6449b50d902SRodney W. Grimes { 6459b50d902SRodney W. Grimes node->n_flink = list; 6469b50d902SRodney W. Grimes node->n_blink = NIL; 6479b50d902SRodney W. Grimes if (list != NIL) 6489b50d902SRodney W. Grimes list->n_blink = node; 6499b50d902SRodney W. Grimes return(node); 6509b50d902SRodney W. Grimes } 6519b50d902SRodney W. Grimes 6529b50d902SRodney W. Grimes /* 6539b50d902SRodney W. Grimes * Determine the number of undeleted elements in 6549b50d902SRodney W. Grimes * a name list and return it. 6559b50d902SRodney W. Grimes */ 6569b50d902SRodney W. Grimes int 6579b50d902SRodney W. Grimes count(np) 6589b50d902SRodney W. Grimes register struct name *np; 6599b50d902SRodney W. Grimes { 6609b50d902SRodney W. Grimes register int c; 6619b50d902SRodney W. Grimes 6629b50d902SRodney W. Grimes for (c = 0; np != NIL; np = np->n_flink) 6639b50d902SRodney W. Grimes if ((np->n_type & GDEL) == 0) 6649b50d902SRodney W. Grimes c++; 6659b50d902SRodney W. Grimes return c; 6669b50d902SRodney W. Grimes } 6679b50d902SRodney W. Grimes 6689b50d902SRodney W. Grimes /* 6699b50d902SRodney W. Grimes * Delete the given name from a namelist. 6709b50d902SRodney W. Grimes */ 6719b50d902SRodney W. Grimes struct name * 6729b50d902SRodney W. Grimes delname(np, name) 6739b50d902SRodney W. Grimes register struct name *np; 6749b50d902SRodney W. Grimes char name[]; 6759b50d902SRodney W. Grimes { 6769b50d902SRodney W. Grimes register struct name *p; 6779b50d902SRodney W. Grimes 6789b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) 6799b50d902SRodney W. Grimes if (strcasecmp(p->n_name, name) == 0) { 6809b50d902SRodney W. Grimes if (p->n_blink == NIL) { 6819b50d902SRodney W. Grimes if (p->n_flink != NIL) 6829b50d902SRodney W. Grimes p->n_flink->n_blink = NIL; 6839b50d902SRodney W. Grimes np = p->n_flink; 6849b50d902SRodney W. Grimes continue; 6859b50d902SRodney W. Grimes } 6869b50d902SRodney W. Grimes if (p->n_flink == NIL) { 6879b50d902SRodney W. Grimes if (p->n_blink != NIL) 6889b50d902SRodney W. Grimes p->n_blink->n_flink = NIL; 6899b50d902SRodney W. Grimes continue; 6909b50d902SRodney W. Grimes } 6919b50d902SRodney W. Grimes p->n_blink->n_flink = p->n_flink; 6929b50d902SRodney W. Grimes p->n_flink->n_blink = p->n_blink; 6939b50d902SRodney W. Grimes } 6949b50d902SRodney W. Grimes return np; 6959b50d902SRodney W. Grimes } 6969b50d902SRodney W. Grimes 6979b50d902SRodney W. Grimes /* 6989b50d902SRodney W. Grimes * Pretty print a name list 6999b50d902SRodney W. Grimes * Uncomment it if you need it. 7009b50d902SRodney W. Grimes */ 7019b50d902SRodney W. Grimes 7029b50d902SRodney W. Grimes /* 7039b50d902SRodney W. Grimes void 7049b50d902SRodney W. Grimes prettyprint(name) 7059b50d902SRodney W. Grimes struct name *name; 7069b50d902SRodney W. Grimes { 7079b50d902SRodney W. Grimes register struct name *np; 7089b50d902SRodney W. Grimes 7099b50d902SRodney W. Grimes np = name; 7109b50d902SRodney W. Grimes while (np != NIL) { 7119b50d902SRodney W. Grimes fprintf(stderr, "%s(%d) ", np->n_name, np->n_type); 7129b50d902SRodney W. Grimes np = np->n_flink; 7139b50d902SRodney W. Grimes } 7149b50d902SRodney W. Grimes fprintf(stderr, "\n"); 7159b50d902SRodney W. Grimes } 7169b50d902SRodney W. Grimes */ 717