1 | |
---|
2 | /* |
---|
3 | * folder_read.c -- initialize folder structure and read folder |
---|
4 | * |
---|
5 | * $Id: folder_read.c,v 1.1.1.1 1999-02-07 18:14:08 danw Exp $ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <h/mh.h> |
---|
9 | |
---|
10 | /* We allocate the `mi' array 1024 elements at a time */ |
---|
11 | #define NUMMSGS 1024 |
---|
12 | |
---|
13 | /* |
---|
14 | * 1) Create the folder/message structure |
---|
15 | * 2) Read the directory (folder) and temporarily |
---|
16 | * record the numbers of the messages we have seen. |
---|
17 | * 3) Then allocate the array for message attributes and |
---|
18 | * set the initial flags for all messages we've seen. |
---|
19 | * 4) Read and initialize the sequence information. |
---|
20 | */ |
---|
21 | |
---|
22 | struct msgs * |
---|
23 | folder_read (char *name) |
---|
24 | { |
---|
25 | int msgnum, prefix_len, len, *mi; |
---|
26 | struct msgs *mp; |
---|
27 | struct stat st; |
---|
28 | struct dirent *dp; |
---|
29 | DIR *dd; |
---|
30 | |
---|
31 | name = m_mailpath (name); |
---|
32 | if (!(dd = opendir (name))) { |
---|
33 | free (name); |
---|
34 | return NULL; |
---|
35 | } |
---|
36 | |
---|
37 | if (stat (name, &st) == -1) { |
---|
38 | free (name); |
---|
39 | return NULL; |
---|
40 | } |
---|
41 | |
---|
42 | /* Allocate the main structure for folder information */ |
---|
43 | if (!(mp = (struct msgs *) malloc ((size_t) sizeof(*mp)))) |
---|
44 | adios (NULL, "unable to allocate folder storage"); |
---|
45 | |
---|
46 | clear_folder_flags (mp); |
---|
47 | mp->foldpath = name; |
---|
48 | mp->lowmsg = 0; |
---|
49 | mp->hghmsg = 0; |
---|
50 | mp->curmsg = 0; |
---|
51 | mp->lowsel = 0; |
---|
52 | mp->hghsel = 0; |
---|
53 | mp->numsel = 0; |
---|
54 | mp->nummsg = 0; |
---|
55 | |
---|
56 | if (access (name, W_OK) == -1 || st.st_uid != getuid()) |
---|
57 | set_readonly (mp); |
---|
58 | prefix_len = strlen(BACKUP_PREFIX); |
---|
59 | |
---|
60 | /* |
---|
61 | * Allocate a temporary place to record the |
---|
62 | * name of the messages in this folder. |
---|
63 | */ |
---|
64 | len = NUMMSGS; |
---|
65 | if (!(mi = (int *) malloc ((size_t) (len * sizeof(*mi))))) |
---|
66 | adios (NULL, "unable to allocate storage"); |
---|
67 | |
---|
68 | while ((dp = readdir (dd))) { |
---|
69 | if ((msgnum = m_atoi (dp->d_name))) { |
---|
70 | /* |
---|
71 | * Check if we need to allocate more |
---|
72 | * temporary elements for message names. |
---|
73 | */ |
---|
74 | if (mp->nummsg >= len) { |
---|
75 | len += NUMMSGS; |
---|
76 | if (!(mi = (int *) realloc (mi, |
---|
77 | (size_t) (len * sizeof(*mi))))) { |
---|
78 | adios (NULL, "unable to allocate storage"); |
---|
79 | } |
---|
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 | if (!(mp->msgstats = malloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff)))) |
---|
138 | adios (NULL, "unable to allocate storage for msgstats"); |
---|
139 | |
---|
140 | /* |
---|
141 | * Clear all the flag bits for all the message |
---|
142 | * status entries we just allocated. |
---|
143 | */ |
---|
144 | for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++) |
---|
145 | clear_msg_flags (mp, msgnum); |
---|
146 | |
---|
147 | /* |
---|
148 | * Scan through the array of messages we've seen and |
---|
149 | * setup the initial flags for those messages in the |
---|
150 | * newly allocated mp->msgstats area. |
---|
151 | */ |
---|
152 | for (msgnum = 0; msgnum < mp->nummsg; msgnum++) |
---|
153 | set_exists (mp, mi[msgnum]); |
---|
154 | |
---|
155 | free (mi); /* We don't need this anymore */ |
---|
156 | |
---|
157 | /* |
---|
158 | * Read and initialize the sequence information. |
---|
159 | */ |
---|
160 | seq_read (mp); |
---|
161 | |
---|
162 | return mp; |
---|
163 | } |
---|