3 * folder_read.c -- initialize folder structure and read folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 /* We allocate the `mi' array 1024 elements at a time */
19 * 1) Create the folder/message structure
20 * 2) Read the directory (folder) and temporarily
21 * record the numbers of the messages we have seen.
22 * 3) Then allocate the array for message attributes and
23 * set the initial flags for all messages we've seen.
24 * 4) Read and initialize the sequence information.
28 folder_read (char *name)
30 int msgnum, prefix_len, len, *mi;
36 name = m_mailpath (name);
37 if (!(dd = opendir (name))) {
42 if (stat (name, &st) == -1) {
47 /* Allocate the main structure for folder information */
48 mp = (struct msgs *) mh_xmalloc ((size_t) sizeof(*mp));
50 clear_folder_flags (mp);
60 if (access (name, W_OK) == -1)
62 prefix_len = strlen(BACKUP_PREFIX);
65 * Allocate a temporary place to record the
66 * name of the messages in this folder.
69 mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
71 while ((dp = readdir (dd))) {
72 if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
74 * Check if we need to allocate more
75 * temporary elements for message names.
77 if (mp->nummsg >= len) {
79 mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
82 /* Check if this is the first message we've seen */
83 if (mp->nummsg == 0) {
87 /* Check if this is it the highest or lowest we've seen? */
88 if (msgnum < mp->lowmsg)
90 if (msgnum > mp->hghmsg)
95 * Now increment count, and record message
96 * number in a temporary place for now.
98 mi[mp->nummsg++] = msgnum;
101 switch (dp->d_name[0]) {
110 /* skip any files beginning with backup prefix */
111 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
114 /* skip the LINK file */
115 if (!strcmp (dp->d_name, LINK))
118 /* indicate that there are other files in folder */
119 set_other_files (mp);
126 mp->lowoff = max (mp->lowmsg, 1);
128 /* Go ahead and allocate space for 100 additional messages. */
129 mp->hghoff = mp->hghmsg + 100;
131 /* for testing, allocate minimal necessary space */
132 /* mp->hghoff = max (mp->hghmsg, 1); */
135 * Allocate space for status of each message.
137 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
140 * Clear all the flag bits for all the message
141 * status entries we just allocated.
143 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
144 clear_msg_flags (mp, msgnum);
147 * Scan through the array of messages we've seen and
148 * setup the initial flags for those messages in the
149 * newly allocated mp->msgstats area.
151 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
152 set_exists (mp, mi[msgnum]);
154 free (mi); /* We don't need this anymore */
157 * Read and initialize the sequence information.