scansbr: Removed the ``performance hack'' for inc as it wasn't faster.
[mmh] / uip / scansbr.c
index b15c908..7dedf70 100644 (file)
@@ -30,9 +30,7 @@
 /*
 ** Buffer size for content part of header fields.  We want this
 ** to be large enough so that we don't do a lot of extra FLDPLUS
-** calls on m_getfld but small enough so that we don't snarf
-** the entire message body when we're only going to display 30
-** characters of it.
+** calls on m_getfld.
 */
 #define SBUFSIZ 512
 
@@ -67,6 +65,7 @@ int
 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
        int unseen)
 {
+       static int slwidth;
        int i, compnum, state;
        unsigned char *cp, *tmpbuf;
        char **nxtbuf;
@@ -75,11 +74,11 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
        char *scnmsg = NULL;
        FILE *scnout = NULL;
        char name[NAMESZ];
-       static int slwidth;
        char returnpath[BUFSIZ];
        char deliverydate[BUFSIZ];
        int incing = (outnum > 0);
        int scanfolder = (outnum == 0);
+       long fpos;
 
        /* first-time only initialization */
        if (!scanl) {
@@ -91,7 +90,7 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
                }
                dat[3] = slwidth = width;
                scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
-                               (slwidth + 2) );
+                               (slwidth + 2));  /* probably for \n and \0 */
                if (incing)
                        umask(~m_gmprot());
 
@@ -101,13 +100,16 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
                FINDCOMP(datecomp, "date");
                nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
                                sizeof(char *));
-               if (nxtbuf == NULL)
+               if (!nxtbuf)
                        adios(NULL, "unable to allocate component buffers");
                used_buf = (struct comp **) calloc((size_t) (ncomps+1),
-                       sizeof(struct comp *));
-               if (used_buf == NULL)
+                               sizeof(struct comp *));
+               if (!used_buf)
                        adios(NULL, "unable to allocate component buffer stack");
-               used_buf += ncomps+1; *--used_buf = 0;
+               /* NULL-terminate array */
+               used_buf += ncomps;
+               *used_buf = NULL;
+               /* allocate space for the items */
                for (i = ncomps; i--; )
                        *nxtbuf++ = mh_xmalloc(SBUFSIZ);
        }
@@ -121,6 +123,7 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
        dat[0] = innum ? innum : outnum;
        dat[1] = curflg;
        dat[4] = unseen;
+       fpos = ftell(inb);
 
        /*
        ** Get the first field.  If the message is non-empty
@@ -139,7 +142,7 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
                scnmsg = m_name(outnum);
                if (*scnmsg == '?')  /* msg num out of range */
                        return SCNNUM;
-               if ((scnout = fopen(scnmsg, "w")) == NULL)
+               if (!(scnout = fopen(scnmsg, "w")))
                        adios(scnmsg, "unable to write");
                /*
                ** Add the Return-Path and Delivery-Date
@@ -202,46 +205,25 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
                case BODY:
                        compnum = -1;
                        if (!incing) {
-                               state = FILEEOF; /* stop here if scan cmd */
+                               /* stop here if scan cmd */
+                               if (scanfolder) {
+                                       state = FILEEOF;
+                                       goto finished;
+                               }
+                               /* for mboxes: snarf the body */
+                               while (state == BODY) {
+                                       state = m_getfld(state, name, tmpbuf,
+                                                       SBUFSIZ, inb);
+                               }
                                goto finished;
                        }
                        FPUTS("\n");
                        FPUTS(tmpbuf);
-                       /*
-                       ** performance hack: some people like to run "inc"
-                       ** on things like net.sources or large digests.
-                       ** We do a copy directly into the output buffer
-                       ** rather than going through an intermediate buffer.
-                       **
-                       ** We need the amount of data m_getfld found &
-                       ** don't want to do a strlen on the long buffer so
-                       ** there's a hack in m_getfld to save the amount
-                       ** of data it returned in the global "msg_count".
-                       */
 body:;
                        while (state == BODY) {
-#ifdef LINUX_STDIO
-                               if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
-#elif defined(__DragonFly__)
-                               if (((struct __FILE_public *)scnout)->_w <= 0) {
-#else
-                               if (scnout->_cnt <= 0) {
-#endif
-                                       if (fflush(scnout) == EOF)
-                                               adios(scnmsg, "write error on");
-                               }
-#ifdef LINUX_STDIO
-                               state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
-                               scnout->_IO_write_ptr += msg_count;
-#elif defined(__DragonFly__)
-                               state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
-                               ((struct __FILE_public *)scnout)->_w -= msg_count;
-                               ((struct __FILE_public *)scnout)->_p += msg_count;
-#else
-                               state = m_getfld(state, name, scnout->_ptr, -(scnout->_cnt), inb);
-                               scnout->_cnt -= msg_count;
-                               scnout->_ptr += msg_count;
-#endif
+                               state = m_getfld(state, name, tmpbuf, SBUFSIZ,
+                                               inb);
+                               FPUTS(tmpbuf);
                        }
                        goto finished;
 
@@ -278,6 +260,10 @@ finished:
        if (incing) {
                if ((dat[2] = ftell(scnout)) == EOF)
                        adios(scnmsg, "write error on");
+       } else if (!scanfolder) {
+               if ((dat[2] = ftell(inb)) == EOF)
+                       adios(scnmsg, "write error on");
+               dat[2] -= fpos;
        }
 
        if ((datecomp && !datecomp->c_text) || scanfolder) {
@@ -288,9 +274,9 @@ finished:
                        dat[2] = st.st_size;
                if (datecomp) {
                        if (!datecomp->c_text) {
-                               if (datecomp->c_tws == NULL)
+                               if (!datecomp->c_tws)
                                        datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
-                               if (datecomp->c_tws == NULL)
+                               if (!datecomp->c_tws)
                                        adios(NULL, "unable to allocate tws buffer");
                                *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
                                datecomp->c_flags |= CF_DATEFAB|CF_TRUE;