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