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