]> diplodocus.org Git - nmh/blob - sbr/folder_read.c
Remove SYNOPSIS from nmh(7).
[nmh] / sbr / folder_read.c
1
2 /*
3 * folder_read.c -- initialize folder structure and read folder
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /* We allocate the `mi' array 1024 elements at a time */
14 #define NUMMSGS 1024
15
16 /*
17 * 1) Create the folder/message structure
18 * 2) Read the directory (folder) and temporarily
19 * record the numbers of the messages we have seen.
20 * 3) Then allocate the array for message attributes and
21 * set the initial flags for all messages we've seen.
22 * 4) Read and initialize the sequence information.
23 */
24
25 struct msgs *
26 folder_read (char *name, int lockflag)
27 {
28 int msgnum, prefix_len, len, *mi;
29 struct msgs *mp;
30 struct stat st;
31 struct dirent *dp;
32 DIR *dd;
33 bvector_t *v;
34 size_t i;
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 mp->seqhandle = NULL;
60 mp->seqname = NULL;
61
62 if (access (name, W_OK) == -1)
63 set_readonly (mp);
64 prefix_len = strlen(BACKUP_PREFIX);
65
66 /*
67 * Allocate a temporary place to record the
68 * name of the messages in this folder.
69 */
70 len = NUMMSGS;
71 mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
72
73 while ((dp = readdir (dd))) {
74 if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
75 /*
76 * Check if we need to allocate more
77 * temporary elements for message names.
78 */
79 if (mp->nummsg >= len) {
80 len += NUMMSGS;
81 mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
82 }
83
84 /* Check if this is the first message we've seen */
85 if (mp->nummsg == 0) {
86 mp->lowmsg = msgnum;
87 mp->hghmsg = msgnum;
88 } else {
89 /* Check if this is it the highest or lowest we've seen? */
90 if (msgnum < mp->lowmsg)
91 mp->lowmsg = msgnum;
92 if (msgnum > mp->hghmsg)
93 mp->hghmsg = msgnum;
94 }
95
96 /*
97 * Now increment count, and record message
98 * number in a temporary place for now.
99 */
100 mi[mp->nummsg++] = msgnum;
101
102 } else {
103 switch (dp->d_name[0]) {
104 case '.':
105 case ',':
106 continue;
107
108 default:
109 /* skip any files beginning with backup prefix */
110 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
111 continue;
112
113 /* skip the LINK file */
114 if (!strcmp (dp->d_name, LINK))
115 continue;
116
117 /* indicate that there are other files in folder */
118 set_other_files (mp);
119 continue;
120 }
121 }
122 }
123
124 closedir (dd);
125 mp->lowoff = max (mp->lowmsg, 1);
126
127 /* Go ahead and allocate space for 100 additional messages. */
128 mp->hghoff = mp->hghmsg + 100;
129
130 /* for testing, allocate minimal necessary space */
131 /* mp->hghoff = max (mp->hghmsg, 1); */
132
133 /*
134 * Allocate space for status of each message.
135 */
136 mp->num_msgstats = MSGSTATNUM (mp->lowoff, mp->hghoff);
137 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp));
138 for (i = 0, v = mp->msgstats; i < mp->num_msgstats; ++i, ++v) {
139 *v = bvector_create (0);
140 }
141
142 mp->msgattrs = svector_create (0);
143
144 /*
145 * Clear all the flag bits for all the message
146 * status entries we just allocated.
147 */
148 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
149 clear_msg_flags (mp, msgnum);
150
151 /*
152 * Scan through the array of messages we've seen and
153 * setup the initial flags for those messages in the
154 * newly allocated mp->msgstats area.
155 */
156 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
157 set_exists (mp, mi[msgnum]);
158
159 free (mi); /* We don't need this anymore */
160
161 /*
162 * Read and initialize the sequence information.
163 */
164 seq_read (mp, lockflag);
165
166 return mp;
167 }