2 ** folder_read.c -- initialize folder structure and read folder
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
12 /* We allocate the `mi' array 1024 elements at a time */
16 ** 1) Create the folder/message structure
17 ** 2) Read the directory (folder) and temporarily
18 ** record the numbers of the messages we have seen.
19 ** 3) Then allocate the array for message attributes and
20 ** set the initial flags for all messages we've seen.
21 ** 4) Read and initialize the sequence information.
25 folder_read(char *name)
27 int msgnum, prefix_len, len, *mi;
33 name = m_mailpath(name);
34 if (!(dd = opendir(name))) {
39 if (stat(name, &st) == -1) {
44 /* Allocate the main structure for folder information */
45 mp = (struct msgs *) mh_xmalloc((size_t) sizeof(*mp));
47 clear_folder_flags(mp);
57 if (access(name, W_OK) == -1)
59 prefix_len = strlen(backup_prefix);
62 ** Allocate a temporary place to record the
63 ** name of the messages in this folder.
66 mi = (int *) mh_xmalloc((size_t) (len * sizeof(*mi)));
68 while ((dp = readdir(dd))) {
69 if ((msgnum = m_atoi(dp->d_name)) && msgnum > 0) {
71 ** Check if we need to allocate more
72 ** temporary elements for message names.
74 if (mp->nummsg >= len) {
76 mi = (int *) mh_xrealloc(mi, (size_t) (len * sizeof(*mi)));
79 /* Check if this is the first message we've seen */
80 if (mp->nummsg == 0) {
85 ** Check if this is it the highest or
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]) {
111 ** skip any files beginning with
114 if (!strncmp(dp->d_name, backup_prefix, prefix_len))
117 /* skip the altmsg link file */
118 if (!strcmp(dp->d_name, altmsglink))
122 ** indicate that there are other
132 mp->lowoff = max(mp->lowmsg, 1);
134 /* Go ahead and allocate space for 100 additional messages. */
135 mp->hghoff = mp->hghmsg + 100;
137 /* for testing, allocate minimal necessary space */
138 /* mp->hghoff = max(mp->hghmsg, 1); */
141 ** Allocate space for status of each message.
143 mp->msgstats = mh_xmalloc(MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
146 ** Clear all the flag bits for all the message
147 ** status entries we just allocated.
149 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
150 clear_msg_flags(mp, msgnum);
153 ** Scan through the array of messages we've seen and
154 ** setup the initial flags for those messages in the
155 ** newly allocated mp->msgstats area.
157 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
158 set_exists(mp, mi[msgnum]);
160 free(mi); /* We don't need this anymore */
163 ** Read and initialize the sequence information.