]> diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/folder.c
Added start_test/finish_test to a bunch of tests.
[nmh] / docs / historical / 2.9BSD / cmds / folder.c
1 #include "mh.h"
2 #include <stdio.h>
3 #include <sys/param.h>
4 #include <sys/stat.h>
5 #include <dir.h>
6 #include <strings.h>
7
8 #define NFOLDERS 100
9
10 int all, hdrflag, foldp;
11 struct msgs *mp;
12 char folder[128], *folds[NFOLDERS];
13 int msgtot, foldtot, totonly, fshort;
14 int fpack;
15 struct swit switches[] = {
16 "all", 0, /* 0 */
17 "down", 0, /* 1 */
18 "fast", 0, /* 2 */
19 "nofast", 0, /* 3 */
20 "header", 0, /* 4 */
21 "noheader", 0, /* 5 */
22 "pack", 0, /* 6 */
23 "nopack", 0, /* 7 */
24 "short", -1, /* 8 */
25 "total", 0, /* 9 */
26 "nototal", 0, /*10 */
27 "up", 0, /*11 */
28 "help", 4, /*12 */
29 "debug", 5, /*13 */
30 0, 0
31 };
32
33 int debug;
34
35 main(argc, argv)
36 char *argv[];
37 {
38 register char *cp, *curm;
39 register int i;
40 char *argfolder;
41 int up, down, j, def_short;
42 char *arguments[50], **argp, **ap;
43 struct stat stbf;
44 struct node *np;
45 extern char _sobuf[];
46 register struct direct *dir;
47 DIR *dirp;
48
49 setbuf(stdout, _sobuf);
50 #ifdef NEWS
51 m_news();
52 #endif
53 up = down = 0;
54 argfolder = NULL;
55 curm = 0;
56 if(argv[0][strlen(argv[0])-1] == 's') /* Plural name?? */
57 all++;
58 cp = r1bindex(argv[0], '/');
59 if((cp = m_find(cp)) != NULL) {
60 ap = brkstring(cp = getcpy(cp), " ", "\n");
61 ap = copyip(ap, arguments);
62 } else
63 ap = arguments;
64 copyip(argv+1, ap);
65 argp = arguments;
66 while(cp = *argp++) {
67 if(*cp == '-')
68 switch(smatch(++cp, switches)) {
69 case -2:ambigsw(cp, switches); /* ambiguous */
70 goto leave;
71 /* unknown */
72 case -1:fprintf(stderr, "folder: -%s unknown\n", cp);
73 goto leave;
74 case 0: all++; continue; /* -all */
75 case 1: down++; continue; /* -down */
76 case 2: /* -fast */
77 case 3: fshort = 0; continue; /* -nofast */
78 case 4: hdrflag = -1; continue; /* -header */
79 case 5: hdrflag = 0; continue; /* -noheader */
80 case 6: fpack = 1; continue; /* -pack */
81 case 7: fpack = 0; continue; /* -nopack */
82 case 8: fshort = 1; continue; /* -short */
83 case 9: all++; totonly = 1; /* -total */
84 continue;
85 case 10:if(totonly) all--; /* -nototal */
86 totonly =0; continue;
87 case 11:up++; continue; /* -up */
88 /* -help */
89 case 12:help("folder [+folder] [msg] [switches]",
90 switches);
91 goto leave;
92 case 13:debug++; /* -debug */
93 continue;
94 }
95 if(*cp == '+') {
96 if(argfolder) {
97 fprintf(stderr, "Only one folder at a time.\n");
98 goto leave;
99 } else
100 argfolder = cp + 1;
101 } else if(curm) {
102 fprintf(stderr, "Only one current may be given.\n");
103 goto leave;
104 } else
105 curm = cp;
106 }
107 if(all) {
108 hdrflag = 0;
109 cp = m_maildir("");
110 m_getdefs();
111 for(np = m_defs; np; np = np->n_next) {
112 if(!ssequal("cur-", np->n_name))
113 continue;
114 if(fshort) {
115 def_short++;
116 printf("%s\n", np->n_name+4);
117 } else
118 addfold(np->n_name+4);
119 }
120 if(def_short)
121 putchar('\n');
122 if(fshort) {
123 m_update();
124 fflush(stdout);
125 execl(lsproc, "mh-ls", "-x", cp, 0);
126 fprintf(stderr, "Can't exec: ");
127 perror(lsproc);
128 goto leave;
129 }
130 if(chdir(cp) < 0) {
131 fprintf(stderr, "Can't chdir to: ");
132 perror(cp);
133 goto leave;
134 }
135 if((cp = m_find(pfolder)) == NULL)
136 *folder = 0;
137 else
138 copy(cp, folder);
139 dirp = opendir(".");
140 (void) readdir(dirp);
141 (void) readdir(dirp);
142 while (dir = readdir(dirp)) {
143 if (stat(dir->d_name, &stbf) < 0 ||
144 (stbf.st_mode & S_IFMT) != S_IFDIR)
145 continue;
146 addfold(dir->d_name);
147 }
148 closedir(dirp);
149 for(i = 0; i < foldp; i++) {
150 pfold(folds[i], 0); fflush(stdout);
151 }
152 if(!totonly)
153 printf("\n\t\t ");
154 printf("TOTAL= %3d message%c in %d Folder%s.\n",
155 msgtot, msgtot!=1? 's':' ',
156 foldtot, foldtot!=1? "s":"");
157 } else {
158 hdrflag++;
159 if(argfolder)
160 cp = copy(argfolder, folder);
161 else
162 cp = copy(m_getfolder(), folder);
163 if(up) {
164 while(cp > folder && *cp != '/') --cp;
165 if(cp > folder)
166 *cp = 0;
167 argfolder = folder;
168 } else if(down) {
169 copy(listname, copy("/", cp));
170 argfolder = folder;
171 }
172 if(pfold(folder, curm) && argfolder)
173 m_replace(pfolder, argfolder);
174 }
175
176 leave:
177 m_update();
178 done(0);
179 }
180
181
182 addfold(fold)
183 char *fold;
184 {
185 register int i,j;
186 register char *cp;
187
188 if(foldp >= NFOLDERS) {
189 fprintf(stderr, "More than %d folders!!\n", NFOLDERS);
190 return(1);
191 }
192 cp = getcpy(fold);
193 for(i = 0; i < foldp; i++)
194 if(compare(cp, folds[i]) < 0) {
195 for(j = foldp - 1; j >= i; j--)
196 folds[j+1] = folds[j];
197 foldp++;
198 folds[i] = cp;
199 return(0);
200 }
201 folds[foldp++] = cp;
202 return(0);
203 }
204
205
206 pfold(fold, curm)
207 char *fold;
208 {
209 register char *mailfile;
210 register int msgnum, hole;
211 char newmsg[8], oldmsg[8];
212
213 mailfile = m_maildir(fold);
214 if(chdir(mailfile) < 0) {
215 fprintf(stderr, "Can't chdir to: ");
216 perror(mailfile);
217 return(0);
218 }
219 if(fshort) {
220 printf("%s\n", fold);
221 return(0);
222 }
223 mp = m_gmsg(fold);
224 foldtot++;
225 msgtot += mp->nummsg;
226 if (debug)
227 printf("pfold: %s message flags: 0%o\n", fold, mp->msgflags);
228 if(fpack && (mp->msgflags & READONLY) == 0) {
229 if (debug) {
230 printf("Would normally be packing folder %s\n", fold);
231 return;
232 }
233 for(msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
234 if(mp->msgstats[msgnum]&EXISTS) {
235 if(msgnum != hole) {
236 copy(m_name(hole), newmsg);
237 copy(m_name(msgnum), oldmsg);
238 if(link(oldmsg, newmsg) == -1 ||
239 unlink(oldmsg) == -1) {
240 fprintf(stderr, "Error moving %s to ", oldmsg);
241 perror(newmsg);
242 done(1);
243 }
244 mp->msgstats[hole] = mp->msgstats[msgnum];
245 if(msgnum == mp->lowsel)
246 mp->lowsel = hole;
247 if(msgnum == mp->hghsel)
248 mp->hghsel = hole;
249 }
250 hole++;
251 }
252 }
253 if(mp->nummsg > 0) {
254 mp->lowmsg = 1;
255 mp->hghmsg = hole - 1;
256 }
257 }
258 if(totonly)
259 goto out;
260 if(curm) {
261 if(!m_convert(curm))
262 return(0);
263 if(mp->numsel > 1) {
264 fprintf(stderr, "Can't set current msg to range: %s\n", curm);
265 return(0);
266 }
267 m_setcur(mp->curmsg = mp->hghsel);
268 }
269 if(!hdrflag++)
270 printf("\t\tFolder # of messages ( range ); cur msg (other files)\n");
271 printf("%22s", fold);
272 if(strcmp(folder, fold) == 0)
273 printf("+ ");
274 else
275 printf(" ");
276 if(mp->hghmsg == 0)
277 printf("has no messages");
278 else {
279 printf("has %3d message%s (%3d-%3d)",
280 mp->nummsg, (mp->nummsg==1)?" ":"s",
281 mp->lowmsg, mp->hghmsg);
282 if(mp->curmsg >= mp->lowmsg && mp->curmsg <= mp->hghmsg)
283 printf("; cur=%3s", m_name(mp->curmsg));
284 }
285 if(mp->selist || mp->others) {
286 printf("; (");
287 if(mp->selist) {
288 printf("%s", listname);
289 if(mp->others)
290 printf(", ");
291 }
292 if(mp->others)
293 printf("others");
294 putchar(')');
295 }
296 putchar('.');
297 putchar('\n');
298 out:
299 free(mp);
300 mp = 0;
301 return(1);
302 }
303
304
305 compare(s1, s2)
306 char *s1, *s2;
307 {
308 register char *c1, *c2;
309 register int i;
310
311 c1 = s1; c2 = s2;
312 while(*c1 || *c2)
313 if(i = *c1++ - *c2++)
314 return(i);
315 return(0);
316 }