* man/mhbuild.man: wrapped one appearance of "Content-Disposition"
[mmh] / 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                 mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
80             }
81
82             /* Check if this is the first message we've seen */
83             if (mp->nummsg == 0) {
84                 mp->lowmsg = msgnum;
85                 mp->hghmsg = msgnum;
86             } else {
87                 /* Check if this is it the highest or lowest we've seen? */
88                 if (msgnum < mp->lowmsg)
89                    mp->lowmsg = msgnum;
90                 if (msgnum > mp->hghmsg)
91                    mp->hghmsg = msgnum;
92             }
93
94             /*
95              * Now increment count, and record message
96              * number in a temporary place for now.
97              */
98             mi[mp->nummsg++] = msgnum;
99
100         } else {
101             switch (dp->d_name[0]) {
102                 case '.': 
103                 case ',': 
104 #ifdef MHE
105                 case '+': 
106 #endif /* MHE */
107                     continue;
108
109                 default: 
110                     /* skip any files beginning with backup prefix */
111                     if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
112                         continue;
113
114                     /* skip the LINK file */
115                     if (!strcmp (dp->d_name, LINK))
116                         continue;
117
118                     /* indicate that there are other files in folder */
119                     set_other_files (mp);
120                     continue;
121             }
122         }
123     }
124
125     closedir (dd);
126     mp->lowoff = max (mp->lowmsg, 1);
127
128     /* Go ahead and allocate space for 100 additional messages. */
129     mp->hghoff = mp->hghmsg + 100;
130
131     /* for testing, allocate minimal necessary space */
132     /* mp->hghoff = max (mp->hghmsg, 1); */
133
134     /*
135      * Allocate space for status of each message.
136      */
137     mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
138
139     /*
140      * Clear all the flag bits for all the message
141      * status entries we just allocated.
142      */
143     for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
144         clear_msg_flags (mp, msgnum);
145
146     /*
147      * Scan through the array of messages we've seen and
148      * setup the initial flags for those messages in the
149      * newly allocated mp->msgstats area.
150      */
151     for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
152         set_exists (mp, mi[msgnum]);
153
154     free (mi);          /* We don't need this anymore    */
155
156     /*
157      * Read and initialize the sequence information.
158      */
159     seq_read (mp);
160
161     return mp;
162 }