names.c revision 216564
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1980, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3174769Smikeh#if 0 321590Srgrimesstatic char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93"; 3374769Smikeh#endif 341590Srgrimes#endif /* not lint */ 3599112Sobrien#include <sys/cdefs.h> 3699112Sobrien__FBSDID("$FreeBSD: head/usr.bin/mail/names.c 216564 2010-12-19 16:25:23Z charnier $"); 371590Srgrimes 381590Srgrimes/* 391590Srgrimes * Mail -- a mail program 401590Srgrimes * 411590Srgrimes * Handle name lists. 421590Srgrimes */ 431590Srgrimes 441590Srgrimes#include "rcv.h" 451590Srgrimes#include <fcntl.h> 461590Srgrimes#include "extern.h" 471590Srgrimes 481590Srgrimes/* 491590Srgrimes * Allocate a single element of a name list, 501590Srgrimes * initialize its name field to the passed 511590Srgrimes * name and return it. 521590Srgrimes */ 531590Srgrimesstruct name * 54216564Scharniernalloc(char str[], int ntype) 551590Srgrimes{ 5677274Smikeh struct name *np; 571590Srgrimes 5877274Smikeh np = (struct name *)salloc(sizeof(*np)); 5977274Smikeh np->n_flink = NULL; 6077274Smikeh np->n_blink = NULL; 611590Srgrimes np->n_type = ntype; 621590Srgrimes np->n_name = savestr(str); 6377274Smikeh return (np); 641590Srgrimes} 651590Srgrimes 661590Srgrimes/* 671590Srgrimes * Find the tail of a list and return it. 681590Srgrimes */ 691590Srgrimesstruct name * 70216564Scharniertailof(struct name *name) 711590Srgrimes{ 7277274Smikeh struct name *np; 731590Srgrimes 741590Srgrimes np = name; 7577274Smikeh if (np == NULL) 7677274Smikeh return (NULL); 7777274Smikeh while (np->n_flink != NULL) 781590Srgrimes np = np->n_flink; 7977274Smikeh return (np); 801590Srgrimes} 811590Srgrimes 821590Srgrimes/* 831590Srgrimes * Extract a list of names from a line, 841590Srgrimes * and make a list of names from it. 8577274Smikeh * Return the list or NULL if none found. 861590Srgrimes */ 871590Srgrimesstruct name * 88216564Scharnierextract(char line[], int ntype) 891590Srgrimes{ 9077274Smikeh char *cp, *nbuf; 9177274Smikeh struct name *top, *np, *t; 921590Srgrimes 9377274Smikeh if (line == NULL || *line == '\0') 9477274Smikeh return (NULL); 9577274Smikeh if ((nbuf = malloc(strlen(line) + 1)) == NULL) 9674769Smikeh err(1, "Out of memory"); 9777274Smikeh top = NULL; 9877274Smikeh np = NULL; 991590Srgrimes cp = line; 10077274Smikeh while ((cp = yankword(cp, nbuf)) != NULL) { 1011590Srgrimes t = nalloc(nbuf, ntype); 10277274Smikeh if (top == NULL) 1031590Srgrimes top = t; 1041590Srgrimes else 1051590Srgrimes np->n_flink = t; 1061590Srgrimes t->n_blink = np; 1071590Srgrimes np = t; 1081590Srgrimes } 10977274Smikeh (void)free(nbuf); 11077274Smikeh return (top); 1111590Srgrimes} 1121590Srgrimes 1131590Srgrimes/* 1141590Srgrimes * Turn a list of names into a string of the same names. 1151590Srgrimes */ 1161590Srgrimeschar * 117216564Scharnierdetract(struct name *np, int ntype) 1181590Srgrimes{ 11977274Smikeh int s, comma; 12077274Smikeh char *cp, *top; 12177274Smikeh struct name *p; 1221590Srgrimes 1231590Srgrimes comma = ntype & GCOMMA; 12477274Smikeh if (np == NULL) 12577274Smikeh return (NULL); 1261590Srgrimes ntype &= ~GCOMMA; 1271590Srgrimes s = 0; 1281590Srgrimes if (debug && comma) 1291590Srgrimes fprintf(stderr, "detract asked to insert commas\n"); 13077274Smikeh for (p = np; p != NULL; p = p->n_flink) { 1311590Srgrimes if (ntype && (p->n_type & GMASK) != ntype) 1321590Srgrimes continue; 1331590Srgrimes s += strlen(p->n_name) + 1; 1341590Srgrimes if (comma) 1351590Srgrimes s++; 1361590Srgrimes } 1371590Srgrimes if (s == 0) 13877274Smikeh return (NULL); 1391590Srgrimes s += 2; 1401590Srgrimes top = salloc(s); 1411590Srgrimes cp = top; 14277274Smikeh for (p = np; p != NULL; p = p->n_flink) { 1431590Srgrimes if (ntype && (p->n_type & GMASK) != ntype) 1441590Srgrimes continue; 14574769Smikeh cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1); 14677274Smikeh if (comma && p->n_flink != NULL) 1471590Srgrimes *cp++ = ','; 1481590Srgrimes *cp++ = ' '; 1491590Srgrimes } 15074769Smikeh *--cp = '\0'; 1511590Srgrimes if (comma && *--cp == ',') 15274769Smikeh *cp = '\0'; 15377274Smikeh return (top); 1541590Srgrimes} 1551590Srgrimes 1561590Srgrimes/* 1571590Srgrimes * Grab a single word (liberal word) 1581590Srgrimes * Throw away things between ()'s, and take anything between <>. 1591590Srgrimes */ 1601590Srgrimeschar * 161216564Scharnieryankword(char *ap, char wbuf[]) 1621590Srgrimes{ 16377274Smikeh char *cp, *cp2; 1641590Srgrimes 1651590Srgrimes cp = ap; 1661590Srgrimes for (;;) { 1671590Srgrimes if (*cp == '\0') 16877274Smikeh return (NULL); 1691590Srgrimes if (*cp == '(') { 17077274Smikeh int nesting = 0; 1711590Srgrimes 1721590Srgrimes while (*cp != '\0') { 1731590Srgrimes switch (*cp++) { 1741590Srgrimes case '(': 1751590Srgrimes nesting++; 1761590Srgrimes break; 1771590Srgrimes case ')': 1781590Srgrimes --nesting; 1791590Srgrimes break; 1801590Srgrimes } 1811590Srgrimes if (nesting <= 0) 1821590Srgrimes break; 1831590Srgrimes } 1841590Srgrimes } else if (*cp == ' ' || *cp == '\t' || *cp == ',') 1851590Srgrimes cp++; 1861590Srgrimes else 1871590Srgrimes break; 1881590Srgrimes } 1891590Srgrimes if (*cp == '<') 1901590Srgrimes for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';) 1911590Srgrimes ; 1921590Srgrimes else 19377274Smikeh for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL; 19477274Smikeh *cp2++ = *cp++) 1951590Srgrimes ; 1961590Srgrimes *cp2 = '\0'; 19777274Smikeh return (cp); 1981590Srgrimes} 1991590Srgrimes 2001590Srgrimes/* 201126415Smikeh * Grab a single login name (liberal word) 202126415Smikeh * Throw away things between ()'s, take anything between <>, 203126415Smikeh * and look for words before metacharacters %, @, !. 204126415Smikeh */ 205126415Smikehchar * 206216564Scharnieryanklogin(char *ap, char wbuf[]) 207126415Smikeh{ 208126415Smikeh char *cp, *cp2, *cp_temp; 209126415Smikeh int n; 210126415Smikeh 211126415Smikeh cp = ap; 212126415Smikeh for (;;) { 213126415Smikeh if (*cp == '\0') 214126415Smikeh return (NULL); 215126415Smikeh if (*cp == '(') { 216126415Smikeh int nesting = 0; 217126415Smikeh 218126415Smikeh while (*cp != '\0') { 219126415Smikeh switch (*cp++) { 220126415Smikeh case '(': 221126415Smikeh nesting++; 222126415Smikeh break; 223126415Smikeh case ')': 224126415Smikeh --nesting; 225126415Smikeh break; 226126415Smikeh } 227126415Smikeh if (nesting <= 0) 228126415Smikeh break; 229126415Smikeh } 230126415Smikeh } else if (*cp == ' ' || *cp == '\t' || *cp == ',') 231126415Smikeh cp++; 232126415Smikeh else 233126415Smikeh break; 234126415Smikeh } 235126415Smikeh 236126415Smikeh /* 237126415Smikeh * Now, let's go forward till we meet the needed character, 238126415Smikeh * and step one word back. 239126415Smikeh */ 240126415Smikeh 241126415Smikeh /* First, remember current point. */ 242126415Smikeh cp_temp = cp; 243126415Smikeh n = 0; 244126415Smikeh 245126415Smikeh /* 246126415Smikeh * Note that we look ahead in a cycle. This is safe, since 247126415Smikeh * non-end of string is checked first. 248126415Smikeh */ 249126415Smikeh while(*cp != '\0' && strchr("@%!", *(cp + 1)) == NULL) 250126415Smikeh cp++; 251126415Smikeh 252126415Smikeh /* 253126415Smikeh * Now, start stepping back to the first non-word character, 254126415Smikeh * while counting the number of symbols in a word. 255126415Smikeh */ 256126415Smikeh while(cp != cp_temp && strchr(" \t,<>", *(cp - 1)) == NULL) { 257126415Smikeh n++; 258126415Smikeh cp--; 259126415Smikeh } 260126415Smikeh 261126415Smikeh /* Finally, grab the word forward. */ 262126415Smikeh cp2 = wbuf; 263126415Smikeh while(n >= 0) { 264126415Smikeh *cp2++=*cp++; 265126415Smikeh n--; 266126415Smikeh } 267126415Smikeh 268126415Smikeh *cp2 = '\0'; 269126415Smikeh return (cp); 270126415Smikeh} 271126415Smikeh 272126415Smikeh/* 2731590Srgrimes * For each recipient in the passed name list with a / 2741590Srgrimes * in the name, append the message to the end of the named file 2751590Srgrimes * and remove him from the recipient list. 2761590Srgrimes * 2771590Srgrimes * Recipients whose name begins with | are piped through the given 2781590Srgrimes * program and removed. 2791590Srgrimes */ 2801590Srgrimesstruct name * 281216564Scharnieroutof(struct name *names, FILE *fo, struct header *hp) 2821590Srgrimes{ 28377274Smikeh int c, ispipe; 28477274Smikeh struct name *np, *top; 28577274Smikeh time_t now; 28677274Smikeh char *date, *fname; 2871590Srgrimes FILE *fout, *fin; 2881590Srgrimes 2891590Srgrimes top = names; 2901590Srgrimes np = names; 29177274Smikeh (void)time(&now); 2921590Srgrimes date = ctime(&now); 29377274Smikeh while (np != NULL) { 2941590Srgrimes if (!isfileaddr(np->n_name) && np->n_name[0] != '|') { 2951590Srgrimes np = np->n_flink; 2961590Srgrimes continue; 2971590Srgrimes } 2981590Srgrimes ispipe = np->n_name[0] == '|'; 2991590Srgrimes if (ispipe) 3001590Srgrimes fname = np->n_name+1; 3011590Srgrimes else 3021590Srgrimes fname = expand(np->n_name); 3031590Srgrimes 3041590Srgrimes /* 3051590Srgrimes * See if we have copied the complete message out yet. 3061590Srgrimes * If not, do so. 3071590Srgrimes */ 3081590Srgrimes 3091590Srgrimes if (image < 0) { 31074769Smikeh int fd; 31174769Smikeh char tempname[PATHSIZE]; 31274769Smikeh 31377274Smikeh (void)snprintf(tempname, sizeof(tempname), 31477274Smikeh "%s/mail.ReXXXXXXXXXX", tmpdir); 31574769Smikeh if ((fd = mkstemp(tempname)) == -1 || 31674769Smikeh (fout = Fdopen(fd, "a")) == NULL) { 31774769Smikeh warn("%s", tempname); 3181590Srgrimes senderr++; 3191590Srgrimes goto cant; 3201590Srgrimes } 32174769Smikeh image = open(tempname, O_RDWR); 32277274Smikeh (void)rm(tempname); 3231590Srgrimes if (image < 0) { 32474769Smikeh warn("%s", tempname); 3251590Srgrimes senderr++; 32677274Smikeh (void)Fclose(fout); 3271590Srgrimes goto cant; 3281590Srgrimes } 32977274Smikeh (void)fcntl(image, F_SETFD, 1); 3301590Srgrimes fprintf(fout, "From %s %s", myname, date); 33132189Sjoerg puthead(hp, fout, 33277274Smikeh GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL); 3331590Srgrimes while ((c = getc(fo)) != EOF) 33477274Smikeh (void)putc(c, fout); 3351590Srgrimes rewind(fo); 33677274Smikeh fprintf(fout, "\n"); 33777274Smikeh (void)fflush(fout); 33874769Smikeh if (ferror(fout)) { 33974769Smikeh warn("%s", tempname); 34074769Smikeh senderr++; 34177274Smikeh (void)Fclose(fout); 34274769Smikeh goto cant; 34374769Smikeh } 34477274Smikeh (void)Fclose(fout); 3451590Srgrimes } 3461590Srgrimes 3471590Srgrimes /* 3481590Srgrimes * Now either copy "image" to the desired file 3491590Srgrimes * or give it as the standard input to the desired 3501590Srgrimes * program as appropriate. 3511590Srgrimes */ 3521590Srgrimes 3531590Srgrimes if (ispipe) { 3541590Srgrimes int pid; 35577274Smikeh char *sh; 35688150Smikeh sigset_t nset; 3571590Srgrimes 3581590Srgrimes /* 3591590Srgrimes * XXX 3601590Srgrimes * We can't really reuse the same image file, 3611590Srgrimes * because multiple piped recipients will 3621590Srgrimes * share the same lseek location and trample 3631590Srgrimes * on one another. 3641590Srgrimes */ 36577274Smikeh if ((sh = value("SHELL")) == NULL) 36677274Smikeh sh = _PATH_CSHELL; 36788150Smikeh (void)sigemptyset(&nset); 36888150Smikeh (void)sigaddset(&nset, SIGHUP); 36988150Smikeh (void)sigaddset(&nset, SIGINT); 37088150Smikeh (void)sigaddset(&nset, SIGQUIT); 37188150Smikeh pid = start_command(sh, &nset, image, -1, "-c", fname, 37288150Smikeh NULL); 3731590Srgrimes if (pid < 0) { 3741590Srgrimes senderr++; 3751590Srgrimes goto cant; 3761590Srgrimes } 3771590Srgrimes free_child(pid); 3781590Srgrimes } else { 3791590Srgrimes int f; 3801590Srgrimes if ((fout = Fopen(fname, "a")) == NULL) { 38174769Smikeh warn("%s", fname); 3821590Srgrimes senderr++; 3831590Srgrimes goto cant; 3841590Srgrimes } 3851590Srgrimes if ((f = dup(image)) < 0) { 38674769Smikeh warn("dup"); 3871590Srgrimes fin = NULL; 3881590Srgrimes } else 3891590Srgrimes fin = Fdopen(f, "r"); 3901590Srgrimes if (fin == NULL) { 3911590Srgrimes fprintf(stderr, "Can't reopen image\n"); 39277274Smikeh (void)Fclose(fout); 3931590Srgrimes senderr++; 3941590Srgrimes goto cant; 3951590Srgrimes } 3961590Srgrimes rewind(fin); 3971590Srgrimes while ((c = getc(fin)) != EOF) 39877274Smikeh (void)putc(c, fout); 39974769Smikeh if (ferror(fout)) { 40074769Smikeh warnx("%s", fname); 40174769Smikeh senderr++; 40277274Smikeh (void)Fclose(fout); 40377274Smikeh (void)Fclose(fin); 40474769Smikeh goto cant; 40574769Smikeh } 40677274Smikeh (void)Fclose(fout); 40777274Smikeh (void)Fclose(fin); 4081590Srgrimes } 4091590Srgrimescant: 4101590Srgrimes /* 4111590Srgrimes * In days of old we removed the entry from the 4121590Srgrimes * the list; now for sake of header expansion 4131590Srgrimes * we leave it in and mark it as deleted. 4141590Srgrimes */ 4151590Srgrimes np->n_type |= GDEL; 4161590Srgrimes np = np->n_flink; 4171590Srgrimes } 4181590Srgrimes if (image >= 0) { 41977274Smikeh (void)close(image); 4201590Srgrimes image = -1; 4211590Srgrimes } 42277274Smikeh return (top); 4231590Srgrimes} 4241590Srgrimes 4251590Srgrimes/* 4261590Srgrimes * Determine if the passed address is a local "send to file" address. 4271590Srgrimes * If any of the network metacharacters precedes any slashes, it can't 4281590Srgrimes * be a filename. We cheat with .'s to allow path names like ./... 4291590Srgrimes */ 4301590Srgrimesint 431216564Scharnierisfileaddr(char *name) 4321590Srgrimes{ 43377274Smikeh char *cp; 4341590Srgrimes 4351590Srgrimes if (*name == '+') 43677274Smikeh return (1); 43777274Smikeh for (cp = name; *cp != '\0'; cp++) { 4381590Srgrimes if (*cp == '!' || *cp == '%' || *cp == '@') 43977274Smikeh return (0); 4401590Srgrimes if (*cp == '/') 44177274Smikeh return (1); 4421590Srgrimes } 44377274Smikeh return (0); 4441590Srgrimes} 4451590Srgrimes 4461590Srgrimes/* 4471590Srgrimes * Map all of the aliased users in the invoker's mailrc 4481590Srgrimes * file and insert them into the list. 4491590Srgrimes * Changed after all these months of service to recursively 4501590Srgrimes * expand names (2/14/80). 4511590Srgrimes */ 4521590Srgrimes 4531590Srgrimesstruct name * 454216564Scharnierusermap(struct name *names) 4551590Srgrimes{ 45677274Smikeh struct name *new, *np, *cp; 4571590Srgrimes struct grouphead *gh; 45877274Smikeh int metoo; 4591590Srgrimes 46077274Smikeh new = NULL; 4611590Srgrimes np = names; 46277274Smikeh metoo = (value("metoo") != NULL); 46377274Smikeh while (np != NULL) { 4641590Srgrimes if (np->n_name[0] == '\\') { 4651590Srgrimes cp = np->n_flink; 4661590Srgrimes new = put(new, np); 4671590Srgrimes np = cp; 4681590Srgrimes continue; 4691590Srgrimes } 4701590Srgrimes gh = findgroup(np->n_name); 4711590Srgrimes cp = np->n_flink; 47277274Smikeh if (gh != NULL) 4731590Srgrimes new = gexpand(new, gh, metoo, np->n_type); 4741590Srgrimes else 4751590Srgrimes new = put(new, np); 4761590Srgrimes np = cp; 4771590Srgrimes } 47877274Smikeh return (new); 4791590Srgrimes} 4801590Srgrimes 4811590Srgrimes/* 4821590Srgrimes * Recursively expand a group name. We limit the expansion to some 4831590Srgrimes * fixed level to keep things from going haywire. 4841590Srgrimes * Direct recursion is not expanded for convenience. 4851590Srgrimes */ 4861590Srgrimes 4871590Srgrimesstruct name * 488216564Scharniergexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype) 4891590Srgrimes{ 4901590Srgrimes struct group *gp; 4911590Srgrimes struct grouphead *ngh; 4921590Srgrimes struct name *np; 4931590Srgrimes static int depth; 4941590Srgrimes char *cp; 4951590Srgrimes 4961590Srgrimes if (depth > MAXEXP) { 4971590Srgrimes printf("Expanding alias to depth larger than %d\n", MAXEXP); 49877274Smikeh return (nlist); 4991590Srgrimes } 5001590Srgrimes depth++; 50177274Smikeh for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) { 5021590Srgrimes cp = gp->ge_name; 5031590Srgrimes if (*cp == '\\') 5041590Srgrimes goto quote; 5051590Srgrimes if (strcmp(cp, gh->g_name) == 0) 5061590Srgrimes goto quote; 50777274Smikeh if ((ngh = findgroup(cp)) != NULL) { 5081590Srgrimes nlist = gexpand(nlist, ngh, metoo, ntype); 5091590Srgrimes continue; 5101590Srgrimes } 5111590Srgrimesquote: 5121590Srgrimes np = nalloc(cp, ntype); 5131590Srgrimes /* 5141590Srgrimes * At this point should allow to expand 5151590Srgrimes * to self if only person in group 5161590Srgrimes */ 51777274Smikeh if (gp == gh->g_list && gp->ge_link == NULL) 5181590Srgrimes goto skip; 5191590Srgrimes if (!metoo && strcmp(cp, myname) == 0) 5201590Srgrimes np->n_type |= GDEL; 5211590Srgrimesskip: 5221590Srgrimes nlist = put(nlist, np); 5231590Srgrimes } 5241590Srgrimes depth--; 52577274Smikeh return (nlist); 5261590Srgrimes} 5271590Srgrimes 5281590Srgrimes/* 5291590Srgrimes * Concatenate the two passed name lists, return the result. 5301590Srgrimes */ 5311590Srgrimesstruct name * 532216564Scharniercat(struct name *n1, struct name *n2) 5331590Srgrimes{ 53477274Smikeh struct name *tail; 5351590Srgrimes 53677274Smikeh if (n1 == NULL) 53777274Smikeh return (n2); 53877274Smikeh if (n2 == NULL) 53977274Smikeh return (n1); 5401590Srgrimes tail = tailof(n1); 5411590Srgrimes tail->n_flink = n2; 5421590Srgrimes n2->n_blink = tail; 54377274Smikeh return (n1); 5441590Srgrimes} 5451590Srgrimes 5461590Srgrimes/* 5471590Srgrimes * Unpack the name list onto a vector of strings. 5481590Srgrimes * Return an error if the name list won't fit. 5491590Srgrimes */ 5501590Srgrimeschar ** 551216564Scharnierunpack(struct name *np) 5521590Srgrimes{ 55377274Smikeh char **ap, **top; 55477274Smikeh struct name *n; 5551590Srgrimes int t, extra, metoo, verbose; 5561590Srgrimes 5571590Srgrimes n = np; 5581590Srgrimes if ((t = count(n)) == 0) 55974769Smikeh errx(1, "No names to unpack"); 5601590Srgrimes /* 5611590Srgrimes * Compute the number of extra arguments we will need. 5621590Srgrimes * We need at least two extra -- one for "mail" and one for 5631590Srgrimes * the terminating 0 pointer. Additional spots may be needed 5641590Srgrimes * to pass along -f to the host mailer. 5651590Srgrimes */ 5661590Srgrimes extra = 2; 5671590Srgrimes extra++; 56877274Smikeh metoo = value("metoo") != NULL; 5691590Srgrimes if (metoo) 5701590Srgrimes extra++; 57177274Smikeh verbose = value("verbose") != NULL; 5721590Srgrimes if (verbose) 5731590Srgrimes extra++; 57477274Smikeh top = (char **)salloc((t + extra) * sizeof(*top)); 5751590Srgrimes ap = top; 5761590Srgrimes *ap++ = "send-mail"; 5771590Srgrimes *ap++ = "-i"; 5781590Srgrimes if (metoo) 5791590Srgrimes *ap++ = "-m"; 5801590Srgrimes if (verbose) 5811590Srgrimes *ap++ = "-v"; 58277274Smikeh for (; n != NULL; n = n->n_flink) 5831590Srgrimes if ((n->n_type & GDEL) == 0) 5841590Srgrimes *ap++ = n->n_name; 58577274Smikeh *ap = NULL; 58677274Smikeh return (top); 5871590Srgrimes} 5881590Srgrimes 5891590Srgrimes/* 5901590Srgrimes * Remove all of the duplicates from the passed name list by 5911590Srgrimes * insertion sorting them, then checking for dups. 5921590Srgrimes * Return the head of the new list. 5931590Srgrimes */ 5941590Srgrimesstruct name * 595216564Scharnierelide(struct name *names) 5961590Srgrimes{ 59777274Smikeh struct name *np, *t, *new; 5981590Srgrimes struct name *x; 5991590Srgrimes 60077274Smikeh if (names == NULL) 60177274Smikeh return (NULL); 6021590Srgrimes new = names; 6031590Srgrimes np = names; 6041590Srgrimes np = np->n_flink; 60577274Smikeh if (np != NULL) 60677274Smikeh np->n_blink = NULL; 60777274Smikeh new->n_flink = NULL; 60877274Smikeh while (np != NULL) { 6091590Srgrimes t = new; 6101590Srgrimes while (strcasecmp(t->n_name, np->n_name) < 0) { 61177274Smikeh if (t->n_flink == NULL) 6121590Srgrimes break; 6131590Srgrimes t = t->n_flink; 6141590Srgrimes } 6151590Srgrimes 6161590Srgrimes /* 6171590Srgrimes * If we ran out of t's, put the new entry after 6181590Srgrimes * the current value of t. 6191590Srgrimes */ 6201590Srgrimes 6211590Srgrimes if (strcasecmp(t->n_name, np->n_name) < 0) { 6221590Srgrimes t->n_flink = np; 6231590Srgrimes np->n_blink = t; 6241590Srgrimes t = np; 6251590Srgrimes np = np->n_flink; 62677274Smikeh t->n_flink = NULL; 6271590Srgrimes continue; 6281590Srgrimes } 6291590Srgrimes 6301590Srgrimes /* 6311590Srgrimes * Otherwise, put the new entry in front of the 6321590Srgrimes * current t. If at the front of the list, 6331590Srgrimes * the new guy becomes the new head of the list. 6341590Srgrimes */ 6351590Srgrimes 6361590Srgrimes if (t == new) { 6371590Srgrimes t = np; 6381590Srgrimes np = np->n_flink; 6391590Srgrimes t->n_flink = new; 6401590Srgrimes new->n_blink = t; 64177274Smikeh t->n_blink = NULL; 6421590Srgrimes new = t; 6431590Srgrimes continue; 6441590Srgrimes } 6451590Srgrimes 6461590Srgrimes /* 6471590Srgrimes * The normal case -- we are inserting into the 6481590Srgrimes * middle of the list. 6491590Srgrimes */ 6501590Srgrimes 6511590Srgrimes x = np; 6521590Srgrimes np = np->n_flink; 6531590Srgrimes x->n_flink = t; 6541590Srgrimes x->n_blink = t->n_blink; 6551590Srgrimes t->n_blink->n_flink = x; 6561590Srgrimes t->n_blink = x; 6571590Srgrimes } 6581590Srgrimes 6591590Srgrimes /* 6601590Srgrimes * Now the list headed up by new is sorted. 6611590Srgrimes * Go through it and remove duplicates. 6621590Srgrimes */ 6631590Srgrimes 6641590Srgrimes np = new; 66577274Smikeh while (np != NULL) { 6661590Srgrimes t = np; 66777274Smikeh while (t->n_flink != NULL && 66877274Smikeh strcasecmp(np->n_name, t->n_flink->n_name) == 0) 6691590Srgrimes t = t->n_flink; 67077274Smikeh if (t == np || t == NULL) { 6711590Srgrimes np = np->n_flink; 6721590Srgrimes continue; 6731590Srgrimes } 6748874Srgrimes 6751590Srgrimes /* 6761590Srgrimes * Now t points to the last entry with the same name 6771590Srgrimes * as np. Make np point beyond t. 6781590Srgrimes */ 6791590Srgrimes 6801590Srgrimes np->n_flink = t->n_flink; 68177274Smikeh if (t->n_flink != NULL) 6821590Srgrimes t->n_flink->n_blink = np; 6831590Srgrimes np = np->n_flink; 6841590Srgrimes } 68577274Smikeh return (new); 6861590Srgrimes} 6871590Srgrimes 6881590Srgrimes/* 6891590Srgrimes * Put another node onto a list of names and return 6901590Srgrimes * the list. 6911590Srgrimes */ 6921590Srgrimesstruct name * 693216564Scharnierput(struct name *list, struct name *node) 6941590Srgrimes{ 6951590Srgrimes node->n_flink = list; 69677274Smikeh node->n_blink = NULL; 69777274Smikeh if (list != NULL) 6981590Srgrimes list->n_blink = node; 69977274Smikeh return (node); 7001590Srgrimes} 7011590Srgrimes 7021590Srgrimes/* 7031590Srgrimes * Determine the number of undeleted elements in 7041590Srgrimes * a name list and return it. 7051590Srgrimes */ 7061590Srgrimesint 707216564Scharniercount(struct name *np) 7081590Srgrimes{ 70977274Smikeh int c; 7101590Srgrimes 71177274Smikeh for (c = 0; np != NULL; np = np->n_flink) 7121590Srgrimes if ((np->n_type & GDEL) == 0) 7131590Srgrimes c++; 71477274Smikeh return (c); 7151590Srgrimes} 7161590Srgrimes 7171590Srgrimes/* 7181590Srgrimes * Delete the given name from a namelist. 7191590Srgrimes */ 7201590Srgrimesstruct name * 721216564Scharnierdelname(struct name *np, char name[]) 7221590Srgrimes{ 72377274Smikeh struct name *p; 7241590Srgrimes 72577274Smikeh for (p = np; p != NULL; p = p->n_flink) 7261590Srgrimes if (strcasecmp(p->n_name, name) == 0) { 72777274Smikeh if (p->n_blink == NULL) { 72877274Smikeh if (p->n_flink != NULL) 72977274Smikeh p->n_flink->n_blink = NULL; 7301590Srgrimes np = p->n_flink; 7311590Srgrimes continue; 7321590Srgrimes } 73377274Smikeh if (p->n_flink == NULL) { 73477274Smikeh if (p->n_blink != NULL) 73577274Smikeh p->n_blink->n_flink = NULL; 7361590Srgrimes continue; 7371590Srgrimes } 7381590Srgrimes p->n_blink->n_flink = p->n_flink; 7391590Srgrimes p->n_flink->n_blink = p->n_blink; 7401590Srgrimes } 74177274Smikeh return (np); 7421590Srgrimes} 7431590Srgrimes 7441590Srgrimes/* 7451590Srgrimes * Pretty print a name list 7461590Srgrimes * Uncomment it if you need it. 7471590Srgrimes */ 7481590Srgrimes 7491590Srgrimes/* 7501590Srgrimesvoid 751216564Scharnierprettyprint(struct name *name) 7521590Srgrimes{ 75377274Smikeh struct name *np; 7541590Srgrimes 7551590Srgrimes np = name; 75677274Smikeh while (np != NULL) { 7571590Srgrimes fprintf(stderr, "%s(%d) ", np->n_name, np->n_type); 7581590Srgrimes np = np->n_flink; 7591590Srgrimes } 7601590Srgrimes fprintf(stderr, "\n"); 7611590Srgrimes} 7621590Srgrimes*/ 763