]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/subs/m_gmsg.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / subs / m_gmsg.c
1 #include "mh.h"
2 #include <stdio.h>
3 #include <sys/param.h>
4 #include <sys/stat.h>
5 #include <sys/dir.h>
6
7 struct msgs *mp;
8
9 struct msgs *m_gmsg(name)
10 char *name;
11 {
12 register int i, j;
13 register char *cp;
14 register struct msgs *msgp;
15 register struct direct *dir;
16 DIR *dirp;
17 int curfil;
18 struct stat statb;
19 char buf[132];
20
21 struct {
22 int xhghmsg,
23 xnummsg,
24 xlowmsg,
25 xcurmsg;
26 char xselist,
27 xflags,
28 xfiller,
29 xothers;
30 char xmsgs[1000];
31 } msgbuf;
32
33 if ((dirp = opendir(".")) == 0)
34 return (0);
35 for(j = 0; j < 1000; j++)
36 msgbuf.xmsgs[j] = 0;
37 msgbuf.xcurmsg = 0;
38 msgbuf.xnummsg = 0;
39 msgbuf.xselist = 0;
40 msgbuf.xothers = 0;
41 msgbuf.xlowmsg = 5000;
42 msgbuf.xhghmsg = 0;
43 msgbuf.xflags = (access(".",2) == -1)? READONLY:0;
44
45 /*
46 * The following hack is that if the directory is writable
47 * and the cur file is not, we consider it to be read only
48 * folder.
49 */
50
51 if (stat("cur", &statb) >= 0 && access("cur", 2) < 0)
52 msgbuf.xflags |= READONLY;
53 curfil = 0;
54 while (dir = readdir(dirp)) {
55 cp = dir->d_name;
56 if (j = mu_atoi(cp)) {
57 if (j > msgbuf.xhghmsg)
58 msgbuf.xhghmsg = j;
59 msgbuf.xnummsg++;
60 if (j < msgbuf.xlowmsg)
61 msgbuf.xlowmsg = j;
62 msgbuf.xmsgs[j] = EXISTS;
63 } else if (*cp != ',' && *cp != '.' && *cp != '#')
64 if (strcmp(cp, current) == 0)
65 curfil++;
66 else if (strcmp(cp, listname) == 0)
67 msgbuf.xselist++;
68 else
69 msgbuf.xothers++;
70 }
71 if(!msgbuf.xhghmsg)
72 msgbuf.xlowmsg = 0;
73 closedir(dirp);
74 if(msgbuf.xflags&READONLY) {
75 sprintf(buf, "cur-%s", name);
76 /*** copy(name, copy("cur-", buf)); ***/
77 if((cp = m_find(buf)) != NULL)
78 if(j = mu_atoi(cp))
79 msgbuf.xcurmsg = j;
80 } else if(curfil && (i = open(current, 0)) >= 0) {
81 if((j = read(i, buf, sizeof (buf))) >= 2) {
82 buf[j-1] = '\0'; /* Zap <lf> */
83 if (j = mu_atoi(buf))
84 msgbuf.xcurmsg = j;
85 }
86 close(i);
87 }
88 if( (int) (msgp = (struct msgs *) malloc(sizeof *mp + msgbuf.xhghmsg + 2)) == -1)
89 return(0);
90 msgp->hghmsg = msgbuf.xhghmsg;
91 msgp->nummsg = msgbuf.xnummsg;
92 msgp->lowmsg = msgbuf.xlowmsg;
93 msgp->curmsg = msgbuf.xcurmsg;
94 msgp->selist = msgbuf.xselist;
95 msgp->msgflags = msgbuf.xflags;
96 msgp->others = msgbuf.xothers;
97 msgp->foldpath = name;
98 msgp->lowsel = 5000;
99 msgp->hghsel = 0;
100 msgp->numsel = 0;
101 for(j = 0; j <= msgbuf.xhghmsg; j++)
102 msgp->msgstats[j] = msgbuf.xmsgs[j];
103 return(msgp);
104 }
105
106
107 mu_atoi(str)
108 char *str;
109 {
110 register char *cp;
111 register int i;
112
113 i = 0;
114 cp = str;
115 while(*cp) {
116 if(*cp < '0' || *cp > '9' || i > 99)
117 return(0);
118 i *= 10;
119 i += *cp++ - '0';
120 }
121 return(i);
122 }