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 if (!(mi = (int *) realloc (mi,
80 (size_t) (len * sizeof(*mi))))) {
81 adios (NULL, "unable to allocate storage");
85 /* Check if this is the first message we've seen */
86 if (mp->nummsg == 0) {
90 /* Check if this is it the highest or lowest we've seen? */
91 if (msgnum < mp->lowmsg)
93 if (msgnum > mp->hghmsg)
98 * Now increment count, and record message
99 * number in a temporary place for now.
101 mi[mp->nummsg++] = msgnum;
104 switch (dp->d_name[0]) {
113 /* skip any files beginning with backup prefix */
114 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
117 /* skip the LINK file */
118 if (!strcmp (dp->d_name, LINK))
121 /* indicate that there are other files in folder */
122 set_other_files (mp);
129 mp->lowoff = max (mp->lowmsg, 1);
131 /* Go ahead and allocate space for 100 additional messages. */
132 mp->hghoff = mp->hghmsg + 100;
134 /* for testing, allocate minimal necessary space */
135 /* mp->hghoff = max (mp->hghmsg, 1); */
138 * Allocate space for status of each message.
140 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
143 * Clear all the flag bits for all the message
144 * status entries we just allocated.
146 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
147 clear_msg_flags (mp, msgnum);
150 * Scan through the array of messages we've seen and
151 * setup the initial flags for those messages in the
152 * newly allocated mp->msgstats area.
154 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
155 set_exists (mp, mi[msgnum]);
157 free (mi); /* We don't need this anymore */
160 * Read and initialize the sequence information.