1 /* m_gmsg.c - read a folder */
3 static char ident[] = "@(#)$Id: m_gmsg.c,v 2.10 1992/10/20 22:46:29 jromine Exp $";
7 #include "../h/local.h"
11 #define NINFO (MAXFOLDER / 5) /* PLEASE be non-trivial... */
18 static struct info *head;
21 static int m_setatr();
24 struct msgs *m_gmsg (name)
33 register struct info *rover,
39 register struct msgs *mp;
41 register struct dirent *dp;
44 register struct direct *dp;
49 if ((dd = opendir (name = m_mailpath (name))) == NULL) {
53 (void) fstat (dd -> dd_fd, &st);
55 mp = (struct msgs *) malloc (MHSIZE (mp, 0, 0));
57 adios (NULLCP, "unable to allocate folder storage");
58 mp -> lowmsg = mp -> hghmsg = mp -> nummsg = 0;
60 mp -> lowsel = mp -> hghsel = mp -> numsel = 0;
61 mp -> foldpath = name;
63 if (st.st_uid != getuid () || access (name, 02) == NOTOK)
64 mp -> msgflags |= READONLY;
70 if ((head = (struct info *)
71 malloc ((unsigned) ((len = NINFO) * sizeof *head))) == NULL)
72 adios (NULLCP, "unable to allocate info storage");
73 tail = (rover = head) + len;
75 while (dp = readdir (dd))
76 if (i = m_atoi (dp -> d_name)) {
78 register int curlen = tail - head;
80 if ((tail = (struct info *) realloc ((char *) head,
81 (unsigned) ((len += NINFO) * sizeof *head)))
83 adios (NULLCP, "unable to allocate info storage");
85 rover = tail + curlen, head = tail, tail += len;
90 if (mp -> lowmsg == 0 || i < mp -> lowmsg)
93 rover -> stats = EXISTS;
95 rover -> stats &= ~DELETED;
100 switch (dp -> d_name[0]) {
106 if ((i = m_atoi (dp -> d_name + 1)) {
107 register struct info *l;
109 for (l = head; l < rover; l++)
110 if (l -> msgno == i) {
111 if (!(l -> stats & EXISTS))
112 l -> stats |= DELETED;
132 if (strcmp (dp -> d_name, current) == 0) {
137 if (strcmp (dp -> d_name, LINK) == 0
138 || strncmp (dp -> d_name, SBACKUP, j) == 0)
140 mp -> msgflags |= OTHERS;
149 (void) sprintf (buffer, "%s-%s", current, name);
150 if (cp = m_find (buffer)) {
152 (void) m_delete(buffer);
156 if (mp -> curmsg == 0 && cur && (fd = open (current, 0)) != NOTOK) {
157 if ((i = read (fd, buffer, sizeof buffer)) > 0) {
158 if (cp = index (buffer, '\n'))
160 if ((i = m_atoi (buffer)) > 0)
165 if (cur && !(mp -> msgflags & READONLY)){ /* sneaky... */
166 (void) sprintf (buffer, "%s/%s", name, current);
167 (void) unlink (buffer);
174 mp -> lowoff = mp -> lowmsg;
176 mp -> hghoff = mp -> hghmsg + 1;/* for "new" in m_convert */
179 realloc ((char *) mp, MHSIZE (mp, mp -> lowoff, mp -> hghoff));
181 adios (NULLCP, "unable to allocate folder storage");
183 for (i = mp -> lowmsg; i <= mp -> hghmsg; i++)
184 mp -> msgstats[i] = 0;
186 mp -> msgstats = (int *)
187 calloc ((unsigned) 1, MHSIZEX (mp, mp -> lowmsg, mp -> hghmsg));
188 if (mp -> msgstats == NULL)
189 adios (NULLCP, "unable to allocate messages storage");
190 mp -> msgstats = (mp -> msgbase = mp -> msgstats) - mp -> lowoff;
191 if (mp -> msgstats < (int *)0) /* non portable */
192 adios (NULLCP, "m_gmsg() botch -- you lose big");
194 for (tail = head; tail < rover; tail++)
195 mp -> msgstats[tail -> msgno] = tail -> stats;
204 register struct msgs *mp;
215 register struct node *np;
220 mp -> msgattrs[i = 0] = getcpy (current);
221 mp -> msgattrs[++i] = '\0';
222 mp -> attrstats = 0; /* initially, all public */
225 if (mh_seq == '\0' || *mh_seq == '\0')
228 (void) sprintf (field, "%s/%s", mp -> foldpath, mh_seq);
229 if (fp = fopen (field, "r")) {
230 for (state = FLD;;) {
231 switch (state = m_getfld (state, name, field, sizeof field, fp)) {
234 (void) m_setatr (mp, getcpy (name), trimcpy (field));
242 "no blank lines are permitted in %s/%s",
243 mp -> foldpath, mh_seq);/* fall */
249 adios (NULLCP, "%s/%s is poorly formatted",
250 mp -> foldpath, mh_seq);
258 alen = strlen ("atr-");
259 plen = strlen (mp -> foldpath) + 1;
261 for (np = m_defs; np; np = np -> n_next)
262 if (ssequal ("atr-", np -> n_name)
263 && (j = strlen (np -> n_name) - plen) > alen
264 && *(np -> n_name + j) == '-'
265 && strcmp (mp -> foldpath, np -> n_name + j + 1) == 0) {
266 cp = getcpy (np -> n_name + alen);
267 *(cp + j - alen) = '\0';
268 if ((i = m_setatr (mp, cp, getcpy (np -> n_field))) != NOTOK)
269 mp -> attrstats |= 1 << (bits + i);/* private */
275 static int m_setatr (mp, name, field)
276 register struct msgs *mp;
289 hack = strcmp (current, name) == 0;/* hack... */
290 /* if we're going to use UNSEEN, it should be set here! */
292 for (i = 0; mp -> msgattrs[i]; i++)
293 if (strcmp (mp -> msgattrs[i], name) == 0) {
294 for (j = mp -> lowmsg; j <= mp -> hghmsg; j++)
295 mp -> msgstats[j] &= ~(1 << (bits + i));
304 if (mp -> msgattrs[i] == NULL) {
305 mp -> msgattrs[i] = name;
306 mp -> msgattrs[i + 1] = NULL;
311 for (ap = brkstring (field, " ", "\n");
314 if (cp = index (*ap, '-'))
316 if ((j = m_atoi (*ap)) > 0) {
318 if (hack && j >= mp -> lowmsg && j <= mp -> hghmsg
319 && (mp -> msgstats[j] & EXISTS))
321 #else /* not notdef */
324 #endif /* not notdef */
325 for (k = cp ? m_atoi (cp) : j; j <= k; j++)
326 if (j >= mp -> lowmsg && j <= mp -> hghmsg
327 && (mp -> msgstats[j] & EXISTS))
328 mp -> msgstats[j] |= 1 << (bits + i);