comparison uip/dropsbr.c @ 0:bce86c4163a3

Initial revision
author kono
date Mon, 18 Apr 2005 23:46:02 +0900
parents
children a6481689f99c
comparison
equal deleted inserted replaced
-1:000000000000 0:bce86c4163a3
1 /* dropsbr.c - write to a mailbox */
2 #ifndef lint
3 static char ident[] = "@(#)$Id$";
4 #endif /* lint */
5
6 #include <stdio.h>
7 #ifndef MMDFONLY
8 #include "../h/mh.h"
9 #include "../h/dropsbr.h"
10 #include "../zotnet/mts.h"
11 #else /* MMDFONLY */
12 #include "dropsbr.h"
13 #include "strings.h"
14 #include "mmdfonly.h"
15 #endif /* MMDFONLY */
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #if (defined(BSD42) || defined(SOCKETS)) && defined(NTOHLSWAP)
20 #undef NULLVP
21 #include <netinet/in.h>
22 #else
23 #define ntohl(n) (n)
24 #endif
25
26
27 #define MMDF 1
28 #define UUCP 2
29
30 /* */
31
32 static int mbx_style = MMDF;
33
34 static int mbx_create(), mbx_chk(), map_open();
35
36 extern int errno;
37
38 off_t lseek ();
39
40 /* */
41
42 int mbx_mmdf () {
43 int style = mbx_style;
44
45 mbx_style = MMDF;
46 return style;
47 }
48
49
50 int mbx_uucp () {
51 int style = mbx_style;
52
53 mbx_style = UUCP;
54 return style;
55 }
56
57 /* */
58
59 int mbx_open (file, uid, gid, mode)
60 char *file;
61 int uid,
62 gid,
63 mode;
64 {
65 int clear,
66 fd;
67
68 if ((fd = mbx_Xopen (file, uid, gid, mode, &clear)) == NOTOK)
69 return fd;
70
71 if (!clear)
72 switch (mbx_style) {
73 case MMDF:
74 default:
75 if (mbx_chk (fd) == NOTOK) {
76 (void) close (fd);
77 return NOTOK;
78 }
79 break;
80
81 case UUCP:
82 if (lseek (fd, (off_t)0, 2) == (off_t) NOTOK) {
83 (void) close (fd);
84 return NOTOK;
85 }
86 break;
87 }
88
89 return fd;
90 }
91
92 /* */
93
94 int mbx_Xopen (file, uid, gid, mode, clear)
95 char *file;
96 int uid,
97 gid,
98 mode,
99 *clear;
100 {
101 register int j;
102 int count,
103 fd;
104 struct stat st;
105
106 for (*clear = 0, count = 4, j = 0; count > 0; count--)
107 if ((fd = lkopen (file, 6)) == NOTOK)
108 switch (errno) {
109 case ENOENT:
110 if (mbx_create (file, uid, gid, mode) == NOTOK)
111 return NOTOK;
112 (*clear)++;
113 break;
114
115 #ifdef BSD42
116 case EWOULDBLOCK:
117 #endif /* BSD42 */
118 case ETXTBSY:
119 j = errno;
120 sleep (5);
121 break;
122
123 default:
124 return NOTOK;
125 }
126 else {
127 *clear = fstat (fd, &st) != NOTOK && st.st_size == (off_t)0;
128 break;
129 }
130
131 errno = j;
132 return fd;
133 }
134
135 /* */
136
137 static int mbx_create (file, uid, gid, mode)
138 char *file;
139 int uid,
140 gid,
141 mode;
142 {
143 int fd;
144
145 if ((fd = creat (file, 0600)) == NOTOK)
146 return NOTOK;
147
148 (void) close (fd);
149 (void) chown (file, uid, gid);
150 (void) chmod (file, mode);
151
152 return OK;
153 }
154
155
156 static int mbx_chk (fd)
157 int fd;
158 {
159 int count;
160 char ldelim[BUFSIZ];
161
162 count = strlen (mmdlm2);
163
164 if (lseek (fd, (off_t) (-count), 2) == (off_t) NOTOK
165 || read (fd, ldelim, count) != count)
166 return NOTOK;
167 ldelim[count] = 0;
168
169 if (strcmp (ldelim, mmdlm2)
170 && write (fd, "\n", 1) != 1
171 && write (fd, mmdlm2, count) != count)
172 return NOTOK;
173
174 return OK;
175 }
176
177 /* */
178
179 int mbx_read (fp, pos, drops, noisy)
180 register FILE *fp;
181 register long pos;
182 struct drop **drops;
183 int noisy;
184 {
185 register int len,
186 size;
187 register long ld1,
188 ld2;
189 register char *bp;
190 char buffer[BUFSIZ];
191 register struct drop *cp,
192 *dp,
193 *ep,
194 *pp;
195
196 pp = (struct drop *) calloc ((unsigned) (len = MAXFOLDER), sizeof *dp);
197 if (pp == NULL) {
198 if (noisy)
199 admonish (NULLCP, "unable to allocate drop storage");
200 return NOTOK;
201 }
202
203 ld1 = (long) strlen (mmdlm1);
204 ld2 = (long) strlen (mmdlm2);
205
206 (void) fseek (fp, pos, 0);
207 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof buffer, fp);) {
208 size = 0;
209 if (strcmp (buffer, mmdlm1) == 0)
210 pos += ld1, dp -> d_start = (long) pos;
211 else {
212 dp -> d_start = (long)pos , pos += (long) strlen (buffer);
213 for (bp = buffer; *bp; bp++, size++)
214 if (*bp == '\n')
215 size++;
216 }
217
218 while (fgets (buffer, sizeof buffer, fp) != NULL)
219 if (strcmp (buffer, mmdlm2) == 0)
220 break;
221 else {
222 pos += (long) strlen (buffer);
223 for (bp = buffer; *bp; bp++, size++)
224 if (*bp == '\n')
225 size++;
226 }
227
228 if (dp -> d_start != (long) pos) {
229 dp -> d_id = 0;
230 dp -> d_size = (long) size;
231 dp -> d_stop = (long) pos;
232 dp++;
233 }
234 pos += ld2;
235
236 if (dp >= ep) {
237 register int curlen = dp - pp;
238
239 cp = (struct drop *) realloc ((char *) pp,
240 (unsigned) (len += MAXFOLDER) * sizeof *pp);
241 if (cp == NULL) {
242 if (noisy)
243 admonish (NULLCP, "unable to allocate drop storage");
244 free ((char *) pp);
245 return 0;
246 }
247 dp = cp + curlen, ep = (pp = cp) + len - 1;
248 }
249 }
250
251 if (dp == pp)
252 free ((char *) pp);
253 else
254 *drops = pp;
255 return (dp - pp);
256 }
257
258 /* */
259
260 int mbx_write (mailbox, md, fp, id, last, pos, stop, mapping, noisy)
261 char *mailbox;
262 register FILE *fp;
263 int md,
264 id,
265 mapping,
266 noisy;
267 long last;
268 register long pos,
269 stop;
270 {
271 register int i,
272 j,
273 size;
274 long start,
275 off;
276 register char *cp;
277 char buffer[BUFSIZ];
278
279 off = (long) lseek (md, (off_t)0, 1);
280 j = strlen (mmdlm1);
281 if (write (md, mmdlm1, j) != j)
282 return NOTOK;
283 start = (long) lseek (md, (off_t)0, 1);
284 size = 0;
285
286 (void) fseek (fp, pos, 0);
287 while (fgets (buffer, sizeof buffer, fp) != NULL && pos < stop) {
288 i = strlen (buffer);
289 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
290 continue;
291 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
292 continue;
293 if (write (md, buffer, i) != i)
294 return NOTOK;
295 pos += (long) i;
296 if (mapping)
297 for (cp = buffer; i-- > 0; size++)
298 if (*cp++ == '\n')
299 size++;
300 }
301
302 stop = (long) lseek (md, (off_t)0, 1);
303 j = strlen (mmdlm2);
304 if (write (md, mmdlm2, j) != j)
305 return NOTOK;
306 if (mapping)
307 (void) map_write (mailbox, md, id, last, start, stop, off, size, noisy);
308
309 return OK;
310 }
311
312 /* */
313
314 int mbx_copy (mailbox, md, fd, mapping, text, noisy)
315 char *mailbox;
316 int md,
317 fd,
318 mapping,
319 noisy;
320 char *text;
321 {
322 register int i,
323 j,
324 size;
325 long start,
326 stop,
327 pos;
328 register char *cp;
329 char buffer[BUFSIZ];
330 register FILE *fp;
331
332 pos = (long) lseek (md, (off_t)0, 1);
333 size = 0;
334
335 switch (mbx_style) {
336 case MMDF:
337 default:
338 j = strlen (mmdlm1);
339 if (write (md, mmdlm1, j) != j)
340 return NOTOK;
341 start = (long) lseek (md, (off_t)0, 1);
342
343 if (text) {
344 i = strlen (text);
345 if (write (md, text, i) != i)
346 return NOTOK;
347 for (cp = text; *cp++; size++)
348 if (*cp == '\n')
349 size++;
350 }
351
352 while ((i = read (fd, buffer, sizeof buffer)) > 0) {
353 for (j = 0;
354 (j = stringdex (mmdlm1, buffer)) >= 0;
355 buffer[j]++)
356 continue;
357 for (j = 0;
358 (j = stringdex (mmdlm2, buffer)) >= 0;
359 buffer[j]++)
360 continue;
361 if (write (md, buffer, i) != i)
362 return NOTOK;
363 if (mapping)
364 for (cp = buffer; i-- > 0; size++)
365 if (*cp++ == '\n')
366 size++;
367 }
368
369 stop = (long) lseek (md, (off_t)0, 1);
370 j = strlen (mmdlm2);
371 if (write (md, mmdlm2, j) != j)
372 return NOTOK;
373 if (mapping)
374 (void) map_write (mailbox, md, 0, (long)0, start, stop, pos, size,
375 noisy);
376
377 return (i != NOTOK ? OK : NOTOK);
378
379 case UUCP: /* I hate this... */
380 if ((j = dup (fd)) == NOTOK)
381 return NOTOK;
382 if ((fp = fdopen (j, "r")) == NULL) {
383 (void) close (j);
384 return NOTOK;
385 }
386 start = (long) lseek (md, (off_t)0, 1);
387
388 if (text) {
389 i = strlen (text);
390 if (write (md, text, i) != i)
391 return NOTOK;
392 for (cp = text; *cp++; size++)
393 if (*cp == '\n')
394 size++;
395 }
396
397 for (j = 0; fgets (buffer, sizeof buffer, fp) != NULL; j++) {
398 #ifndef CONTENT_LENGTH
399 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
400 (void) write (md, ">", 1);
401 size++;
402 }
403 #endif
404 i = strlen (buffer);
405 if (write (md, buffer, i) != i) {
406 (void) fclose (fp);
407 return NOTOK;
408 }
409 if (mapping)
410 for (cp = buffer; i-- > 0; size++)
411 if (*cp++ == '\n')
412 size++;
413 }
414 if (write (md, "\n", 1) != 1) {
415 (void) fclose (fp);
416 return NOTOK;
417 }
418 if (mapping) size += 2;
419
420 (void) fclose (fp);
421 (void) (long) lseek (fd, (off_t)0, 2);
422 stop = (long) lseek (md, (off_t)0, 1);
423 if (mapping)
424 (void) map_write (mailbox, md, 0, (long)0, start, stop, pos, size,
425 noisy);
426
427 return OK;
428 }
429 }
430
431 /* */
432
433 int mbx_size (md, start, stop)
434 int md;
435 long start,
436 stop;
437 {
438 register int i,
439 fd;
440 register long pos;
441 register FILE *fp;
442
443 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
444 if (fd != NOTOK)
445 (void) close (fd);
446 return NOTOK;
447 }
448
449 (void) fseek (fp, start, 0);
450 for (i = 0, pos = stop - start; pos-- > 0; i++)
451 if (fgetc (fp) == '\n')
452 i++;
453
454 (void) fclose (fp);
455
456 return i;
457 }
458
459 /* */
460
461 int mbx_close (mailbox, md)
462 char *mailbox;
463 int md;
464 {
465 (void) lkclose (md, mailbox);
466
467 return OK;
468 }
469
470 /* */
471
472 /* This function is performed implicitly by getbbent.c:
473
474 bb -> bb_map = map_name (bb -> bb_file);
475 */
476
477 char *map_name (file)
478 register char *file;
479 {
480 register char *cp;
481 static char buffer[BUFSIZ];
482
483 if ((cp = r1bindex (file, '/')) == file)
484 (void) sprintf (buffer, ".%s.map", cp);
485 else
486 (void) sprintf (buffer, "%.*s.%s.map", cp - file, file, cp);
487
488 return buffer;
489 }
490
491 /* */
492
493 int map_read (file, pos, drops, noisy)
494 char *file;
495 long pos;
496 struct drop **drops;
497 int noisy;
498 {
499 register int i, j,
500 md,
501 msgp;
502 register char *cp;
503 struct drop d;
504 register struct drop *mp,
505 *dp;
506
507 if ((md = open (cp = map_name (file), 0)) == NOTOK
508 || map_chk (cp, md, mp = &d, pos, noisy)) {
509 if (md != NOTOK)
510 (void) close (md);
511 return 0;
512 }
513
514 msgp = mp -> d_id;
515 dp = (struct drop *) calloc ((unsigned) (msgp + 1), sizeof *dp);
516 if (dp == NULL) {
517 (void) close (md);
518 return 0;
519 }
520
521 bcopy ((char *) mp, (char *) dp, sizeof *dp);
522
523 (void) lseek (md, (off_t) sizeof *mp, 0);
524 if ((i = read (md, (char *) (dp + 1), msgp * sizeof *dp)) < sizeof *dp) {
525 i = 0;
526 free ((char *) dp);
527 }
528 else {
529 #ifdef NTOHLSWAP
530 register struct drop *tdp;
531 for (j = 0, tdp = dp; j <= i / sizeof(*dp); j++, tdp++) {
532 tdp->d_id = ntohl(tdp->d_id);
533 tdp->d_size = ntohl(tdp->d_size);
534 tdp->d_start = ntohl(tdp->d_start);
535 tdp->d_stop = ntohl(tdp->d_stop);
536 }
537 #endif
538 *drops = dp;
539 }
540
541 (void) close (md);
542
543 return (i / sizeof *dp);
544 }
545
546 /* */
547
548 int map_write (mailbox, md, id, last, start, stop, pos, size, noisy)
549 register char *mailbox;
550 int md,
551 id,
552 size,
553 noisy;
554 long last,
555 start,
556 stop,
557 pos;
558 {
559 register int i;
560 int clear,
561 fd,
562 td;
563 char *file;
564 register struct drop *dp;
565 struct drop d1,
566 d2,
567 *rp;
568 register FILE *fp;
569
570 if ((fd = map_open (file = map_name (mailbox), &clear, md)) == NOTOK)
571 return NOTOK;
572
573 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
574 (void) unlink (file);
575 (void) mbx_close (file, fd);
576 if ((fd = map_open (file, &clear, md)) == NOTOK)
577 return NOTOK;
578 clear++;
579 }
580
581 if (clear) {
582 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
583 if (noisy)
584 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
585 if (td != NOTOK)
586 (void) close (td);
587 (void) mbx_close (file, fd);
588 return NOTOK;
589 }
590
591 switch (i = mbx_read (fp, 0L, &rp, noisy)) {
592 case NOTOK:
593 (void) fclose (fp);
594 (void) mbx_close (file, fd);
595 return NOTOK;
596
597 case OK:
598 break;
599
600 default:
601 d1.d_id = 0;
602 for (dp = rp; i-- >0; dp++) {
603 if (dp -> d_start == start)
604 dp -> d_id = id;
605 #ifdef NTOHLSWAP
606 dp -> d_id = htonl(dp -> d_id);
607 dp -> d_size = htonl(dp -> d_size);
608 dp -> d_start = htonl(dp -> d_start);
609 dp -> d_stop = htonl(dp -> d_stop);
610 #endif
611 (void) lseek (fd, (off_t) (++d1.d_id * sizeof *dp), 0);
612 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
613 if (noisy)
614 admonish (file, "write error");
615 (void) mbx_close (file, fd);
616 (void) fclose (fp);
617 return NOTOK;
618 }
619 }
620 free ((char *) rp);
621 break;
622 }
623 }
624 else {
625 if (last == 0)
626 last = d1.d_start;
627 dp = &d2;
628 #ifdef NTOHLSWAP
629 dp -> d_id = htonl(id);
630 dp -> d_size = htonl((long) (size ? size : mbx_size (fd, start, stop)));
631 dp -> d_start = htonl((long) start);
632 dp -> d_stop = htonl((long) stop);
633 #else
634 dp -> d_id = id;
635 dp -> d_size = (long) (size ? size : mbx_size (fd, start, stop));
636 dp -> d_start = (long) start;
637 dp -> d_stop = (long) stop;
638 #endif
639 (void) lseek (fd, (off_t) (++d1.d_id * sizeof *dp), 0);
640 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
641 if (noisy)
642 admonish (file, "write error");
643 (void) mbx_close (file, fd);
644 return NOTOK;
645 }
646 }
647
648 dp = &d1;
649 #ifdef NTOHLSWAP
650 dp -> d_id = htonl(dp -> d_id);
651 dp -> d_size = htonl(DRVRSN);
652 dp -> d_start = htonl((long) last);
653 dp -> d_stop = htonl((long) lseek (md, (off_t)0, 1));
654 #else
655 dp -> d_size = DRVRSN;
656 dp -> d_start = (long) last;
657 dp -> d_stop = (long) lseek (md, (off_t)0, 1);
658 #endif
659
660 (void) lseek (fd, (off_t)0, 0);
661 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
662 if (noisy)
663 admonish (file, "write error");
664 (void) mbx_close (file, fd);
665 return NOTOK;
666 }
667
668 (void) mbx_close (file, fd);
669
670 return OK;
671 }
672
673 /* */
674
675 static int map_open (file, clear, md)
676 char *file;
677 int *clear,
678 md;
679 {
680 int mode;
681 struct stat st;
682
683 mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
684 return mbx_Xopen (file, st.st_uid, st.st_gid, mode, clear);
685 }
686
687 /* */
688
689 int map_chk (file, fd, dp, pos, noisy)
690 char *file;
691 int fd,
692 noisy;
693 register struct drop *dp;
694 long pos;
695 {
696 long count;
697 struct drop d, tmpd;
698 register struct drop *dl;
699
700 if (read (fd, (char *) &tmpd, sizeof *dp) != sizeof *dp) {
701 #ifdef notdef
702 admonish (NULLCP, "%s: missing or partial index", file);
703 #endif /* notdef */
704 return NOTOK;
705 }
706 #ifndef NTOHLSWAP
707 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
708 #else
709 dp->d_id = ntohl(tmpd.d_id);
710 dp->d_size = ntohl(tmpd.d_size);
711 dp->d_start = ntohl(tmpd.d_start);
712 dp->d_stop = ntohl(tmpd.d_stop);
713 #endif
714
715 if (dp -> d_size != DRVRSN) {
716 if (noisy)
717 admonish (NULLCP, "%s: version mismatch (%d != %d)", file,
718 dp->d_size, DRVRSN);
719 return NOTOK;
720 }
721
722 if (dp -> d_stop != (long) pos) {
723 if (noisy && pos != (long)0)
724 admonish (NULLCP,
725 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
726 file, dp -> d_stop, (long) pos);
727 return NOTOK;
728 }
729
730 if ((long) ((dp -> d_id + 1) * sizeof *dp) != (long) lseek (fd, (off_t)0, 2)) {
731 if (noisy)
732 admonish (NULLCP, "%s: corrupt index(1)", file);
733 return NOTOK;
734 }
735
736 dl = &d;
737 count = (long) strlen (mmdlm2);
738 (void) lseek (fd, (off_t) (dp -> d_id * sizeof *dp), 0);
739 if (read (fd, (char *) dl, sizeof *dl) != sizeof *dl
740 #ifdef NTOHLSWAP
741 || (ntohl(dl -> d_stop) != dp -> d_stop
742 && ntohl(dl -> d_stop) + count != dp -> d_stop)) {
743 #else
744 || dl -> d_stop != dp -> d_stop
745 && dl -> d_stop + count != dp -> d_stop) {
746 #endif
747 if (noisy)
748 admonish (NULLCP, "%s: corrupt index(2)", file);
749 return NOTOK;
750 }
751
752 return OK;
753 }