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 359b50d902SRodney W. Grimes static char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes /* 399b50d902SRodney W. Grimes * Mail -- a mail program 409b50d902SRodney W. Grimes * 419b50d902SRodney W. Grimes * Handle name lists. 429b50d902SRodney W. Grimes */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include "rcv.h" 459b50d902SRodney W. Grimes #include <fcntl.h> 469b50d902SRodney W. Grimes #include "extern.h" 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes /* 499b50d902SRodney W. Grimes * Allocate a single element of a name list, 509b50d902SRodney W. Grimes * initialize its name field to the passed 519b50d902SRodney W. Grimes * name and return it. 529b50d902SRodney W. Grimes */ 539b50d902SRodney W. Grimes struct name * 549b50d902SRodney W. Grimes nalloc(str, ntype) 559b50d902SRodney W. Grimes char str[]; 569b50d902SRodney W. Grimes int ntype; 579b50d902SRodney W. Grimes { 589b50d902SRodney W. Grimes register struct name *np; 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes np = (struct name *) salloc(sizeof *np); 619b50d902SRodney W. Grimes np->n_flink = NIL; 629b50d902SRodney W. Grimes np->n_blink = NIL; 639b50d902SRodney W. Grimes np->n_type = ntype; 649b50d902SRodney W. Grimes np->n_name = savestr(str); 659b50d902SRodney W. Grimes return(np); 669b50d902SRodney W. Grimes } 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes /* 699b50d902SRodney W. Grimes * Find the tail of a list and return it. 709b50d902SRodney W. Grimes */ 719b50d902SRodney W. Grimes struct name * 729b50d902SRodney W. Grimes tailof(name) 739b50d902SRodney W. Grimes struct name *name; 749b50d902SRodney W. Grimes { 759b50d902SRodney W. Grimes register struct name *np; 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes np = name; 789b50d902SRodney W. Grimes if (np == NIL) 799b50d902SRodney W. Grimes return(NIL); 809b50d902SRodney W. Grimes while (np->n_flink != NIL) 819b50d902SRodney W. Grimes np = np->n_flink; 829b50d902SRodney W. Grimes return(np); 839b50d902SRodney W. Grimes } 849b50d902SRodney W. Grimes 859b50d902SRodney W. Grimes /* 869b50d902SRodney W. Grimes * Extract a list of names from a line, 879b50d902SRodney W. Grimes * and make a list of names from it. 889b50d902SRodney W. Grimes * Return the list or NIL if none found. 899b50d902SRodney W. Grimes */ 909b50d902SRodney W. Grimes struct name * 919b50d902SRodney W. Grimes extract(line, ntype) 929b50d902SRodney W. Grimes char line[]; 939b50d902SRodney W. Grimes int ntype; 949b50d902SRodney W. Grimes { 959b50d902SRodney W. Grimes register char *cp; 969b50d902SRodney W. Grimes register struct name *top, *np, *t; 979b50d902SRodney W. Grimes char nbuf[BUFSIZ]; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes if (line == NOSTR || *line == '\0') 1009b50d902SRodney W. Grimes return NIL; 1019b50d902SRodney W. Grimes top = NIL; 1029b50d902SRodney W. Grimes np = NIL; 1039b50d902SRodney W. Grimes cp = line; 1049b50d902SRodney W. Grimes while ((cp = yankword(cp, nbuf)) != NOSTR) { 1059b50d902SRodney W. Grimes t = nalloc(nbuf, ntype); 1069b50d902SRodney W. Grimes if (top == NIL) 1079b50d902SRodney W. Grimes top = t; 1089b50d902SRodney W. Grimes else 1099b50d902SRodney W. Grimes np->n_flink = t; 1109b50d902SRodney W. Grimes t->n_blink = np; 1119b50d902SRodney W. Grimes np = t; 1129b50d902SRodney W. Grimes } 1139b50d902SRodney W. Grimes return top; 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes /* 1179b50d902SRodney W. Grimes * Turn a list of names into a string of the same names. 1189b50d902SRodney W. Grimes */ 1199b50d902SRodney W. Grimes char * 1209b50d902SRodney W. Grimes detract(np, ntype) 1219b50d902SRodney W. Grimes register struct name *np; 1229b50d902SRodney W. Grimes int ntype; 1239b50d902SRodney W. Grimes { 1249b50d902SRodney W. Grimes register int s; 1259b50d902SRodney W. Grimes register char *cp, *top; 1269b50d902SRodney W. Grimes register struct name *p; 1279b50d902SRodney W. Grimes register int comma; 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes comma = ntype & GCOMMA; 1309b50d902SRodney W. Grimes if (np == NIL) 1319b50d902SRodney W. Grimes return(NOSTR); 1329b50d902SRodney W. Grimes ntype &= ~GCOMMA; 1339b50d902SRodney W. Grimes s = 0; 1349b50d902SRodney W. Grimes if (debug && comma) 1359b50d902SRodney W. Grimes fprintf(stderr, "detract asked to insert commas\n"); 1369b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) { 1379b50d902SRodney W. Grimes if (ntype && (p->n_type & GMASK) != ntype) 1389b50d902SRodney W. Grimes continue; 1399b50d902SRodney W. Grimes s += strlen(p->n_name) + 1; 1409b50d902SRodney W. Grimes if (comma) 1419b50d902SRodney W. Grimes s++; 1429b50d902SRodney W. Grimes } 1439b50d902SRodney W. Grimes if (s == 0) 1449b50d902SRodney W. Grimes return(NOSTR); 1459b50d902SRodney W. Grimes s += 2; 1469b50d902SRodney W. Grimes top = salloc(s); 1479b50d902SRodney W. Grimes cp = top; 1489b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) { 1499b50d902SRodney W. Grimes if (ntype && (p->n_type & GMASK) != ntype) 1509b50d902SRodney W. Grimes continue; 1519b50d902SRodney W. Grimes cp = copy(p->n_name, cp); 1529b50d902SRodney W. Grimes if (comma && p->n_flink != NIL) 1539b50d902SRodney W. Grimes *cp++ = ','; 1549b50d902SRodney W. Grimes *cp++ = ' '; 1559b50d902SRodney W. Grimes } 1569b50d902SRodney W. Grimes *--cp = 0; 1579b50d902SRodney W. Grimes if (comma && *--cp == ',') 1589b50d902SRodney W. Grimes *cp = 0; 1599b50d902SRodney W. Grimes return(top); 1609b50d902SRodney W. Grimes } 1619b50d902SRodney W. Grimes 1629b50d902SRodney W. Grimes /* 1639b50d902SRodney W. Grimes * Grab a single word (liberal word) 1649b50d902SRodney W. Grimes * Throw away things between ()'s, and take anything between <>. 1659b50d902SRodney W. Grimes */ 1669b50d902SRodney W. Grimes char * 1679b50d902SRodney W. Grimes yankword(ap, wbuf) 1689b50d902SRodney W. Grimes char *ap, wbuf[]; 1699b50d902SRodney W. Grimes { 1709b50d902SRodney W. Grimes register char *cp, *cp2; 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes cp = ap; 1739b50d902SRodney W. Grimes for (;;) { 1749b50d902SRodney W. Grimes if (*cp == '\0') 1759b50d902SRodney W. Grimes return NOSTR; 1769b50d902SRodney W. Grimes if (*cp == '(') { 1779b50d902SRodney W. Grimes register int nesting = 0; 1789b50d902SRodney W. Grimes 1799b50d902SRodney W. Grimes while (*cp != '\0') { 1809b50d902SRodney W. Grimes switch (*cp++) { 1819b50d902SRodney W. Grimes case '(': 1829b50d902SRodney W. Grimes nesting++; 1839b50d902SRodney W. Grimes break; 1849b50d902SRodney W. Grimes case ')': 1859b50d902SRodney W. Grimes --nesting; 1869b50d902SRodney W. Grimes break; 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes if (nesting <= 0) 1899b50d902SRodney W. Grimes break; 1909b50d902SRodney W. Grimes } 1919b50d902SRodney W. Grimes } else if (*cp == ' ' || *cp == '\t' || *cp == ',') 1929b50d902SRodney W. Grimes cp++; 1939b50d902SRodney W. Grimes else 1949b50d902SRodney W. Grimes break; 1959b50d902SRodney W. Grimes } 1969b50d902SRodney W. Grimes if (*cp == '<') 1979b50d902SRodney W. Grimes for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';) 1989b50d902SRodney W. Grimes ; 1999b50d902SRodney W. Grimes else 2009b50d902SRodney W. Grimes for (cp2 = wbuf; *cp && !index(" \t,(", *cp); *cp2++ = *cp++) 2019b50d902SRodney W. Grimes ; 2029b50d902SRodney W. Grimes *cp2 = '\0'; 2039b50d902SRodney W. Grimes return cp; 2049b50d902SRodney W. Grimes } 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes /* 2079b50d902SRodney W. Grimes * For each recipient in the passed name list with a / 2089b50d902SRodney W. Grimes * in the name, append the message to the end of the named file 2099b50d902SRodney W. Grimes * and remove him from the recipient list. 2109b50d902SRodney W. Grimes * 2119b50d902SRodney W. Grimes * Recipients whose name begins with | are piped through the given 2129b50d902SRodney W. Grimes * program and removed. 2139b50d902SRodney W. Grimes */ 2149b50d902SRodney W. Grimes struct name * 2159b50d902SRodney W. Grimes outof(names, fo, hp) 2169b50d902SRodney W. Grimes struct name *names; 2179b50d902SRodney W. Grimes FILE *fo; 2189b50d902SRodney W. Grimes struct header *hp; 2199b50d902SRodney W. Grimes { 2209b50d902SRodney W. Grimes register int c; 2219b50d902SRodney W. Grimes register struct name *np, *top; 2229b50d902SRodney W. Grimes time_t now, time(); 2239b50d902SRodney W. Grimes char *date, *fname, *ctime(); 2249b50d902SRodney W. Grimes FILE *fout, *fin; 2259b50d902SRodney W. Grimes int ispipe; 2269b50d902SRodney W. Grimes extern char tempEdit[]; 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes top = names; 2299b50d902SRodney W. Grimes np = names; 2309b50d902SRodney W. Grimes (void) time(&now); 2319b50d902SRodney W. Grimes date = ctime(&now); 2329b50d902SRodney W. Grimes while (np != NIL) { 2339b50d902SRodney W. Grimes if (!isfileaddr(np->n_name) && np->n_name[0] != '|') { 2349b50d902SRodney W. Grimes np = np->n_flink; 2359b50d902SRodney W. Grimes continue; 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes ispipe = np->n_name[0] == '|'; 2389b50d902SRodney W. Grimes if (ispipe) 2399b50d902SRodney W. Grimes fname = np->n_name+1; 2409b50d902SRodney W. Grimes else 2419b50d902SRodney W. Grimes fname = expand(np->n_name); 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes /* 2449b50d902SRodney W. Grimes * See if we have copied the complete message out yet. 2459b50d902SRodney W. Grimes * If not, do so. 2469b50d902SRodney W. Grimes */ 2479b50d902SRodney W. Grimes 2489b50d902SRodney W. Grimes if (image < 0) { 2499b50d902SRodney W. Grimes if ((fout = Fopen(tempEdit, "a")) == NULL) { 2509b50d902SRodney W. Grimes perror(tempEdit); 2519b50d902SRodney W. Grimes senderr++; 2529b50d902SRodney W. Grimes goto cant; 2539b50d902SRodney W. Grimes } 2549b50d902SRodney W. Grimes image = open(tempEdit, 2); 2559b50d902SRodney W. Grimes (void) unlink(tempEdit); 2569b50d902SRodney W. Grimes if (image < 0) { 2579b50d902SRodney W. Grimes perror(tempEdit); 2589b50d902SRodney W. Grimes senderr++; 2599b50d902SRodney W. Grimes (void) Fclose(fout); 2609b50d902SRodney W. Grimes goto cant; 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes (void) fcntl(image, F_SETFD, 1); 2639b50d902SRodney W. Grimes fprintf(fout, "From %s %s", myname, date); 2649b50d902SRodney W. Grimes puthead(hp, fout, GTO|GSUBJECT|GCC|GNL); 2659b50d902SRodney W. Grimes while ((c = getc(fo)) != EOF) 2669b50d902SRodney W. Grimes (void) putc(c, fout); 2679b50d902SRodney W. Grimes rewind(fo); 2689b50d902SRodney W. Grimes (void) putc('\n', fout); 2699b50d902SRodney W. Grimes (void) fflush(fout); 2709b50d902SRodney W. Grimes if (ferror(fout)) 2719b50d902SRodney W. Grimes perror(tempEdit); 2729b50d902SRodney W. Grimes (void) Fclose(fout); 2739b50d902SRodney W. Grimes } 2749b50d902SRodney W. Grimes 2759b50d902SRodney W. Grimes /* 2769b50d902SRodney W. Grimes * Now either copy "image" to the desired file 2779b50d902SRodney W. Grimes * or give it as the standard input to the desired 2789b50d902SRodney W. Grimes * program as appropriate. 2799b50d902SRodney W. Grimes */ 2809b50d902SRodney W. Grimes 2819b50d902SRodney W. Grimes if (ispipe) { 2829b50d902SRodney W. Grimes int pid; 2839b50d902SRodney W. Grimes char *shell; 2849b50d902SRodney W. Grimes 2859b50d902SRodney W. Grimes /* 2869b50d902SRodney W. Grimes * XXX 2879b50d902SRodney W. Grimes * We can't really reuse the same image file, 2889b50d902SRodney W. Grimes * because multiple piped recipients will 2899b50d902SRodney W. Grimes * share the same lseek location and trample 2909b50d902SRodney W. Grimes * on one another. 2919b50d902SRodney W. Grimes */ 2929b50d902SRodney W. Grimes if ((shell = value("SHELL")) == NOSTR) 2939b50d902SRodney W. Grimes shell = _PATH_CSHELL; 2949b50d902SRodney W. Grimes pid = start_command(shell, sigmask(SIGHUP)| 2959b50d902SRodney W. Grimes sigmask(SIGINT)|sigmask(SIGQUIT), 2969b50d902SRodney W. Grimes image, -1, "-c", fname, NOSTR); 2979b50d902SRodney W. Grimes if (pid < 0) { 2989b50d902SRodney W. Grimes senderr++; 2999b50d902SRodney W. Grimes goto cant; 3009b50d902SRodney W. Grimes } 3019b50d902SRodney W. Grimes free_child(pid); 3029b50d902SRodney W. Grimes } else { 3039b50d902SRodney W. Grimes int f; 3049b50d902SRodney W. Grimes if ((fout = Fopen(fname, "a")) == NULL) { 3059b50d902SRodney W. Grimes perror(fname); 3069b50d902SRodney W. Grimes senderr++; 3079b50d902SRodney W. Grimes goto cant; 3089b50d902SRodney W. Grimes } 3099b50d902SRodney W. Grimes if ((f = dup(image)) < 0) { 3109b50d902SRodney W. Grimes perror("dup"); 3119b50d902SRodney W. Grimes fin = NULL; 3129b50d902SRodney W. Grimes } else 3139b50d902SRodney W. Grimes fin = Fdopen(f, "r"); 3149b50d902SRodney W. Grimes if (fin == NULL) { 3159b50d902SRodney W. Grimes fprintf(stderr, "Can't reopen image\n"); 3169b50d902SRodney W. Grimes (void) Fclose(fout); 3179b50d902SRodney W. Grimes senderr++; 3189b50d902SRodney W. Grimes goto cant; 3199b50d902SRodney W. Grimes } 3209b50d902SRodney W. Grimes rewind(fin); 3219b50d902SRodney W. Grimes while ((c = getc(fin)) != EOF) 3229b50d902SRodney W. Grimes (void) putc(c, fout); 3239b50d902SRodney W. Grimes if (ferror(fout)) 3249b50d902SRodney W. Grimes senderr++, perror(fname); 3259b50d902SRodney W. Grimes (void) Fclose(fout); 3269b50d902SRodney W. Grimes (void) Fclose(fin); 3279b50d902SRodney W. Grimes } 3289b50d902SRodney W. Grimes cant: 3299b50d902SRodney W. Grimes /* 3309b50d902SRodney W. Grimes * In days of old we removed the entry from the 3319b50d902SRodney W. Grimes * the list; now for sake of header expansion 3329b50d902SRodney W. Grimes * we leave it in and mark it as deleted. 3339b50d902SRodney W. Grimes */ 3349b50d902SRodney W. Grimes np->n_type |= GDEL; 3359b50d902SRodney W. Grimes np = np->n_flink; 3369b50d902SRodney W. Grimes } 3379b50d902SRodney W. Grimes if (image >= 0) { 3389b50d902SRodney W. Grimes (void) close(image); 3399b50d902SRodney W. Grimes image = -1; 3409b50d902SRodney W. Grimes } 3419b50d902SRodney W. Grimes return(top); 3429b50d902SRodney W. Grimes } 3439b50d902SRodney W. Grimes 3449b50d902SRodney W. Grimes /* 3459b50d902SRodney W. Grimes * Determine if the passed address is a local "send to file" address. 3469b50d902SRodney W. Grimes * If any of the network metacharacters precedes any slashes, it can't 3479b50d902SRodney W. Grimes * be a filename. We cheat with .'s to allow path names like ./... 3489b50d902SRodney W. Grimes */ 3499b50d902SRodney W. Grimes int 3509b50d902SRodney W. Grimes isfileaddr(name) 3519b50d902SRodney W. Grimes char *name; 3529b50d902SRodney W. Grimes { 3539b50d902SRodney W. Grimes register char *cp; 3549b50d902SRodney W. Grimes 3559b50d902SRodney W. Grimes if (*name == '+') 3569b50d902SRodney W. Grimes return 1; 3579b50d902SRodney W. Grimes for (cp = name; *cp; cp++) { 3589b50d902SRodney W. Grimes if (*cp == '!' || *cp == '%' || *cp == '@') 3599b50d902SRodney W. Grimes return 0; 3609b50d902SRodney W. Grimes if (*cp == '/') 3619b50d902SRodney W. Grimes return 1; 3629b50d902SRodney W. Grimes } 3639b50d902SRodney W. Grimes return 0; 3649b50d902SRodney W. Grimes } 3659b50d902SRodney W. Grimes 3669b50d902SRodney W. Grimes /* 3679b50d902SRodney W. Grimes * Map all of the aliased users in the invoker's mailrc 3689b50d902SRodney W. Grimes * file and insert them into the list. 3699b50d902SRodney W. Grimes * Changed after all these months of service to recursively 3709b50d902SRodney W. Grimes * expand names (2/14/80). 3719b50d902SRodney W. Grimes */ 3729b50d902SRodney W. Grimes 3739b50d902SRodney W. Grimes struct name * 3749b50d902SRodney W. Grimes usermap(names) 3759b50d902SRodney W. Grimes struct name *names; 3769b50d902SRodney W. Grimes { 3779b50d902SRodney W. Grimes register struct name *new, *np, *cp; 3789b50d902SRodney W. Grimes struct grouphead *gh; 3799b50d902SRodney W. Grimes register int metoo; 3809b50d902SRodney W. Grimes 3819b50d902SRodney W. Grimes new = NIL; 3829b50d902SRodney W. Grimes np = names; 3839b50d902SRodney W. Grimes metoo = (value("metoo") != NOSTR); 3849b50d902SRodney W. Grimes while (np != NIL) { 3859b50d902SRodney W. Grimes if (np->n_name[0] == '\\') { 3869b50d902SRodney W. Grimes cp = np->n_flink; 3879b50d902SRodney W. Grimes new = put(new, np); 3889b50d902SRodney W. Grimes np = cp; 3899b50d902SRodney W. Grimes continue; 3909b50d902SRodney W. Grimes } 3919b50d902SRodney W. Grimes gh = findgroup(np->n_name); 3929b50d902SRodney W. Grimes cp = np->n_flink; 3939b50d902SRodney W. Grimes if (gh != NOGRP) 3949b50d902SRodney W. Grimes new = gexpand(new, gh, metoo, np->n_type); 3959b50d902SRodney W. Grimes else 3969b50d902SRodney W. Grimes new = put(new, np); 3979b50d902SRodney W. Grimes np = cp; 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes return(new); 4009b50d902SRodney W. Grimes } 4019b50d902SRodney W. Grimes 4029b50d902SRodney W. Grimes /* 4039b50d902SRodney W. Grimes * Recursively expand a group name. We limit the expansion to some 4049b50d902SRodney W. Grimes * fixed level to keep things from going haywire. 4059b50d902SRodney W. Grimes * Direct recursion is not expanded for convenience. 4069b50d902SRodney W. Grimes */ 4079b50d902SRodney W. Grimes 4089b50d902SRodney W. Grimes struct name * 4099b50d902SRodney W. Grimes gexpand(nlist, gh, metoo, ntype) 4109b50d902SRodney W. Grimes struct name *nlist; 4119b50d902SRodney W. Grimes struct grouphead *gh; 4129b50d902SRodney W. Grimes int metoo, ntype; 4139b50d902SRodney W. Grimes { 4149b50d902SRodney W. Grimes struct group *gp; 4159b50d902SRodney W. Grimes struct grouphead *ngh; 4169b50d902SRodney W. Grimes struct name *np; 4179b50d902SRodney W. Grimes static int depth; 4189b50d902SRodney W. Grimes char *cp; 4199b50d902SRodney W. Grimes 4209b50d902SRodney W. Grimes if (depth > MAXEXP) { 4219b50d902SRodney W. Grimes printf("Expanding alias to depth larger than %d\n", MAXEXP); 4229b50d902SRodney W. Grimes return(nlist); 4239b50d902SRodney W. Grimes } 4249b50d902SRodney W. Grimes depth++; 4259b50d902SRodney W. Grimes for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) { 4269b50d902SRodney W. Grimes cp = gp->ge_name; 4279b50d902SRodney W. Grimes if (*cp == '\\') 4289b50d902SRodney W. Grimes goto quote; 4299b50d902SRodney W. Grimes if (strcmp(cp, gh->g_name) == 0) 4309b50d902SRodney W. Grimes goto quote; 4319b50d902SRodney W. Grimes if ((ngh = findgroup(cp)) != NOGRP) { 4329b50d902SRodney W. Grimes nlist = gexpand(nlist, ngh, metoo, ntype); 4339b50d902SRodney W. Grimes continue; 4349b50d902SRodney W. Grimes } 4359b50d902SRodney W. Grimes quote: 4369b50d902SRodney W. Grimes np = nalloc(cp, ntype); 4379b50d902SRodney W. Grimes /* 4389b50d902SRodney W. Grimes * At this point should allow to expand 4399b50d902SRodney W. Grimes * to self if only person in group 4409b50d902SRodney W. Grimes */ 4419b50d902SRodney W. Grimes if (gp == gh->g_list && gp->ge_link == NOGE) 4429b50d902SRodney W. Grimes goto skip; 4439b50d902SRodney W. Grimes if (!metoo && strcmp(cp, myname) == 0) 4449b50d902SRodney W. Grimes np->n_type |= GDEL; 4459b50d902SRodney W. Grimes skip: 4469b50d902SRodney W. Grimes nlist = put(nlist, np); 4479b50d902SRodney W. Grimes } 4489b50d902SRodney W. Grimes depth--; 4499b50d902SRodney W. Grimes return(nlist); 4509b50d902SRodney W. Grimes } 4519b50d902SRodney W. Grimes 4529b50d902SRodney W. Grimes /* 4539b50d902SRodney W. Grimes * Concatenate the two passed name lists, return the result. 4549b50d902SRodney W. Grimes */ 4559b50d902SRodney W. Grimes struct name * 4569b50d902SRodney W. Grimes cat(n1, n2) 4579b50d902SRodney W. Grimes struct name *n1, *n2; 4589b50d902SRodney W. Grimes { 4599b50d902SRodney W. Grimes register struct name *tail; 4609b50d902SRodney W. Grimes 4619b50d902SRodney W. Grimes if (n1 == NIL) 4629b50d902SRodney W. Grimes return(n2); 4639b50d902SRodney W. Grimes if (n2 == NIL) 4649b50d902SRodney W. Grimes return(n1); 4659b50d902SRodney W. Grimes tail = tailof(n1); 4669b50d902SRodney W. Grimes tail->n_flink = n2; 4679b50d902SRodney W. Grimes n2->n_blink = tail; 4689b50d902SRodney W. Grimes return(n1); 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes 4719b50d902SRodney W. Grimes /* 4729b50d902SRodney W. Grimes * Unpack the name list onto a vector of strings. 4739b50d902SRodney W. Grimes * Return an error if the name list won't fit. 4749b50d902SRodney W. Grimes */ 4759b50d902SRodney W. Grimes char ** 4769b50d902SRodney W. Grimes unpack(np) 4779b50d902SRodney W. Grimes struct name *np; 4789b50d902SRodney W. Grimes { 4799b50d902SRodney W. Grimes register char **ap, **top; 4809b50d902SRodney W. Grimes register struct name *n; 4819b50d902SRodney W. Grimes int t, extra, metoo, verbose; 4829b50d902SRodney W. Grimes 4839b50d902SRodney W. Grimes n = np; 4849b50d902SRodney W. Grimes if ((t = count(n)) == 0) 4859b50d902SRodney W. Grimes panic("No names to unpack"); 4869b50d902SRodney W. Grimes /* 4879b50d902SRodney W. Grimes * Compute the number of extra arguments we will need. 4889b50d902SRodney W. Grimes * We need at least two extra -- one for "mail" and one for 4899b50d902SRodney W. Grimes * the terminating 0 pointer. Additional spots may be needed 4909b50d902SRodney W. Grimes * to pass along -f to the host mailer. 4919b50d902SRodney W. Grimes */ 4929b50d902SRodney W. Grimes extra = 2; 4939b50d902SRodney W. Grimes extra++; 4949b50d902SRodney W. Grimes metoo = value("metoo") != NOSTR; 4959b50d902SRodney W. Grimes if (metoo) 4969b50d902SRodney W. Grimes extra++; 4979b50d902SRodney W. Grimes verbose = value("verbose") != NOSTR; 4989b50d902SRodney W. Grimes if (verbose) 4999b50d902SRodney W. Grimes extra++; 5009b50d902SRodney W. Grimes top = (char **) salloc((t + extra) * sizeof *top); 5019b50d902SRodney W. Grimes ap = top; 5029b50d902SRodney W. Grimes *ap++ = "send-mail"; 5039b50d902SRodney W. Grimes *ap++ = "-i"; 5049b50d902SRodney W. Grimes if (metoo) 5059b50d902SRodney W. Grimes *ap++ = "-m"; 5069b50d902SRodney W. Grimes if (verbose) 5079b50d902SRodney W. Grimes *ap++ = "-v"; 5089b50d902SRodney W. Grimes for (; n != NIL; n = n->n_flink) 5099b50d902SRodney W. Grimes if ((n->n_type & GDEL) == 0) 5109b50d902SRodney W. Grimes *ap++ = n->n_name; 5119b50d902SRodney W. Grimes *ap = NOSTR; 5129b50d902SRodney W. Grimes return(top); 5139b50d902SRodney W. Grimes } 5149b50d902SRodney W. Grimes 5159b50d902SRodney W. Grimes /* 5169b50d902SRodney W. Grimes * Remove all of the duplicates from the passed name list by 5179b50d902SRodney W. Grimes * insertion sorting them, then checking for dups. 5189b50d902SRodney W. Grimes * Return the head of the new list. 5199b50d902SRodney W. Grimes */ 5209b50d902SRodney W. Grimes struct name * 5219b50d902SRodney W. Grimes elide(names) 5229b50d902SRodney W. Grimes struct name *names; 5239b50d902SRodney W. Grimes { 5249b50d902SRodney W. Grimes register struct name *np, *t, *new; 5259b50d902SRodney W. Grimes struct name *x; 5269b50d902SRodney W. Grimes 5279b50d902SRodney W. Grimes if (names == NIL) 5289b50d902SRodney W. Grimes return(NIL); 5299b50d902SRodney W. Grimes new = names; 5309b50d902SRodney W. Grimes np = names; 5319b50d902SRodney W. Grimes np = np->n_flink; 5329b50d902SRodney W. Grimes if (np != NIL) 5339b50d902SRodney W. Grimes np->n_blink = NIL; 5349b50d902SRodney W. Grimes new->n_flink = NIL; 5359b50d902SRodney W. Grimes while (np != NIL) { 5369b50d902SRodney W. Grimes t = new; 5379b50d902SRodney W. Grimes while (strcasecmp(t->n_name, np->n_name) < 0) { 5389b50d902SRodney W. Grimes if (t->n_flink == NIL) 5399b50d902SRodney W. Grimes break; 5409b50d902SRodney W. Grimes t = t->n_flink; 5419b50d902SRodney W. Grimes } 5429b50d902SRodney W. Grimes 5439b50d902SRodney W. Grimes /* 5449b50d902SRodney W. Grimes * If we ran out of t's, put the new entry after 5459b50d902SRodney W. Grimes * the current value of t. 5469b50d902SRodney W. Grimes */ 5479b50d902SRodney W. Grimes 5489b50d902SRodney W. Grimes if (strcasecmp(t->n_name, np->n_name) < 0) { 5499b50d902SRodney W. Grimes t->n_flink = np; 5509b50d902SRodney W. Grimes np->n_blink = t; 5519b50d902SRodney W. Grimes t = np; 5529b50d902SRodney W. Grimes np = np->n_flink; 5539b50d902SRodney W. Grimes t->n_flink = NIL; 5549b50d902SRodney W. Grimes continue; 5559b50d902SRodney W. Grimes } 5569b50d902SRodney W. Grimes 5579b50d902SRodney W. Grimes /* 5589b50d902SRodney W. Grimes * Otherwise, put the new entry in front of the 5599b50d902SRodney W. Grimes * current t. If at the front of the list, 5609b50d902SRodney W. Grimes * the new guy becomes the new head of the list. 5619b50d902SRodney W. Grimes */ 5629b50d902SRodney W. Grimes 5639b50d902SRodney W. Grimes if (t == new) { 5649b50d902SRodney W. Grimes t = np; 5659b50d902SRodney W. Grimes np = np->n_flink; 5669b50d902SRodney W. Grimes t->n_flink = new; 5679b50d902SRodney W. Grimes new->n_blink = t; 5689b50d902SRodney W. Grimes t->n_blink = NIL; 5699b50d902SRodney W. Grimes new = t; 5709b50d902SRodney W. Grimes continue; 5719b50d902SRodney W. Grimes } 5729b50d902SRodney W. Grimes 5739b50d902SRodney W. Grimes /* 5749b50d902SRodney W. Grimes * The normal case -- we are inserting into the 5759b50d902SRodney W. Grimes * middle of the list. 5769b50d902SRodney W. Grimes */ 5779b50d902SRodney W. Grimes 5789b50d902SRodney W. Grimes x = np; 5799b50d902SRodney W. Grimes np = np->n_flink; 5809b50d902SRodney W. Grimes x->n_flink = t; 5819b50d902SRodney W. Grimes x->n_blink = t->n_blink; 5829b50d902SRodney W. Grimes t->n_blink->n_flink = x; 5839b50d902SRodney W. Grimes t->n_blink = x; 5849b50d902SRodney W. Grimes } 5859b50d902SRodney W. Grimes 5869b50d902SRodney W. Grimes /* 5879b50d902SRodney W. Grimes * Now the list headed up by new is sorted. 5889b50d902SRodney W. Grimes * Go through it and remove duplicates. 5899b50d902SRodney W. Grimes */ 5909b50d902SRodney W. Grimes 5919b50d902SRodney W. Grimes np = new; 5929b50d902SRodney W. Grimes while (np != NIL) { 5939b50d902SRodney W. Grimes t = np; 5949b50d902SRodney W. Grimes while (t->n_flink != NIL && 5959b50d902SRodney W. Grimes strcasecmp(np->n_name, t->n_flink->n_name) == 0) 5969b50d902SRodney W. Grimes t = t->n_flink; 5979b50d902SRodney W. Grimes if (t == np || t == NIL) { 5989b50d902SRodney W. Grimes np = np->n_flink; 5999b50d902SRodney W. Grimes continue; 6009b50d902SRodney W. Grimes } 6019b50d902SRodney W. Grimes 6029b50d902SRodney W. Grimes /* 6039b50d902SRodney W. Grimes * Now t points to the last entry with the same name 6049b50d902SRodney W. Grimes * as np. Make np point beyond t. 6059b50d902SRodney W. Grimes */ 6069b50d902SRodney W. Grimes 6079b50d902SRodney W. Grimes np->n_flink = t->n_flink; 6089b50d902SRodney W. Grimes if (t->n_flink != NIL) 6099b50d902SRodney W. Grimes t->n_flink->n_blink = np; 6109b50d902SRodney W. Grimes np = np->n_flink; 6119b50d902SRodney W. Grimes } 6129b50d902SRodney W. Grimes return(new); 6139b50d902SRodney W. Grimes } 6149b50d902SRodney W. Grimes 6159b50d902SRodney W. Grimes /* 6169b50d902SRodney W. Grimes * Put another node onto a list of names and return 6179b50d902SRodney W. Grimes * the list. 6189b50d902SRodney W. Grimes */ 6199b50d902SRodney W. Grimes struct name * 6209b50d902SRodney W. Grimes put(list, node) 6219b50d902SRodney W. Grimes struct name *list, *node; 6229b50d902SRodney W. Grimes { 6239b50d902SRodney W. Grimes node->n_flink = list; 6249b50d902SRodney W. Grimes node->n_blink = NIL; 6259b50d902SRodney W. Grimes if (list != NIL) 6269b50d902SRodney W. Grimes list->n_blink = node; 6279b50d902SRodney W. Grimes return(node); 6289b50d902SRodney W. Grimes } 6299b50d902SRodney W. Grimes 6309b50d902SRodney W. Grimes /* 6319b50d902SRodney W. Grimes * Determine the number of undeleted elements in 6329b50d902SRodney W. Grimes * a name list and return it. 6339b50d902SRodney W. Grimes */ 6349b50d902SRodney W. Grimes int 6359b50d902SRodney W. Grimes count(np) 6369b50d902SRodney W. Grimes register struct name *np; 6379b50d902SRodney W. Grimes { 6389b50d902SRodney W. Grimes register int c; 6399b50d902SRodney W. Grimes 6409b50d902SRodney W. Grimes for (c = 0; np != NIL; np = np->n_flink) 6419b50d902SRodney W. Grimes if ((np->n_type & GDEL) == 0) 6429b50d902SRodney W. Grimes c++; 6439b50d902SRodney W. Grimes return c; 6449b50d902SRodney W. Grimes } 6459b50d902SRodney W. Grimes 6469b50d902SRodney W. Grimes /* 6479b50d902SRodney W. Grimes * Delete the given name from a namelist. 6489b50d902SRodney W. Grimes */ 6499b50d902SRodney W. Grimes struct name * 6509b50d902SRodney W. Grimes delname(np, name) 6519b50d902SRodney W. Grimes register struct name *np; 6529b50d902SRodney W. Grimes char name[]; 6539b50d902SRodney W. Grimes { 6549b50d902SRodney W. Grimes register struct name *p; 6559b50d902SRodney W. Grimes 6569b50d902SRodney W. Grimes for (p = np; p != NIL; p = p->n_flink) 6579b50d902SRodney W. Grimes if (strcasecmp(p->n_name, name) == 0) { 6589b50d902SRodney W. Grimes if (p->n_blink == NIL) { 6599b50d902SRodney W. Grimes if (p->n_flink != NIL) 6609b50d902SRodney W. Grimes p->n_flink->n_blink = NIL; 6619b50d902SRodney W. Grimes np = p->n_flink; 6629b50d902SRodney W. Grimes continue; 6639b50d902SRodney W. Grimes } 6649b50d902SRodney W. Grimes if (p->n_flink == NIL) { 6659b50d902SRodney W. Grimes if (p->n_blink != NIL) 6669b50d902SRodney W. Grimes p->n_blink->n_flink = NIL; 6679b50d902SRodney W. Grimes continue; 6689b50d902SRodney W. Grimes } 6699b50d902SRodney W. Grimes p->n_blink->n_flink = p->n_flink; 6709b50d902SRodney W. Grimes p->n_flink->n_blink = p->n_blink; 6719b50d902SRodney W. Grimes } 6729b50d902SRodney W. Grimes return np; 6739b50d902SRodney W. Grimes } 6749b50d902SRodney W. Grimes 6759b50d902SRodney W. Grimes /* 6769b50d902SRodney W. Grimes * Pretty print a name list 6779b50d902SRodney W. Grimes * Uncomment it if you need it. 6789b50d902SRodney W. Grimes */ 6799b50d902SRodney W. Grimes 6809b50d902SRodney W. Grimes /* 6819b50d902SRodney W. Grimes void 6829b50d902SRodney W. Grimes prettyprint(name) 6839b50d902SRodney W. Grimes struct name *name; 6849b50d902SRodney W. Grimes { 6859b50d902SRodney W. Grimes register struct name *np; 6869b50d902SRodney W. Grimes 6879b50d902SRodney W. Grimes np = name; 6889b50d902SRodney W. Grimes while (np != NIL) { 6899b50d902SRodney W. Grimes fprintf(stderr, "%s(%d) ", np->n_name, np->n_type); 6909b50d902SRodney W. Grimes np = np->n_flink; 6919b50d902SRodney W. Grimes } 6929b50d902SRodney W. Grimes fprintf(stderr, "\n"); 6939b50d902SRodney W. Grimes } 6949b50d902SRodney W. Grimes */ 695