home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / ucbmail / names.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-03  |  17.5 KB  |  967 lines

  1. /*
  2.  *  N A M E S . C 
  3.  *
  4.  *  EE/CIS Computer Lab
  5.  *  Department of Computer and Information Sciences
  6.  *  Department of Electrical Engineering
  7.  *  University of Delaware
  8.  *
  9.  *  REVISION HISTORY:
  10.  *
  11.  *  $Revision: 1.3 $
  12.  *
  13.  *  $Log:    names.c,v $
  14.  * Revision 1.3  86/01/14  16:20:09  galvin
  15.  * Throw away extract and rewrite it.  In order to provide some amount of
  16.  * backward compatibility we make a decision about spaces in mboxes.
  17.  * See extract() for details.
  18.  * 
  19.  * Force detract() to always delimit with ",".
  20.  * 
  21.  * Change outof() to insert a Date field and MMDF delimiters.
  22.  * 
  23.  * Revision 1.2  86/01/10  12:35:44  galvin
  24.  * Added comment header for revision history.
  25.  * 
  26.  *
  27.  */
  28.  
  29. /*
  30.  * Copyright (c) 1980 Regents of the University of California.
  31.  * All rights reserved.  The Berkeley software License Agreement
  32.  * specifies the terms and conditions for redistribution.
  33.  */
  34.  
  35. #ifndef lint
  36. static char *sccsid = "@(#)names.c    5.3 (Berkeley) 11/10/85";
  37. #endif not lint
  38.  
  39. /*
  40.  * Mail -- a mail program
  41.  *
  42.  * Handle name lists.
  43.  */
  44.  
  45. #include "./rcv.h"
  46. #include "./mmdf.h"
  47. #include <ctype.h>
  48.  
  49. /*
  50.  * Allocate a single element of a name list,
  51.  * initialize its name field to the passed
  52.  * name and return it.
  53.  */
  54.  
  55. struct name *
  56. nalloc(str)
  57.     char str[];
  58. {
  59.     register struct name *np;
  60.  
  61.     np = (struct name *) salloc(sizeof *np);
  62.     np->n_flink = NIL;
  63.     np->n_blink = NIL;
  64.     np->n_type = -1;
  65.     np->n_name = savestr(str);
  66.     return(np);
  67. }
  68.  
  69. /*
  70.  * Find the tail of a list and return it.
  71.  */
  72.  
  73. struct name *
  74. tailof(name)
  75.     struct name *name;
  76. {
  77.     register struct name *np;
  78.  
  79.     np = name;
  80.     if (np == NIL)
  81.         return(NIL);
  82.     while (np->n_flink != NIL)
  83.         np = np->n_flink;
  84.     return(np);
  85. }
  86.  
  87. /*
  88.  * Extract a list of names from a line, and make a list of names from it.
  89.  * Return the list or NIL if none found.
  90.  *
  91.  * We take this from LMCL and use some MMDF routines.  We have to make
  92.  * a probably bad decision about spaces.  In order to provide some
  93.  * amount of backward compatibility, we do not allow spaces in "mbox"
  94.  * as returned by parsadr().  If there are spaces we assume that spaces
  95.  * delimit addresses and parse appropriately.  Ditto if parsadr()
  96.  * does not find an mbox and spaces exist in the address being parsed.
  97.  */
  98.  
  99. extern char *adrptr;        /* this is for next_addr() */
  100.  
  101. struct name *
  102. extract(line, ntype)
  103.     char line[];
  104. {
  105.     register char *cp;
  106.     register struct name *top, *np, *t;
  107.     register int ret;
  108.     char nbuf[BUFSIZ], abuf[BUFSIZ], buf[BUFSIZ], thisaddr[BUFSIZ];
  109.  
  110.     if (line == NOSTR || strlen(line) == 0)
  111.         return(NIL);
  112.     top = NIL;
  113.     np = NIL;
  114.     cp = line;
  115.  
  116. #ifdef DEBUG
  117.     if (debug)
  118.         printf("extracting from: \"%s\"\n", line);
  119. #endif DEBUG
  120.     for (adrptr = line, ret = next_address(buf);
  121.             ret >= 0; ret = next_address(buf)) {
  122.         if (ret == 0)
  123.             continue; /* no address found */
  124.         strcpy(thisaddr, buf);
  125.         parsadr(thisaddr, NOSTR, nbuf, abuf);
  126.         if (!*nbuf && !*abuf || index(nbuf, ' '))
  127.             goto spaces;
  128.         strcpy(nbuf, buf);
  129.  
  130.         /* Perform a simple UUCP rewrite into ARPA */
  131.         /* We could probably be smarter here by calling parsadr */
  132.         if (index(nbuf, '@') == NOSTR && index(nbuf, '<') == NOSTR
  133.                 && index(nbuf, ' ') == NOSTR
  134.                 && index(nbuf, '\t') == NOSTR
  135.                 && (cp = index(nbuf, '!')) != NOSTR) {
  136.             *cp = '\0';
  137.             sprintf(buf, "%s@%s.UUCP", cp+1, nbuf);
  138.             strcpy(nbuf, buf);
  139.         }
  140. #ifdef DEBUG
  141.         if (debug)
  142.             printf("extracted: \"%s\"\n", nbuf);
  143. #endif DEBUG
  144.         t = nalloc(nbuf);
  145.         t->n_type = ntype;
  146.         if (top == NIL)
  147.             top = t;
  148.         else
  149.             np->n_flink = t;
  150.         t->n_blink = np;
  151.         np = t;
  152.     }
  153.     return(top);
  154. spaces:
  155. #ifdef DEBUG
  156.     if (debug)
  157.         printf("Assume addresses are delimited by spaces.\n");
  158. #endif DEBUG
  159.     top = NIL;
  160.     np = NIL;
  161.     ret = 0;
  162.  
  163.     for (cp = line; *cp; ++cp) {
  164.         if (!isspace(*cp)) {
  165.             nbuf[ret++] = *cp;
  166.             continue;
  167.         }
  168.         if (ret == 0)
  169.             continue;
  170.         nbuf[ret] = '\0';
  171.         t = nalloc(nbuf);
  172.         t->n_type = ntype;
  173.         if (top == NIL)
  174.             top = t;
  175.         else
  176.             np->n_flink = t;
  177.         t->n_blink = np;
  178.         np = t;
  179.         ret = 0;
  180.     }
  181.     if (ret) {
  182.         nbuf[ret] = '\0';
  183.         t = nalloc(nbuf);
  184.         t->n_type = ntype;
  185.         if (top == NIL)
  186.             top = t;
  187.         else
  188.             np->n_flink = t;
  189.         t->n_blink = np;
  190.     }
  191.     return(top);
  192. }
  193.  
  194. /*
  195.  * Turn a list of names into a string of the same names.
  196.  */
  197.  
  198. char *
  199. detract(np, ntype)
  200.     register struct name *np;
  201. {
  202.     register int s;
  203.     register char *cp, *top;
  204.     register struct name *p;
  205.     register int comma;
  206.  
  207.     comma = 1;
  208.     if (np == NIL)
  209.         return(NOSTR);
  210.     ntype &= ~GCOMMA;
  211.     s = 0;
  212.     if (debug && comma)
  213.         fprintf(stderr, "detract asked to insert commas\n");
  214.     for (p = np; p != NIL; p = p->n_flink) {
  215.         if (ntype && (p->n_type & GMASK) != ntype)
  216.             continue;
  217.         s += strlen(p->n_name) + 1;
  218.         if (comma)
  219.             s++;
  220.     }
  221.     if (s == 0)
  222.         return(NOSTR);
  223.     s += 2;
  224.     top = salloc(s);
  225.     cp = top;
  226.     for (p = np; p != NIL; p = p->n_flink) {
  227.         if (ntype && (p->n_type & GMASK) != ntype)
  228.             continue;
  229.         cp = copy(p->n_name, cp);
  230.         if (comma && p->n_flink != NIL)
  231.             *cp++ = ',';
  232.         *cp++ = ' ';
  233.     }
  234.     *--cp = 0;
  235.     if (comma && *--cp == ',')
  236.         *cp = 0;
  237.     return(top);
  238. }
  239.  
  240. #ifdef NOT_USED
  241. /*
  242.  * Grab a single word (liberal word)
  243.  * Throw away things between ()'s.
  244.  */
  245.  
  246. char *
  247. yankword(ap, wbuf)
  248.     char *ap, wbuf[];
  249. {
  250.     register char *cp, *cp2;
  251.  
  252.     cp = ap;
  253.     do {
  254.         while (*cp && any(*cp, " \t,"))
  255.             cp++;
  256.         if (*cp == '(') {
  257.             register int nesting = 0;
  258.  
  259.             while (*cp != '\0') {
  260.                 switch (*cp++) {
  261.                 case '(':
  262.                     nesting++;
  263.                     break;
  264.                 case ')':
  265.                     --nesting;
  266.                     break;
  267.                 }
  268.                 if (nesting <= 0)
  269.                     break;
  270.             }
  271.         }
  272.         if (*cp == '\0')
  273.             return(NOSTR);
  274.     } while (any(*cp, " \t,("));
  275.     for (cp2 = wbuf; *cp && !any(*cp, " \t,("); *cp2++ = *cp++)
  276.         ;
  277.     *cp2 = '\0';
  278.     return(cp);
  279. }
  280.  
  281. /*
  282.  * Verify that all the users in the list of names are
  283.  * legitimate.  Bitch about and delink those who aren't.
  284.  */
  285.  
  286. struct name *
  287. verify(names)
  288.     struct name *names;
  289. {
  290.     register struct name *np, *top, *t, *x;
  291.     register char *cp;
  292.  
  293. #ifdef SENDMAIL
  294.     return(names);
  295. #else
  296.     top = names;
  297.     np = names;
  298.     while (np != NIL) {
  299.         if (np->n_type & GDEL) {
  300.             np = np->n_flink;
  301.             continue;
  302.         }
  303.         for (cp = "!:@^"; *cp; cp++)
  304.             if (any(*cp, np->n_name))
  305.                 break;
  306.         if (*cp != 0) {
  307.             np = np->n_flink;
  308.             continue;
  309.         }
  310.         cp = np->n_name;
  311.         while (*cp == '\\')
  312.             cp++;
  313.         if (equal(cp, "msgs") ||
  314.             getuserid(cp) != -1) {
  315.             np = np->n_flink;
  316.             continue;
  317.         }
  318.         fprintf(stderr, "Can't send to %s\n", np->n_name);
  319.         senderr++;
  320.         if (np == top) {
  321.             top = np->n_flink;
  322.             if (top != NIL)
  323.                 top->n_blink = NIL;
  324.             np = top;
  325.             continue;
  326.         }
  327.         x = np->n_blink;
  328.         t = np->n_flink;
  329.         x->n_flink = t;
  330.         if (t != NIL)
  331.             t->n_blink = x;
  332.         np = t;
  333.     }
  334.     return(top);
  335. #endif
  336. }
  337. #endif NOT_USED
  338.  
  339. /*
  340.  * For each recipient in the passed name list with a /
  341.  * in the name, append the message to the end of the named file
  342.  * and remove him from the recipient list.
  343.  *
  344.  * Recipients whose name begins with | are piped through the given
  345.  * program and removed.
  346.  */
  347.  
  348. struct name *
  349. outof(names, fo, hp)
  350.     struct name *names;
  351.     FILE *fo;
  352.     struct header *hp;
  353. {
  354.     register int c;
  355.     register struct name *np, *top;
  356.     time_t time();
  357.     time_t now;
  358.     char *date, *fname, *shell, *ctime();
  359.     FILE *fout, *fin;
  360.     int ispipe, s;
  361.     extern char tempEdit[];
  362.  
  363.     top = names;
  364.     np = names;
  365.     time(&now);
  366.     date = ctime(&now);
  367.     while (np != NIL) {
  368.         if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
  369.             np = np->n_flink;
  370.             continue;
  371.         }
  372.         ispipe = np->n_name[0] == '|';
  373.         if (ispipe)
  374.             fname = np->n_name+1;
  375.         else
  376.             fname = expand(np->n_name);
  377.  
  378.         /*
  379.          * See if we have copied the complete message out yet.
  380.          * If not, do so.
  381.          */
  382.  
  383.         if (image < 0) {
  384.             if ((fout = fopen(tempEdit, "a")) == NULL) {
  385.                 perror(tempEdit);
  386.                 senderr++;
  387.                 goto cant;
  388.             }
  389.             image = open(tempEdit, 2);
  390.             unlink(tempEdit);
  391.             if (image < 0) {
  392.                 perror(tempEdit);
  393.                 senderr++;
  394.                 goto cant;
  395.             }
  396.             else {
  397.                 rewind(fo);
  398.                 fprintf(fout, "Date: %s\n", date);
  399.                 puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
  400.                 while ((c = getc(fo)) != EOF)
  401.                     putc(c, fout);
  402.                 rewind(fo);
  403.                 putc('\n', fout);
  404.                 fflush(fout);
  405.                 if (ferror(fout))
  406.                     perror(tempEdit);
  407.                 fclose(fout);
  408.             }
  409.         }
  410.  
  411.         /*
  412.          * Now either copy "image" to the desired file
  413.          * or give it as the standard input to the desired
  414.          * program as appropriate.
  415.          */
  416.  
  417.         if (ispipe) {
  418.             wait(&s);
  419.             switch (fork()) {
  420.             case 0:
  421.                 sigchild();
  422.                 sigsys(SIGHUP, SIG_IGN);
  423.                 sigsys(SIGINT, SIG_IGN);
  424.                 sigsys(SIGQUIT, SIG_IGN);
  425.                 close(0);
  426.                 dup(image);
  427.                 close(image);
  428.                 if ((shell = value("SHELL")) == NOSTR)
  429.                     shell = SHELL;
  430.                 execl(shell, shell, "-c", fname, 0);
  431.                 perror(shell);
  432.                 exit(1);
  433.                 break;
  434.  
  435.             case -1:
  436.                 perror("fork");
  437.                 senderr++;
  438.                 goto cant;
  439.             }
  440.         }
  441.         else {
  442.             if ((fout = fopen(fname, "a")) == NULL) {
  443.                 perror(fname);
  444.                 senderr++;
  445.                 goto cant;
  446.             }
  447.             fin = Fdopen(image, "r");
  448.             if (fin == NULL) {
  449.                 fprintf(stderr, "Can't reopen image\n");
  450.                 fclose(fout);
  451.                 senderr++;
  452.                 goto cant;
  453.             }
  454.             rewind(fin);
  455.             fprintf(fout, "%s", delim1);
  456.             while ((c = getc(fin)) != EOF)
  457.                 putc(c, fout);
  458.             fprintf(fout, "%s", delim2);
  459.             if (ferror(fout))
  460.                 senderr++, perror(fname);
  461.             fclose(fout);
  462.             fclose(fin);
  463.         }
  464.  
  465. cant:
  466.  
  467.         /*
  468.          * In days of old we removed the entry from the
  469.          * the list; now for sake of header expansion
  470.          * we leave it in and mark it as deleted.
  471.          */
  472.  
  473. #ifdef CRAZYWOW
  474.         if (np == top) {
  475.             top = np->n_flink;
  476.             if (top != NIL)
  477.                 top->n_blink = NIL;
  478.             np = top;
  479.             continue;
  480.         }
  481.         x = np->n_blink;
  482.         t = np->n_flink;
  483.         x->n_flink = t;
  484.         if (t != NIL)
  485.             t->n_blink = x;
  486.         np = t;
  487. #endif
  488.  
  489.         np->n_type |= GDEL;
  490.         np = np->n_flink;
  491.     }
  492.     if (image >= 0) {
  493.         close(image);
  494.         image = -1;
  495.     }
  496.     return(top);
  497. }
  498.  
  499. /*
  500.  * Determine if the passed address is a local "send to file" address.
  501.  * If any of the network metacharacters precedes any slashes, it can't
  502.  * be a filename.  We cheat with .'s to allow path names like ./...
  503.  */
  504. isfileaddr(name)
  505.     char *name;
  506. {
  507.     register char *cp;
  508.     extern char *metanet;
  509.  
  510.     if (any('@', name))
  511.         return(0);
  512.     if (*name == '+')
  513.         return(1);
  514.     for (cp = name; *cp; cp++) {
  515.         if (*cp == '.')
  516.             continue;
  517.         if (any(*cp, metanet))
  518.             return(0);
  519.         if (*cp == '/')
  520.             return(1);
  521.     }
  522.     return(0);
  523. }
  524.  
  525. /*
  526.  * Map all of the aliased users in the invoker's mailrc
  527.  * file and insert them into the list.
  528.  * Changed after all these months of service to recursively
  529.  * expand names (2/14/80).
  530.  */
  531.  
  532. struct name *
  533. usermap(names)
  534.     struct name *names;
  535. {
  536.     register struct name *new, *np, *cp;
  537.     struct grouphead *gh;
  538.     register int metoo;
  539.  
  540.     new = NIL;
  541.     np = names;
  542.     metoo = (value("metoo") != NOSTR);
  543.     while (np != NIL) {
  544.         if (np->n_name[0] == '\\') {
  545.             cp = np->n_flink;
  546.             new = put(new, np);
  547.             np = cp;
  548.             continue;
  549.         }
  550.         gh = findgroup(np->n_name);
  551.         cp = np->n_flink;
  552.         if (gh != NOGRP)
  553.             new = gexpand(new, gh, metoo, np->n_type);
  554.         else
  555.             new = put(new, np);
  556.         np = cp;
  557.     }
  558.     return(new);
  559. }
  560.  
  561. /*
  562.  * Recursively expand a group name.  We limit the expansion to some
  563.  * fixed level to keep things from going haywire.
  564.  * Direct recursion is not expanded for convenience.
  565.  */
  566.  
  567. struct name *
  568. gexpand(nlist, gh, metoo, ntype)
  569.     struct name *nlist;
  570.     struct grouphead *gh;
  571. {
  572.     struct group *gp;
  573.     struct grouphead *ngh;
  574.     struct name *np;
  575.     static int depth;
  576.     char *cp;
  577.  
  578.     if (depth > MAXEXP) {
  579.         printf("Expanding alias to depth larger than %d\n", MAXEXP);
  580.         return(nlist);
  581.     }
  582.     depth++;
  583.     for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) {
  584.         cp = gp->ge_name;
  585.         if (*cp == '\\')
  586.             goto quote;
  587.         if (strcmp(cp, gh->g_name) == 0)
  588.             goto quote;
  589.         if ((ngh = findgroup(cp)) != NOGRP) {
  590.             nlist = gexpand(nlist, ngh, metoo, ntype);
  591.             continue;
  592.         }
  593. quote:
  594.         np = nalloc(cp);
  595.         np->n_type = ntype;
  596.         /*
  597.          * At this point should allow to expand
  598.          * to self if only person in group
  599.          */
  600.         if (gp == gh->g_list && gp->ge_link == NOGE)
  601.             goto skip;
  602.         if (!metoo && strcmp(cp, myname) == 0)
  603.             np->n_type |= GDEL;
  604. skip:
  605.         nlist = put(nlist, np);
  606.     }
  607.     depth--;
  608.     return(nlist);
  609. }
  610.  
  611. #ifdef NOT_USED
  612. /*
  613.  * Compute the length of the passed name list and
  614.  * return it.
  615.  */
  616.  
  617. lengthof(name)
  618.     struct name *name;
  619. {
  620.     register struct name *np;
  621.     register int c;
  622.  
  623.     for (c = 0, np = name; np != NIL; c++, np = np->n_flink)
  624.         ;
  625.     return(c);
  626. }
  627. #endif NOT_USED
  628.  
  629. /*
  630.  * Concatenate the two passed name lists, return the result.
  631.  */
  632.  
  633. struct name *
  634. cat(n1, n2)
  635.     struct name *n1, *n2;
  636. {
  637.     register struct name *tail;
  638.  
  639.     if (n1 == NIL)
  640.         return(n2);
  641.     if (n2 == NIL)
  642.         return(n1);
  643.     tail = tailof(n1);
  644.     tail->n_flink = n2;
  645.     n2->n_blink = tail;
  646.     return(n1);
  647. }
  648.  
  649. #ifdef NOT_USED
  650. /*
  651.  * Unpack the name list onto a vector of strings.
  652.  * Return an error if the name list won't fit.
  653.  */
  654.  
  655. char **
  656. unpack(np)
  657.     struct name *np;
  658. {
  659.     register char **ap, **top;
  660.     register struct name *n;
  661.     char *cp;
  662.     char hbuf[10];
  663.     int t, extra;
  664.  
  665.     n = np;
  666.     if ((t = lengthof(n)) == 0)
  667.         panic("No names to unpack");
  668.  
  669.     /*
  670.      * Compute the number of extra arguments we will need.
  671.      * We need at least two extra -- one for "mail" and one for
  672.      * the terminating 0 pointer.  Additional spots may be needed
  673.      * to pass along -r and -f to the host mailer.
  674.      */
  675.  
  676.     extra = 2;
  677.     if (rflag != NOSTR)
  678.         extra += 2;
  679. #ifdef SENDMAIL
  680.     extra++;
  681.     metoo = value("metoo") != NOSTR;
  682.     if (metoo)
  683.         extra++;
  684.     verbose = value("verbose") != NOSTR;
  685.     if (verbose)
  686.         extra++;
  687. #endif SENDMAIL
  688.     if (hflag)
  689.         extra += 2;
  690.     top = (char **) salloc((t + extra) * sizeof cp);
  691.     ap = top;
  692.     *ap++ = "send-mail";
  693.     if (rflag != NOSTR) {
  694.         *ap++ = "-r";
  695.         *ap++ = rflag;
  696.     }
  697. #ifdef SENDMAIL
  698.     *ap++ = "-i";
  699.     if (metoo)
  700.         *ap++ = "-m";
  701.     if (verbose)
  702.         *ap++ = "-v";
  703. #endif SENDMAIL
  704.     if (hflag) {
  705.         *ap++ = "-h";
  706.         sprintf(hbuf, "%d", hflag);
  707.         *ap++ = savestr(hbuf);
  708.     }
  709.     while (n != NIL) {
  710.         if (n->n_type & GDEL) {
  711.             n = n->n_flink;
  712.             continue;
  713.         }
  714.         *ap++ = n->n_name;
  715.         n = n->n_flink;
  716.     }
  717.     *ap = NOSTR;
  718.     return(top);
  719. }
  720.  
  721. /*
  722.  * See if the user named himself as a destination
  723.  * for outgoing mail.  If so, set the global flag
  724.  * selfsent so that we avoid removing his mailbox.
  725.  */
  726.  
  727. mechk(names)
  728.     struct name *names;
  729. {
  730.     register struct name *np;
  731.  
  732.     for (np = names; np != NIL; np = np->n_flink)
  733.         if ((np->n_type & GDEL) == 0 && equal(np->n_name, myname)) {
  734.             selfsent++;
  735.             return;
  736.         }
  737. }
  738. #endif NOT_USED
  739.  
  740. /*
  741.  * Remove all of the duplicates from the passed name list by
  742.  * insertion sorting them, then checking for dups.
  743.  * Return the head of the new list.
  744.  */
  745.  
  746. struct name *
  747. elide(names)
  748.     struct name *names;
  749. {
  750.     register struct name *np, *t, *new;
  751.     struct name *x;
  752.  
  753.     if (names == NIL)
  754.         return(NIL);
  755.     new = names;
  756.     np = names;
  757.     np = np->n_flink;
  758.     if (np != NIL)
  759.         np->n_blink = NIL;
  760.     new->n_flink = NIL;
  761.     while (np != NIL) {
  762.         t = new;
  763.         while (nstrcmp(t->n_name, np->n_name) < 0) {
  764.             if (t->n_flink == NIL)
  765.                 break;
  766.             t = t->n_flink;
  767.         }
  768.  
  769.         /*
  770.          * If we ran out of t's, put the new entry after
  771.          * the current value of t.
  772.          */
  773.  
  774.         if (nstrcmp(t->n_name, np->n_name) < 0) {
  775.             t->n_flink = np;
  776.             np->n_blink = t;
  777.             t = np;
  778.             np = np->n_flink;
  779.             t->n_flink = NIL;
  780.             continue;
  781.         }
  782.  
  783.         /*
  784.          * Otherwise, put the new entry in front of the
  785.          * current t.  If at the front of the list,
  786.          * the new guy becomes the new head of the list.
  787.          */
  788.  
  789.         if (t == new) {
  790.             t = np;
  791.             np = np->n_flink;
  792.             t->n_flink = new;
  793.             new->n_blink = t;
  794.             t->n_blink = NIL;
  795.             new = t;
  796.             continue;
  797.         }
  798.  
  799.         /*
  800.          * The normal case -- we are inserting into the
  801.          * middle of the list.
  802.          */
  803.  
  804.         x = np;
  805.         np = np->n_flink;
  806.         x->n_flink = t;
  807.         x->n_blink = t->n_blink;
  808.         t->n_blink->n_flink = x;
  809.         t->n_blink = x;
  810.     }
  811.  
  812.     /*
  813.      * Now the list headed up by new is sorted.
  814.      * Go through it and remove duplicates.
  815.      */
  816.  
  817.     np = new;
  818.     while (np != NIL) {
  819.         t = np;
  820.         while (t->n_flink!=NIL &&
  821.             icequal(np->n_name,t->n_flink->n_name))
  822.             t = t->n_flink;
  823.         if (t == np || t == NIL) {
  824.             np = np->n_flink;
  825.             continue;
  826.         }
  827.         
  828.         /*
  829.          * Now t points to the last entry with the same name
  830.          * as np.  Make np point beyond t.
  831.          */
  832.  
  833.         np->n_flink = t->n_flink;
  834.         if (t->n_flink != NIL)
  835.             t->n_flink->n_blink = np;
  836.         np = np->n_flink;
  837.     }
  838.     return(new);
  839. }
  840.  
  841. /*
  842.  * Version of strcmp which ignores case differences.
  843.  */
  844.  
  845. nstrcmp(s1, s2)
  846.     register char *s1, *s2;
  847. {
  848.     register int c1, c2;
  849.  
  850.     do {
  851.         c1 = *s1++;
  852.         c2 = *s2++;
  853.     } while (c1 && c1 == c2);
  854.     return(c1 - c2);
  855. }
  856.  
  857. /*
  858.  * Put another node onto a list of names and return
  859.  * the list.
  860.  */
  861.  
  862. struct name *
  863. put(list, node)
  864.     struct name *list, *node;
  865. {
  866.     node->n_flink = list;
  867.     node->n_blink = NIL;
  868.     if (list != NIL)
  869.         list->n_blink = node;
  870.     return(node);
  871. }
  872.  
  873. /*
  874.  * Determine the number of elements in
  875.  * a name list and return it.
  876.  */
  877.  
  878. count(np)
  879.     register struct name *np;
  880. {
  881.     register int c = 0;
  882.  
  883.     while (np != NIL) {
  884.         c++;
  885.         np = np->n_flink;
  886.     }
  887.     return(c);
  888. }
  889.  
  890. #ifdef NOT_USED
  891. cmpdomain(name, dname)
  892.     register char *name, *dname;
  893. {
  894.     char buf[BUFSIZ];
  895.  
  896.     strcpy(buf, dname);
  897.     buf[strlen(name)] = '\0';
  898.     return(icequal(name, buf));
  899. }
  900. #endif NOT_USED
  901.  
  902. /*
  903.  * Delete the given name from a namelist, using the passed
  904.  * function to compare the names.
  905.  */
  906. struct name *
  907. delname(np, name, cmpfun)
  908.     register struct name *np;
  909.     char name[];
  910.     int (* cmpfun)();
  911. {
  912.     register struct name *p;
  913.  
  914.     for (p = np; p != NIL; p = p->n_flink)
  915.         if ((* cmpfun)(p->n_name, name)) {
  916.             if (p->n_blink == NIL) {
  917.                 if (p->n_flink != NIL)
  918.                     p->n_flink->n_blink = NIL;
  919.                 np = p->n_flink;
  920.                 continue;
  921.             }
  922.             if (p->n_flink == NIL) {
  923.                 if (p->n_blink != NIL)
  924.                     p->n_blink->n_flink = NIL;
  925.                 continue;
  926.             }
  927.             p->n_blink->n_flink = p->n_flink;
  928.             p->n_flink->n_blink = p->n_blink;
  929.         }
  930.     return(np);
  931. }
  932.  
  933. /*
  934.  * Call the given routine on each element of the name
  935.  * list, replacing said value if need be.
  936.  */
  937.  
  938. mapf(np, from)
  939.     register struct name *np;
  940.     char *from;
  941. {
  942.     register struct name *p;
  943.  
  944.     for (p = np; p != NIL; p = p->n_flink)
  945.         p->n_name = netmap(p->n_name, from);
  946. }
  947.  
  948. #ifdef NOT_USED
  949. /*
  950.  * Pretty print a name list
  951.  * Uncomment it if you need it.
  952.  */
  953.  
  954. prettyprint(name)
  955.     struct name *name;
  956. {
  957.     register struct name *np;
  958.  
  959.     np = name;
  960.     while (np != NIL) {
  961.         fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
  962.         np = np->n_flink;
  963.     }
  964.     fprintf(stderr, "\n");
  965. }
  966. #endif NOT_USED
  967.