* uip/mhlistsbr.c, uip/mhlsbr.c, uip/picksbr.c: cast
[mmh] / uip / replsbr.c
index 1ea411c..a415346 100644 (file)
@@ -12,7 +12,9 @@
 #include <h/mh.h>
 #include <h/addrsbr.h>
 #include <h/fmt_scan.h>
+#include <h/utils.h>
 #include <sys/file.h>          /* L_SET */
+#include <errno.h>
 
 extern short ccto;             /* from repl.c */
 extern short cccc;
@@ -76,14 +78,17 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     register char **nxtbuf;
     register char **ap;
     register struct comp **savecomp;
-    int        char_read = 0, format_len;
-    char name[NAMESZ], *scanl, *cp;
+    int        char_read = 0, format_len, mask;
+    char name[NAMESZ], *scanl;
+    unsigned char *cp;
     FILE *out;
 
-    umask(~m_gmprot());
+    mask = umask(~m_gmprot());
     if ((out = fopen (drft, "w")) == NULL)
        adios (drft, "unable to create");
 
+    umask(mask);
+
     /* get new format string */
     cp = new_fs (form, NULL, NULL);
     format_len = strlen (cp);
@@ -101,8 +106,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     *--savecomp = NULL;                /* point at zero'd end minus 1 */
 
     for (i = ncomps; i--; )
-       if (!(*nxtbuf++ = malloc(SBUFSIZ)))
-           adios (NULL, "unable to allocate component buffer");
+       *nxtbuf++ = mh_xmalloc(SBUFSIZ);
 
     nxtbuf = compbuffers;              /* point at start */
     tmpbuf = *nxtbuf++;
@@ -156,10 +160,12 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                 */
                if ((cptr = wantcomp[CHASH(name)]))
                    do {
-                       if (!strcasecmp(name, cptr->c_name)) {
+                       if (!mh_strcasecmp(name, cptr->c_name)) {
                            char_read += msg_count;
                            if (! cptr->c_text) {
-                               cptr->c_text = tmpbuf;
+                               i = strlen(cptr->c_text = tmpbuf) - 1;
+                               if (tmpbuf[i] == '\n')
+                                   tmpbuf[i] = '\0';
                                *--savecomp = cptr;
                                tmpbuf = *nxtbuf++;
                            } else {
@@ -227,7 +233,7 @@ finished:
        }
     }
     i = format_len + char_read + 256;
-    scanl = malloc ((size_t) i + 2);
+    scanl = mh_xmalloc ((size_t) i + 2);
     dat[0] = 0;
     dat[1] = 0;
     dat[2] = 0;
@@ -245,12 +251,17 @@ finished:
      * or add mhn directives
      */
     if (filter) {
+       fflush(out);
+       if (ferror (out))
+           adios (drft, "error writing");
+       
        replfilter (inb, out, filter);
     } else if (mime && mp) {
            fprintf (out, "#forw [original message] +%s %s\n",
                     mp->foldpath, m_name (mp->lowsel));
     }
 
+    fflush(out);
     if (ferror (out))
        adios (drft, "error writing");
     fclose (out);
@@ -283,11 +294,9 @@ static unsigned int bufsiz=0;      /* current size of buf */
                int i = dst - buf;\
                int n = last_dst - buf;\
                bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
-               buf = realloc (buf, bufsiz);\
+               buf = mh_xrealloc (buf, bufsiz);\
                dst = buf + i;\
                last_dst = buf + n;\
-               if (! buf)\
-                   adios (NULL, "formataddr: couldn't get buffer space");\
                bufend = buf + bufsiz;\
            }
 
@@ -316,9 +325,7 @@ formataddr (char *orig, char *str)
 
     /* if we don't have a buffer yet, get one */
     if (bufsiz == 0) {
-       buf = malloc (BUFINCR);
-       if (! buf)
-           adios (NULL, "formataddr: couldn't allocate buffer space");
+       buf = mh_xmalloc (BUFINCR);
        last_dst = buf;         /* XXX */
        bufsiz = BUFINCR - 6;  /* leave some slop */
        bufend = buf + bufsiz;
@@ -385,8 +392,8 @@ insert (struct mailname *np)
        return 0;
 
     for (mp = &mq; mp->m_next; mp = mp->m_next) {
-       if (!strcasecmp (np->m_host, mp->m_next->m_host)
-               && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
+       if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
+               && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
            return 0;
     }
     if (!ccme && ismymbox (np))
@@ -410,6 +417,8 @@ insert (struct mailname *np)
 
 /*
  * Call the mhlproc
+ *
+ * This function expects that argument out has been fflushed by the caller.
  */
 
 static void
@@ -417,6 +426,7 @@ replfilter (FILE *in, FILE *out, char *filter)
 {
     int        pid;
     char *mhl;
+    char *errstr;
 
     if (filter == NULL)
        return;
@@ -428,7 +438,6 @@ replfilter (FILE *in, FILE *out, char *filter)
 
     rewind (in);
     lseek (fileno(in), (off_t) 0, SEEK_SET);
-    fflush (out);
 
     switch (pid = vfork ()) {
        case NOTOK: 
@@ -440,8 +449,12 @@ replfilter (FILE *in, FILE *out, char *filter)
            closefds (3);
 
            execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
-           fprintf (stderr, "unable to exec ");
-           perror (mhlproc);
+           errstr = strerror(errno);
+           write(2, "unable to exec ", 15);
+           write(2, mhlproc, strlen(mhlproc));
+           write(2, ": ", 2);
+           write(2, errstr, strlen(errstr));
+           write(2, "\n", 1);
            _exit (-1);
 
        default: