]> diplodocus.org Git - nmh/blobdiff - sbr/seq_read.c
Fixed memory management in mhstore. The main problem was a double
[nmh] / sbr / seq_read.c
index 1b6844133db8a43a82121fca7ca1a23942b33200..9b071a4e174bb42c0905ffe28a7576612ff631c5 100644 (file)
@@ -15,7 +15,7 @@
  * static prototypes
  */
 static int seq_init (struct msgs *, char *, char *);
-static void seq_public (struct msgs *);
+static int seq_public (struct msgs *, int, int *);
 static void seq_private (struct msgs *);
 
 
@@ -25,26 +25,31 @@ static void seq_private (struct msgs *);
  * or context file (for private sequences).
  */
 
-void
-seq_read (struct msgs *mp)
+int
+seq_read (struct msgs *mp, int lockflag)
 {
+    int failed_to_lock = 0;
+
     /*
      * Initialize the list of sequence names.  Go ahead and
      * add the "cur" sequence to the list of sequences.
      */
-    mp->msgattrs[0] = getcpy (current);
-    mp->msgattrs[1] = NULL;
+    svector_push_back (mp->msgattrs, getcpy (current));
     make_all_public (mp);      /* initially, make all public */
 
     /* If folder is empty, don't scan for sequence information */
     if (mp->nummsg == 0)
-       return;
+       return OK;
 
     /* Initialize the public sequences */
-    seq_public (mp);
+    if (seq_public (mp, lockflag, &failed_to_lock) == NOTOK) {
+       if (failed_to_lock) return NOTOK;
+    }
 
     /* Initialize the private sequences */
     seq_private (mp);
+
+    return OK;
 }
 
 
@@ -52,42 +57,41 @@ seq_read (struct msgs *mp)
  * read folder's sequences file for public sequences
  */
 
-static void
-seq_public (struct msgs *mp)
+static int
+seq_public (struct msgs *mp, int lockflag, int *failed_to_lock)
 {
     int state;
     char *cp, seqfile[PATH_MAX];
     char name[NAMESZ], field[BUFSIZ];
     FILE *fp;
-    m_getfld_state_t gstate;
+    m_getfld_state_t gstate = 0;
 
     /*
-     * If mh_seq == NULL (such as if nmh been compiled with
-     * NOPUBLICSEQ), or if *mh_seq == '\0' (the user has defined
+     * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
      * the "mh-sequences" profile entry, but left it empty),
      * then just return, and do not initialize any public sequences.
      */
     if (mh_seq == NULL || *mh_seq == '\0')
-       return;
+       return OK;
 
     /* get filename of sequence file */
     snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
 
-    if ((fp = lkfopen (seqfile, "r")) == NULL)
-       return;
+    if ((fp = lkfopendata (seqfile, lockflag ? "r+" : "r", failed_to_lock))
+       == NULL)
+       return NOTOK;
 
     /* Use m_getfld to scan sequence file */
-    m_getfld_state_init (&gstate);
     for (;;) {
        int fieldsz = sizeof field;
-       switch (state = m_getfld (gstate, name, field, &fieldsz, fp)) {
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
            case FLD: 
            case FLDPLUS:
                if (state == FLDPLUS) {
                    cp = getcpy (field);
                    while (state == FLDPLUS) {
                        fieldsz = sizeof field;
-                       state = m_getfld (gstate, name, field, &fieldsz, fp);
+                       state = m_getfld (&gstate, name, field, &fieldsz, fp);
                        cp = add (field, cp);
                    }
                    seq_init (mp, getcpy (name), trimcpy (cp));
@@ -97,7 +101,8 @@ seq_public (struct msgs *mp)
                }
                continue;
 
-           case BODY: 
+           case BODY:
+               lkfclosedata (fp, seqfile);
                adios (NULL, "no blank lines are permitted in %s", seqfile);
                /* fall */
 
@@ -105,13 +110,21 @@ seq_public (struct msgs *mp)
                break;
 
            default: 
+               lkfclosedata (fp, seqfile);
                adios (NULL, "%s is poorly formatted", seqfile);
        }
        break;  /* break from for loop */
     }
     m_getfld_state_destroy (&gstate);
 
-    lkfclose (fp, seqfile);
+    if (lockflag) {
+       mp->seqhandle = fp;
+       mp->seqname = getcpy(seqfile);
+    } else {
+       lkfclosedata (fp, seqfile);
+    }
+
+    return OK;
 }
 
 
@@ -175,30 +188,22 @@ seq_init (struct msgs *mp, char *name, char *field)
      * then clear the bit for this sequence from all the
      * mesages in this folder.
      */
-    for (i = 0; mp->msgattrs[i]; i++) {
-       if (!strcmp (mp->msgattrs[i], name)) {
+    for (i = 0; i < svector_size (mp->msgattrs); i++) {
+       if (!strcmp (svector_at (mp->msgattrs, i), name)) {
            for (j = mp->lowmsg; j <= mp->hghmsg; j++)
                clear_sequence (mp, i, j);
            break;
        }
     }
 
-    /* Return error, if too many sequences */
-    if (i >= NUMATTRS) {
-       free (name);
-       free (field);
-       return -1;
-    }
-
     /*
      * If we've already seen this sequence name, just free the
      * name string.  Else add it to the list of sequence names.
      */
-    if (mp->msgattrs[i]) {
+    if (svector_at (mp->msgattrs, i)) {
        free (name);
     } else {
-       mp->msgattrs[i] = name;
-       mp->msgattrs[i + 1] = NULL;
+       svector_push_back (mp->msgattrs, name);
     }
 
     /*