BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / mail / names.c
index 1392c50..a9f3dbb 100644 (file)
@@ -2,21 +2,23 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)names.c    5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)names.c    5.16 (Berkeley) 6/25/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -26,7 +28,6 @@ static char sccsid[] = "@(#)names.c   5.10 (Berkeley) %G%";
  */
 
 #include "rcv.h"
  */
 
 #include "rcv.h"
-#include <sys/wait.h>
 
 /*
  * Allocate a single element of a name list,
 
 /*
  * Allocate a single element of a name list,
@@ -177,7 +178,7 @@ yankword(ap, wbuf)
                for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
                        ;
        else
                for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
                        ;
        else
-               for (cp2 = wbuf; *cp && !any(*cp, " \t,("); *cp2++ = *cp++)
+               for (cp2 = wbuf; *cp && !index(" \t,(", *cp); *cp2++ = *cp++)
                        ;
        *cp2 = '\0';
        return cp;
                        ;
        *cp2 = '\0';
        return cp;
@@ -199,15 +200,11 @@ outof(names, fo, hp)
 {
        register int c;
        register struct name *np, *top;
 {
        register int c;
        register struct name *np, *top;
-#ifdef CRAZYWOW
-       register struct name *t, *x;
-#endif
        time_t now, time();
        time_t now, time();
-       char *date, *fname, *shell, *ctime();
+       char *date, *fname, *ctime();
        FILE *fout, *fin;
        int ispipe;
        extern char tempEdit[];
        FILE *fout, *fin;
        int ispipe;
        extern char tempEdit[];
-       union wait s;
 
        top = names;
        np = names;
 
        top = names;
        np = names;
@@ -230,7 +227,7 @@ outof(names, fo, hp)
                 */
 
                if (image < 0) {
                 */
 
                if (image < 0) {
-                       if ((fout = fopen(tempEdit, "a")) == NULL) {
+                       if ((fout = Fopen(tempEdit, "a")) == NULL) {
                                perror(tempEdit);
                                senderr++;
                                goto cant;
                                perror(tempEdit);
                                senderr++;
                                goto cant;
@@ -240,21 +237,19 @@ outof(names, fo, hp)
                        if (image < 0) {
                                perror(tempEdit);
                                senderr++;
                        if (image < 0) {
                                perror(tempEdit);
                                senderr++;
+                               (void) Fclose(fout);
                                goto cant;
                        }
                                goto cant;
                        }
-                       else {
-                               rewind(fo);
-                               fprintf(fout, "From %s %s", myname, date);
-                               puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
-                               while ((c = getc(fo)) != EOF)
-                                       (void) putc(c, fout);
-                               rewind(fo);
-                               (void) putc('\n', fout);
-                               (void) fflush(fout);
-                               if (ferror(fout))
-                                       perror(tempEdit);
-                               (void) fclose(fout);
-                       }
+                       fprintf(fout, "From %s %s", myname, date);
+                       puthead(hp, fout, GTO|GSUBJECT|GCC|GNL);
+                       while ((c = getc(fo)) != EOF)
+                               (void) putc(c, fout);
+                       rewind(fo);
+                       (void) putc('\n', fout);
+                       (void) fflush(fout);
+                       if (ferror(fout))
+                               perror(tempEdit);
+                       (void) Fclose(fout);
                }
 
                /*
                }
 
                /*
@@ -264,38 +259,41 @@ outof(names, fo, hp)
                 */
 
                if (ispipe) {
                 */
 
                if (ispipe) {
-                       (void) wait(&s);
-                       switch (fork()) {
-                       case 0:
-                               (void) signal(SIGHUP, SIG_IGN);
-                               (void) signal(SIGINT, SIG_IGN);
-                               (void) signal(SIGQUIT, SIG_IGN);
-                               (void) close(0);
-                               (void) dup(image);
-                               (void) close(image);
-                               if ((shell = value("SHELL")) == NOSTR)
-                                       shell = SHELL;
-                               execl(shell, shell, "-c", fname, 0);
-                               perror(shell);
-                               exit(1);
-                               break;
-
-                       case -1:
-                               perror("fork");
+                       int pid;
+                       char *shell;
+
+                       /*
+                        * XXX
+                        * We can't really reuse the same image file,
+                        * because multiple piped recipients will
+                        * share the same lseek location and trample
+                        * on one another.
+                        */
+                       if ((shell = value("SHELL")) == NOSTR)
+                               shell = _PATH_CSHELL;
+                       pid = start_command(shell, sigmask(SIGHUP)|
+                                       sigmask(SIGINT)|sigmask(SIGQUIT),
+                               image, -1, "-c", fname, NOSTR);
+                       if (pid < 0) {
                                senderr++;
                                goto cant;
                        }
                                senderr++;
                                goto cant;
                        }
-               }
-               else {
-                       if ((fout = fopen(fname, "a")) == NULL) {
+                       free_child(pid);
+               } else {
+                       int f;
+                       if ((fout = Fopen(fname, "a")) == NULL) {
                                perror(fname);
                                senderr++;
                                goto cant;
                        }
                                perror(fname);
                                senderr++;
                                goto cant;
                        }
-                       fin = Fdopen(image, "r");
+                       if ((f = dup(image)) < 0) {
+                               perror("dup");
+                               fin = NULL;
+                       } else
+                               fin = Fdopen(f, "r");
                        if (fin == NULL) {
                                fprintf(stderr, "Can't reopen image\n");
                        if (fin == NULL) {
                                fprintf(stderr, "Can't reopen image\n");
-                               (void) fclose(fout);
+                               (void) Fclose(fout);
                                senderr++;
                                goto cant;
                        }
                                senderr++;
                                goto cant;
                        }
@@ -304,34 +302,15 @@ outof(names, fo, hp)
                                (void) putc(c, fout);
                        if (ferror(fout))
                                senderr++, perror(fname);
                                (void) putc(c, fout);
                        if (ferror(fout))
                                senderr++, perror(fname);
-                       (void) fclose(fout);
-                       (void) fclose(fin);
+                       (void) Fclose(fout);
+                       (void) Fclose(fin);
                }
                }
-
 cant:
 cant:
-
                /*
                 * In days of old we removed the entry from the
                 * the list; now for sake of header expansion
                 * we leave it in and mark it as deleted.
                 */
                /*
                 * In days of old we removed the entry from the
                 * the list; now for sake of header expansion
                 * we leave it in and mark it as deleted.
                 */
-
-#ifdef CRAZYWOW
-               if (np == top) {
-                       top = np->n_flink;
-                       if (top != NIL)
-                               top->n_blink = NIL;
-                       np = top;
-                       continue;
-               }
-               x = np->n_blink;
-               t = np->n_flink;
-               x->n_flink = t;
-               if (t != NIL)
-                       t->n_blink = x;
-               np = t;
-#endif
-
                np->n_type |= GDEL;
                np = np->n_flink;
        }
                np->n_type |= GDEL;
                np = np->n_flink;
        }
@@ -504,14 +483,9 @@ unpack(np)
                *ap++ = "-m";
        if (verbose)
                *ap++ = "-v";
                *ap++ = "-m";
        if (verbose)
                *ap++ = "-v";
-       while (n != NIL) {
-               if (n->n_type & GDEL) {
-                       n = n->n_flink;
-                       continue;
-               }
-               *ap++ = n->n_name;
-               n = n->n_flink;
-       }
+       for (; n != NIL; n = n->n_flink)
+               if ((n->n_type & GDEL) == 0)
+                       *ap++ = n->n_name;
        *ap = NOSTR;
        return(top);
 }
        *ap = NOSTR;
        return(top);
 }
@@ -538,7 +512,7 @@ elide(names)
        new->n_flink = NIL;
        while (np != NIL) {
                t = new;
        new->n_flink = NIL;
        while (np != NIL) {
                t = new;
-               while (nstrcmp(t->n_name, np->n_name) < 0) {
+               while (strcasecmp(t->n_name, np->n_name) < 0) {
                        if (t->n_flink == NIL)
                                break;
                        t = t->n_flink;
                        if (t->n_flink == NIL)
                                break;
                        t = t->n_flink;
@@ -549,7 +523,7 @@ elide(names)
                 * the current value of t.
                 */
 
                 * the current value of t.
                 */
 
-               if (nstrcmp(t->n_name, np->n_name) < 0) {
+               if (strcasecmp(t->n_name, np->n_name) < 0) {
                        t->n_flink = np;
                        np->n_blink = t;
                        t = np;
                        t->n_flink = np;
                        np->n_blink = t;
                        t = np;
@@ -595,8 +569,8 @@ elide(names)
        np = new;
        while (np != NIL) {
                t = np;
        np = new;
        while (np != NIL) {
                t = np;
-               while (t->n_flink!=NIL &&
-                   icequal(np->n_name,t->n_flink->n_name))
+               while (t->n_flink != NIL &&
+                      strcasecmp(np->n_name, t->n_flink->n_name) == 0)
                        t = t->n_flink;
                if (t == np || t == NIL) {
                        np = np->n_flink;
                        t = t->n_flink;
                if (t == np || t == NIL) {
                        np = np->n_flink;
@@ -616,21 +590,6 @@ elide(names)
        return(new);
 }
 
        return(new);
 }
 
-/*
- * Version of strcmp which ignores case differences.
- */
-nstrcmp(s1, s2)
-       register char *s1, *s2;
-{
-       register int c1, c2;
-
-       do {
-               c1 = *s1++;
-               c2 = *s2++;
-       } while (c1 && c1 == c2);
-       return(c1 - c2);
-}
-
 /*
  * Put another node onto a list of names and return
  * the list.
 /*
  * Put another node onto a list of names and return
  * the list.
@@ -647,7 +606,7 @@ put(list, node)
 }
 
 /*
 }
 
 /*
- * Determine the number of elements in
+ * Determine the number of undeleted elements in
  * a name list and return it.
  */
 count(np)
  * a name list and return it.
  */
 count(np)
@@ -655,25 +614,24 @@ count(np)
 {
        register int c;
 
 {
        register int c;
 
-       for (c = 0; np != NIL; c++, np = np->n_flink)
-               ;
+       for (c = 0; np != NIL; np = np->n_flink)
+               if ((np->n_type & GDEL) == 0)
+                       c++;
        return c;
 }
 
 /*
        return c;
 }
 
 /*
- * Delete the given name from a namelist, using the passed
- * function to compare the names.
+ * Delete the given name from a namelist.
  */
 struct name *
  */
 struct name *
-delname(np, name, cmpfun)
+delname(np, name)
        register struct name *np;
        char name[];
        register struct name *np;
        char name[];
-       int (* cmpfun)();
 {
        register struct name *p;
 
        for (p = np; p != NIL; p = p->n_flink)
 {
        register struct name *p;
 
        for (p = np; p != NIL; p = p->n_flink)
-               if ((* cmpfun)(p->n_name, name)) {
+               if (strcasecmp(p->n_name, name) == 0) {
                        if (p->n_blink == NIL) {
                                if (p->n_flink != NIL)
                                        p->n_flink->n_blink = NIL;
                        if (p->n_blink == NIL) {
                                if (p->n_flink != NIL)
                                        p->n_flink->n_blink = NIL;
@@ -688,7 +646,7 @@ delname(np, name, cmpfun)
                        p->n_blink->n_flink = p->n_flink;
                        p->n_flink->n_blink = p->n_blink;
                }
                        p->n_blink->n_flink = p->n_flink;
                        p->n_flink->n_blink = p->n_blink;
                }
-       return(np);
+       return np;
 }
 
 /*
 }
 
 /*