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.
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)
36 name = mh_xstrdup(toabsdir(name));
37 if (!(dd = opendir(name))) {
42 if (stat(name, &st) == -1) {
48 /* Allocate the main structure for folder information */
49 mp = mh_xcalloc(1, sizeof(*mp));
51 clear_folder_flags(mp);
61 if (access(name, W_OK) == -1)
65 ** Allocate a temporary place to record the
66 ** name of the messages in this folder.
69 mi = mh_xcalloc(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 = mh_xrealloc(mi, len * sizeof(*mi));
82 /* Check if this is the first message we've seen */
83 if (mp->nummsg == 0) {
88 ** Check if this is it the highest or
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]) {
111 ** indicate that there are other
121 mp->lowoff = max(mp->lowmsg, 1);
123 /* Go ahead and allocate space for 100 additional messages. */
124 mp->hghoff = mp->hghmsg + 100;
126 /* for testing, allocate minimal necessary space */
127 /* mp->hghoff = max(mp->hghmsg, 1); */
129 /* Allocate space for status of each message. */
131 mp->msgstats = mh_xcalloc(MSGSTATSIZE(mp, mp->lowoff, mp->hghoff), 1);
134 ** Clear all the flag bits for all the message
135 ** status entries we just allocated.
136 ** TODO: use memset() ?
138 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
139 clear_msg_flags(mp, msgnum);
142 ** Scan through the array of messages we've seen and
143 ** setup the initial flags for those messages in the
144 ** newly allocated mp->msgstats area.
146 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
147 set_exists(mp, mi[msgnum]);
149 mh_free0(&mi); /* We don't need this anymore */
152 ** Read and initialize the sequence information.