9 date 93.08.25.17.43.26; author jromine; state Exp;
14 date 93.08.25.17.22.52; author jromine; state Exp;
19 date 93.08.25.17.22.44; author jromine; state Exp;
33 @/* dropsbr.c - write to a mailbox */
35 static char Id[] = "@@(#)$Id:$";
41 #include "../h/dropsbr.h"
42 #include "../zotnet/mts.h"
49 #include <sys/types.h>
58 static int mbx_style = MMDF;
68 int style = mbx_style;
76 int style = mbx_style;
84 int mbx_open (file, uid, gid, mode)
93 if ((fd = mbx_Xopen (file, uid, gid, mode, &clear)) == NOTOK)
100 if (mbx_chk (fd) == NOTOK) {
107 if (lseek (fd, (off_t)0, 2) == (off_t) NOTOK) {
119 int mbx_Xopen (file, uid, gid, mode, clear)
131 for (*clear = 0, count = 4, j = 0; count > 0; count--)
132 if ((fd = lkopen (file, 6)) == NOTOK)
135 if (mbx_create (file, uid, gid, mode) == NOTOK)
152 *clear = fstat (fd, &st) != NOTOK && st.st_size == 0L;
162 static int mbx_create (file, uid, gid, mode)
170 if ((fd = creat (file, 0600)) == NOTOK)
174 (void) chown (file, uid, gid);
175 (void) chmod (file, mode);
181 static int mbx_chk (fd)
187 count = strlen (mmdlm2);
189 if (lseek (fd, (off_t) (-count), 2) == (off_t) NOTOK
190 || read (fd, ldelim, count) != count)
192 ldelim[count] = NULL;
194 if (strcmp (ldelim, mmdlm2)
195 && write (fd, "\n", 1) != 1
196 && write (fd, mmdlm2, count) != count)
204 int mbx_read (fp, pos, drops, noisy)
216 register struct drop *cp,
221 pp = (struct drop *) calloc ((unsigned) (len = MAXFOLDER), sizeof *dp);
224 admonish (NULLCP, "unable to allocate drop storage");
228 ld1 = (long) strlen (mmdlm1);
229 ld2 = (long) strlen (mmdlm2);
231 (void) fseek (fp, pos, 0);
232 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof buffer, fp);) {
234 if (strcmp (buffer, mmdlm1) == 0)
235 pos += ld1, dp -> d_start = pos;
237 dp -> d_start = pos, pos += (long) strlen (buffer);
238 for (bp = buffer; *bp; bp++, size++)
243 while (fgets (buffer, sizeof buffer, fp) != NULL)
244 if (strcmp (buffer, mmdlm2) == 0)
247 pos += (long) strlen (buffer);
248 for (bp = buffer; *bp; bp++, size++)
253 if (dp -> d_start != pos) {
262 register int curlen = dp - pp;
264 cp = (struct drop *) realloc ((char *) pp,
265 (unsigned) (len += MAXFOLDER) * sizeof *pp);
268 admonish (NULLCP, "unable to allocate drop storage");
272 dp = cp + curlen, ep = (pp = cp) + len - 1;
285 int mbx_write (mailbox, md, fp, id, last, pos, stop, mapping, noisy)
304 off = (long) lseek (md, (off_t)0, 1);
306 if (write (md, mmdlm1, j) != j)
308 start = (long) lseek (md, (off_t)0, 1);
311 (void) fseek (fp, pos, 0);
312 while (fgets (buffer, sizeof buffer, fp) != NULL && pos < stop) {
314 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
316 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
318 if (write (md, buffer, i) != i)
322 for (cp = buffer; i-- > 0; size++)
327 stop = (long) lseek (md, (off_t)0, 1);
329 if (write (md, mmdlm2, j) != j)
332 (void) map_write (mailbox, md, id, last, start, stop, off, size, noisy);
339 int mbx_copy (mailbox, md, fd, mapping, text, noisy)
357 pos = (long) lseek (md, (off_t)0, 1);
364 if (write (md, mmdlm1, j) != j)
366 start = (long) lseek (md, (off_t)0, 1);
370 if (write (md, text, i) != i)
372 for (cp = text; *cp++; size++)
377 while ((i = read (fd, buffer, sizeof buffer)) > 0) {
379 (j = stringdex (mmdlm1, buffer)) >= 0;
383 (j = stringdex (mmdlm2, buffer)) >= 0;
386 if (write (md, buffer, i) != i)
389 for (cp = buffer; i-- > 0; size++)
394 stop = (long) lseek (md, (off_t)0, 1);
396 if (write (md, mmdlm2, j) != j)
399 (void) map_write (mailbox, md, 0, 0L, start, stop, pos, size,
402 return (i != NOTOK ? OK : NOTOK);
404 case UUCP: /* I hate this... */
405 if ((j = dup (fd)) == NOTOK)
407 if ((fp = fdopen (j, "r")) == NULL) {
411 start = (long) lseek (md, (off_t)0, 1);
415 if (write (md, text, i) != i)
417 for (cp = text; *cp++; size++)
422 for (j = 0; fgets (buffer, sizeof buffer, fp) != NULL; j++) {
423 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
424 (void) write (fd, ">", 1);
428 if (write (md, buffer, i) != i) {
433 for (cp = buffer; i-- > 0; size++)
439 (void) lseek (fd, (off_t)0, 2);
440 stop = (long) lseek (md, (off_t)0, 1);
442 (void) map_write (mailbox, md, 0, 0L, start, stop, pos, size,
451 int mbx_size (md, start, stop)
461 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
467 (void) fseek (fp, start, 0);
468 for (i = 0, pos = stop - start; pos-- > 0; i++)
469 if (fgetc (fp) == '\n')
479 int mbx_close (mailbox, md)
483 (void) lkclose (md, mailbox);
490 /* This function is performed implicitly by getbbent.c:
492 bb -> bb_map = map_name (bb -> bb_file);
495 char *map_name (file)
500 static char buffer[BUFSIZ];
502 if ((dp = index (cp = r1bindex (file, '/'), '.')) == NULL)
503 dp = cp + strlen (cp);
505 (void) sprintf (buffer, ".%.*s%s", dp - cp, cp, ".map");
507 (void) sprintf (buffer, "%.*s.%.*s%s", cp - file, file, dp - cp,
515 int map_read (file, pos, drops, noisy)
526 register struct drop *mp,
529 if ((md = open (cp = map_name (file), 0)) == NOTOK
530 || map_chk (cp, md, mp = &d, pos, noisy)) {
537 dp = (struct drop *) calloc ((unsigned) (msgp + 1), sizeof *dp);
543 bcopy ((char *) mp, (char *) dp, sizeof *dp);
545 (void) lseek (md, (off_t) sizeof *mp, 0);
546 if ((i = read (md, (char *) (dp + 1), msgp * sizeof *dp)) < sizeof *dp) {
555 return (i / sizeof *dp);
560 int map_write (mailbox, md, id, last, start, stop, pos, size, noisy)
561 register char *mailbox;
576 register struct drop *dp;
582 if ((fd = map_open (file = map_name (mailbox), &clear, md)) == NOTOK)
585 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
586 (void) unlink (file);
587 (void) mbx_close (file, fd);
588 if ((fd = map_open (file, &clear, md)) == NOTOK)
594 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
596 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
599 (void) mbx_close (file, fd);
603 switch (i = mbx_read (fp, 0L, &rp, noisy)) {
606 (void) mbx_close (file, fd);
614 for (dp = rp; i-- >0; dp++) {
615 if (dp -> d_start == start)
617 (void) lseek (fd, (off_t) (++d1.d_id * sizeof *dp), 0);
618 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
620 admonish (file, "write error");
621 (void) mbx_close (file, fd);
635 dp -> d_size = size ? size : mbx_size (fd, start, stop);
636 dp -> d_start = start;
638 (void) lseek (fd, (off_t) (++d1.d_id * sizeof *dp), 0);
639 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
641 admonish (file, "write error");
642 (void) mbx_close (file, fd);
648 dp -> d_size = DRVRSN;
649 dp -> d_start = last;
650 dp -> d_stop = (long) lseek (md, (off_t)0, 1);
652 (void) lseek (fd, (off_t)0, 0);
653 if (write (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
655 admonish (file, "write error");
656 (void) mbx_close (file, fd);
660 (void) mbx_close (file, fd);
667 static int map_open (file, clear, md)
675 mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
676 return mbx_Xopen (file, st.st_uid, st.st_gid, mode, clear);
681 int map_chk (file, fd, dp, pos, noisy)
685 register struct drop *dp;
690 register struct drop *dl;
692 if (read (fd, (char *) dp, sizeof *dp) != sizeof *dp) {
694 admonish (NULLCP, "%s: missing or partial index", file);
699 if (dp -> d_size != DRVRSN) {
701 admonish (NULLCP, "%s: version mismatch", file);
705 if (dp -> d_stop != pos) {
706 if (noisy && pos != 0L)
708 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
709 file, dp -> d_stop, pos);
713 if ((long) ((dp -> d_id + 1) * sizeof *dp) != (long) lseek (fd, (off_t)0, 2)) {
715 admonish (NULLCP, "%s: corrupt index(1)", file);
720 count = (long) strlen (mmdlm2);
721 (void) lseek (fd, (off_t) (dp -> d_id * sizeof *dp), 0);
722 if (read (fd, (char *) dl, sizeof *dl) != sizeof *dl
723 || (dl -> d_stop != dp -> d_stop
724 && dl -> d_stop + count != dp -> d_stop)) {
726 admonish (NULLCP, "%s: corrupt index(2)", file);
737 @off_t fixes for BSD44
754 if (lseek (fd, 0L, 2) == (long) NOTOK) {
757 if (lseek (fd, (long) (-count), 2) == (long) NOTOK
760 off = lseek (md, 0L, 1);
763 start = lseek (md, 0L, 1);
766 stop = lseek (md, 0L, 1);
769 pos = lseek (md, 0L, 1);
772 start = lseek (md, 0L, 1);
775 stop = lseek (md, 0L, 1);
778 start = lseek (md, 0L, 1);
781 (void) lseek (fd, 0L, 2);
782 stop = lseek (md, 0L, 1);
785 (void) lseek (md, (long) sizeof *mp, 0);
788 (void) lseek (fd, (long) (++d1.d_id * sizeof *dp), 0);
791 (void) lseek (fd, (long) (++d1.d_id * sizeof *dp), 0);
794 dp -> d_stop = lseek (md, 0L, 1);
797 (void) lseek (fd, 0L, 0);
800 if ((long) ((dp -> d_id + 1) * sizeof *dp) != lseek (fd, 0L, 2)) {
803 (void) lseek (fd, (long) (dp -> d_id * sizeof *dp), 0);