]> diplodocus.org Git - nmh/blob - sbr/folder_read.c
8e3a3ffa7e6648cbc3c917a2fc5aef867e675214
[nmh] / sbr / folder_read.c
1
2 /*
3 * folder_read.c -- initialize folder structure and read folder
4 *
5 * $Id$
6 *
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.
10 */
11
12 #include <h/mh.h>
13 #include <h/utils.h>
14
15 /* We allocate the `mi' array 1024 elements at a time */
16 #define NUMMSGS 1024
17
18 /*
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.
25 */
26
27 struct msgs *
28 folder_read (char *name)
29 {
30 int msgnum, prefix_len, len, *mi;
31 struct msgs *mp;
32 struct stat st;
33 struct dirent *dp;
34 DIR *dd;
35
36 name = m_mailpath (name);
37 if (!(dd = opendir (name))) {
38 free (name);
39 return NULL;
40 }
41
42 if (stat (name, &st) == -1) {
43 free (name);
44 return NULL;
45 }
46
47 /* Allocate the main structure for folder information */
48 mp = (struct msgs *) mh_xmalloc ((size_t) sizeof(*mp));
49
50 clear_folder_flags (mp);
51 mp->foldpath = name;
52 mp->lowmsg = 0;
53 mp->hghmsg = 0;
54 mp->curmsg = 0;
55 mp->lowsel = 0;
56 mp->hghsel = 0;
57 mp->numsel = 0;
58 mp->nummsg = 0;
59
60 if (access (name, W_OK) == -1)
61 set_readonly (mp);
62 prefix_len = strlen(BACKUP_PREFIX);
63
64 /*
65 * Allocate a temporary place to record the
66 * name of the messages in this folder.
67 */
68 len = NUMMSGS;
69 mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
70
71 while ((dp = readdir (dd))) {
72 if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
73 /*
74 * Check if we need to allocate more
75 * temporary elements for message names.
76 */
77 if (mp->nummsg >= len) {
78 len += NUMMSGS;
79 if (!(mi = (int *) realloc (mi,
80 (size_t) (len * sizeof(*mi))))) {
81 adios (NULL, "unable to allocate storage");
82 }
83 }
84
85 /* Check if this is the first message we've seen */
86 if (mp->nummsg == 0) {
87 mp->lowmsg = msgnum;
88 mp->hghmsg = msgnum;
89 } else {
90 /* Check if this is it the highest or lowest we've seen? */
91 if (msgnum < mp->lowmsg)
92 mp->lowmsg = msgnum;
93 if (msgnum > mp->hghmsg)
94 mp->hghmsg = msgnum;
95 }
96
97 /*
98 * Now increment count, and record message
99 * number in a temporary place for now.
100 */
101 mi[mp->nummsg++] = msgnum;
102
103 } else {
104 switch (dp->d_name[0]) {
105 case '.':
106 case ',':
107 #ifdef MHE
108 case '+':
109 #endif /* MHE */
110 continue;
111
112 default:
113 /* skip any files beginning with backup prefix */
114 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
115 continue;
116
117 /* skip the LINK file */
118 if (!strcmp (dp->d_name, LINK))
119 continue;
120
121 /* indicate that there are other files in folder */
122 set_other_files (mp);
123 continue;
124 }
125 }
126 }
127
128 closedir (dd);
129 mp->lowoff = max (mp->lowmsg, 1);
130
131 /* Go ahead and allocate space for 100 additional messages. */
132 mp->hghoff = mp->hghmsg + 100;
133
134 /* for testing, allocate minimal necessary space */
135 /* mp->hghoff = max (mp->hghmsg, 1); */
136
137 /*
138 * Allocate space for status of each message.
139 */
140 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
141
142 /*
143 * Clear all the flag bits for all the message
144 * status entries we just allocated.
145 */
146 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
147 clear_msg_flags (mp, msgnum);
148
149 /*
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.
153 */
154 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
155 set_exists (mp, mi[msgnum]);
156
157 free (mi); /* We don't need this anymore */
158
159 /*
160 * Read and initialize the sequence information.
161 */
162 seq_read (mp);
163
164 return mp;
165 }