- if (mapping)
- size += 2;
-
- fclose (fp);
- lseek (fd, (off_t) 0, SEEK_END);
- stop = lseek (md, (off_t) 0, SEEK_CUR);
- if (mapping)
- map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
-
- return OK;
- }
-}
-
-
-int
-mbx_size (int md, off_t start, off_t stop)
-{
- register int i, fd;
- register long pos;
- register FILE *fp;
-
- if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
- if (fd != NOTOK)
- close (fd);
- return NOTOK;
- }
-
- fseek (fp, start, SEEK_SET);
- for (i = 0, pos = stop - start; pos-- > 0; i++)
- if (fgetc (fp) == '\n')
- i++;
-
- fclose (fp);
- return i;
-}
-
-
-/*
- * Close and unlock file/maildrop.
- */
-
-int
-mbx_close (char *mailbox, int md)
-{
- if (lkclose (md, mailbox) == 0)
- return OK;
- return NOTOK;
-}
-
-
-/*
- * This function is performed implicitly by getbbent.c:
- * bb->bb_map = map_name (bb->bb_file);
- */
-
-char *
-map_name (char *file)
-{
- register char *cp, *dp;
- static char buffer[BUFSIZ];
-
- if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
- dp = cp + strlen (cp);
- if (cp == file)
- snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
- else
- snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
- (int)(cp - file), file, (int)(dp - cp), cp, ".map");
-
- return buffer;
-}
-
-
-int
-map_read (char *file, long pos, struct drop **drops, int noisy)
-{
- register int i, md, msgp;
- register char *cp;
- struct drop d;
- register struct drop *mp, *dp;
-
- if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
- || map_chk (cp, md, mp = &d, pos, noisy)) {
- if (md != NOTOK)
- close (md);
- return 0;
- }
-
- msgp = mp->d_id;
- dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
- if (dp == NULL) {
- close (md);
- return 0;
- }
-
- memcpy((char *) dp, (char *) mp, sizeof(*dp));
-
- lseek (md, (off_t) sizeof(*mp), SEEK_SET);
- if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
- i = 0;
- free ((char *) dp);
- } else {
-#ifdef NTOHLSWAP
- register struct drop *tdp;
- int j;
-
- for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
- tdp->d_id = ntohl(tdp->d_id);
- tdp->d_size = ntohl(tdp->d_size);
- tdp->d_start = ntohl(tdp->d_start);
- tdp->d_stop = ntohl(tdp->d_stop);
- }
-#endif
- *drops = dp;
- }
-
- close (md);
-
- return (i / sizeof(*dp));
-}
-
-
-int
-map_write (char *mailbox, int md, int id, long last, off_t start,
- off_t stop, long pos, int size, int noisy)
-{
- register int i;
- int clear, fd, td;
- char *file;
- register struct drop *dp;
- struct drop d1, d2, *rp;
- register FILE *fp;
- struct stat st;
-
- if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
- return NOTOK;
-
- if ((fstat (fd, &st) == OK) && (st.st_size > 0))
- clear = 0;
- else
- clear = 1;
-
- if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
- unlink (file);
- mbx_close (file, fd);
- if ((fd = map_open (file, md)) == NOTOK)
- return NOTOK;
- clear++;
- }
-
- if (clear) {
- if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
- if (noisy)
- admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
- if (td != NOTOK)
- close (td);
- mbx_close (file, fd);
- return NOTOK;