No error-checking for error-checking alloc functions
[mmh] / uip / sortm.c
index 0d02af9..43dc7dc 100644 (file)
@@ -197,7 +197,7 @@ main(int argc, char **argv)
        /*
        ** sort a list of pointers to our "messages to be sorted".
        */
-       dlist = (struct smsg **) mh_xmalloc((nmsgs+1) * sizeof(*dlist));
+       dlist = (struct smsg **) mh_xcalloc((size_t) (nmsgs+1), sizeof(*dlist));
        for (i = 0; i < nmsgs; i++)
                dlist[i] = &smsgs[i];
        dlist[nmsgs] = 0;
@@ -235,7 +235,7 @@ main(int argc, char **argv)
                ** the collection of messages with the same subj
                ** given a message number.
                */
-               il = (struct smsg ***) calloc(mp->hghsel+1, sizeof(*il));
+               il = (struct smsg ***) mh_xcalloc(mp->hghsel+1, sizeof(*il));
                if (! il)
                        adios(EX_OSERR, NULL, "couldn't allocate msg list");
                for (i = 0; i < nmsgs; i++)
@@ -295,10 +295,8 @@ read_hdrs(struct msgs *mp, char *datesw)
 
        twscopy(&tb, dlocaltimenow());
 
-       smsgs = (struct smsg *) calloc((size_t) (mp->hghsel - mp->lowsel + 2),
+       smsgs = (struct smsg *) mh_xcalloc((size_t) (mp->hghsel - mp->lowsel + 2),
                        sizeof(*smsgs));
-       if (smsgs == NULL)
-               adios(EX_OSERR, NULL, "unable to allocate sort storage");
 
        s = smsgs;
        for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
@@ -322,9 +320,10 @@ read_hdrs(struct msgs *mp, char *datesw)
 static int
 get_fields(char *datesw, int msg, struct smsg *smsg)
 {
-       int state;
+       enum state state;
+       struct field f = {{0}};
        int compnum;
-       char *msgnam, buf[BUFSIZ], nam[NAMESZ];
+       char *msgnam;
        struct tws *tw;
        char *datecomp = NULL, *subjcomp = NULL;
        FILE *in;
@@ -333,51 +332,37 @@ get_fields(char *datesw, int msg, struct smsg *smsg)
                admonish(msgnam, "unable to read message");
                return (0);
        }
-       for (compnum = 1, state = FLD;;) {
-               switch (state = m_getfld(state, nam, buf, sizeof(buf), in)) {
-               case FLD:
-               case FLDEOF:
-               case FLDPLUS:
-                       compnum++;
-                       if (!mh_strcasecmp(nam, datesw)) {
-                               datecomp = add(buf, datecomp);
-                               while (state == FLDPLUS) {
-                                       state = m_getfld(state, nam, buf,
-                                                       sizeof(buf), in);
-                                       datecomp = add(buf, datecomp);
-                               }
-                               if (!subjsort || subjcomp)
+       for (compnum = 1, state = FLD2;; compnum++) {
+               switch (state = m_getfld2(state, &f, in)) {
+               case FLD2:
+                       if (mh_strcasecmp(f.name, datesw)==0) {
+                               datecomp = getcpy(f.value);
+                               if (!subjsort || subjcomp) {
                                        break;
-                       } else if (subjsort && !mh_strcasecmp(nam, subjsort)) {
-                               subjcomp = add(buf, subjcomp);
-                               while (state == FLDPLUS) {
-                                       state = m_getfld(state, nam, buf,
-                                                       sizeof(buf), in);
-                                       subjcomp = add(buf, subjcomp);
                                }
-                               if (datecomp)
+                       } else if (subjsort && mh_strcasecmp(f.name,
+                                       subjsort)==0) {
+                               subjcomp = getcpy(f.value);
+                               if (datecomp) {
                                        break;
-                       } else {
-                               /* just flush this guy */
-                               while (state == FLDPLUS)
-                                       state = m_getfld(state, nam, buf,
-                                                       sizeof(buf), in);
+                               }
                        }
                        continue;
 
-               case BODY:
-               case BODYEOF:
-               case FILEEOF:
+               case BODY2:
+               case FILEEOF2:
                        break;
 
-               case LENERR:
-               case FMTERR:
-                       if (state == LENERR || state == FMTERR)
-                               admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
-                       if (datecomp)
+               case LENERR2:
+               case FMTERR2:
+               case ERR2:
+                       admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
+                       if (datecomp) {
                                free(datecomp);
-                       if (subjcomp)
+                       }
+                       if (subjcomp) {
                                free(subjcomp);
+                       }
                        fclose(in);
                        return (0);