1 /* m_gmsg.c - read a folder */
18 #define NINFO (MAXFOLDER / 5) /* PLEASE be non-trivial... */
24 static int m_getatr(), m_setatr();
27 static struct info *head;
41 register struct info *rover,
47 register struct msgs *mp;
48 register struct direct *dp;
52 if ((dd = opendir (name = m_mailpath (name))) == NULL) {
56 (void) fstat (dd -> dd_fd, &st);
58 mp = (struct msgs *) malloc (MSIZE (mp, 0, 0));
60 adios (NULLCP, "unable to allocate folder storage");
61 mp -> lowmsg = mp -> hghmsg = mp -> nummsg = 0;
63 mp -> lowsel = mp -> hghsel = mp -> numsel = 0;
64 mp -> foldpath = name;
65 mp -> msgflags = NULL;
66 if (st.st_uid != getuid () || access (name, 02) == NOTOK)
67 mp -> msgflags |= READONLY;
73 if ((head = (struct info *)
74 malloc ((unsigned) ((len = NINFO) * sizeof *head))) == NULL)
75 adios (NULLCP, "unable to allocate info storage");
76 tail = (rover = head) + len;
78 while (dp = readdir (dd))
79 if (i = m_atoi (dp -> d_name)) {
81 register int curlen = tail - head;
83 if ((tail = (struct info *) realloc ((char *) head,
84 (unsigned) ((len += NINFO) * sizeof *head)))
86 adios (NULLCP, "unable to allocate info storage");
88 rover = tail + curlen, head = tail, tail += len;
93 if (mp -> lowmsg == 0 || i < mp -> lowmsg)
96 rover -> stats = EXISTS;
98 rover -> stats &= ~DELETED;
103 switch (dp -> d_name[0]) {
109 if ((i = m_atoi (dp -> d_name + 1)) {
110 register struct info *l;
112 for (l = head; l < rover; l++)
113 if (l -> msgno == i) {
114 if (!(l -> stats & EXISTS))
115 l -> stats |= DELETED;
135 if (strcmp (dp -> d_name, current) == 0) {
140 if (strcmp (dp -> d_name, LINK) == 0
141 || strncmp (dp -> d_name, SBACKUP, j) == 0)
143 mp -> msgflags |= OTHERS;
152 (void) sprintf (buffer, "%s-%s", current, name);
153 if (cp = m_find (buffer)) {
155 (void) m_delete(buffer);
159 if (mp -> curmsg == 0 && cur && (fd = open (current, 0)) != NOTOK) {
160 if ((i = read (fd, buffer, sizeof buffer)) > 0) {
161 if (cp = index (buffer, '\n'))
163 if ((i = m_atoi (buffer)) > 0)
168 if (cur && !(mp -> msgflags & READONLY)){ /* sneaky... */
169 (void) sprintf (buffer, "%s/%s", name, current);
170 (void) unlink (buffer);
177 mp -> lowoff = mp -> lowmsg;
179 mp -> hghoff = mp -> hghmsg + 1;/* for "new" in m_convert */
182 realloc ((char *) mp, MSIZE (mp, mp -> lowoff, mp -> hghoff));
184 adios (NULLCP, "unable to allocate folder storage");
186 for (i = mp -> lowmsg; i <= mp -> hghmsg; i++)
187 mp -> msgstats[i] = 0;
189 mp -> msgstats = (int *)
190 calloc ((unsigned) 1, MSIZEX (mp, mp -> lowmsg, mp -> hghmsg));
191 if (mp -> msgstats == NULL)
192 adios (NULLCP, "unable to allocate messages storage");
193 mp -> msgstats = (mp -> msgbase = mp -> msgstats) - mp -> lowoff;
194 if (mp -> msgstats < 0)
195 adios (NULLCP, "m_gmsg() botch -- you lose big");
197 for (tail = head; tail < rover; tail++)
198 mp -> msgstats[tail -> msgno] = tail -> stats;
208 register struct msgs *mp;
219 register struct node *np;
224 mp -> msgattrs[i = 0] = getcpy (current);
225 mp -> msgattrs[++i] = NULL;
226 mp -> attrstats = 0; /* initially, all public */
229 if (mh_seq == NULL || *mh_seq == NULL)
232 (void) sprintf (field, "%s/%s", mp -> foldpath, mh_seq);
233 if (fp = fopen (field, "r")) {
234 for (state = FLD;;) {
235 switch (state = m_getfld (state, name, field, sizeof field, fp)) {
238 (void) m_setatr (mp, getcpy (name), trimcpy (field));
246 "no blank lines are permitted in %s/%s",
247 mp -> foldpath, mh_seq);/* fall */
253 adios (NULLCP, "%s/%s is poorly formatted",
254 mp -> foldpath, mh_seq);
262 alen = strlen ("atr-");
263 plen = strlen (mp -> foldpath) + 1;
265 for (np = m_defs; np; np = np -> n_next)
266 if (ssequal ("atr-", np -> n_name)
267 && (j = strlen (np -> n_name) - plen) > alen
268 && *(np -> n_name + j) == '-'
269 && strcmp (mp -> foldpath, np -> n_name + j + 1) == 0) {
270 cp = getcpy (np -> n_name + alen);
271 *(cp + j - alen) = NULL;
272 if ((i = m_setatr (mp, cp, getcpy (np -> n_field))) != NOTOK)
273 mp -> attrstats |= 1 << (bits + i);/* private */
280 m_setatr(mp, name, field)
281 register struct msgs *mp;
282 register char *name, *field;
293 hack = strcmp (current, name) == 0;/* hack... */
295 for (i = 0; mp -> msgattrs[i]; i++)
296 if (strcmp (mp -> msgattrs[i], name) == 0) {
297 for (j = mp -> lowmsg; j <= mp -> hghmsg; j++)
298 mp -> msgstats[j] &= ~(1 << (bits + i));
307 if (mp -> msgattrs[i] == NULL) {
308 mp -> msgattrs[i] = name;
309 mp -> msgattrs[i + 1] = NULL;
314 for (ap = brkstring (field, " ", "\n");
317 if (cp = index (*ap, '-'))
319 if ((j = m_atoi (*ap)) > 0) {
321 if (hack && j >= mp -> lowmsg && j <= mp -> hghmsg
322 && (mp -> msgstats[j] & EXISTS))
328 for (k = cp ? m_atoi (cp) : j; j <= k; j++)
329 if (j >= mp -> lowmsg && j <= mp -> hghmsg
330 && (mp -> msgstats[j] & EXISTS))
331 mp -> msgstats[j] |= 1 << (bits + i);