da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright (c) 1991, 1993
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The Regents of the University of California. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This code is derived from software contributed to Berkeley by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Edward Sze-Tyan Wang.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Redistribution and use in source and binary forms, with or without
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * modification, are permitted provided that the following conditions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * are met:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 1. Redistributions of source code must retain the above copyright
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * notice, this list of conditions and the following disclaimer.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 2. Redistributions in binary form must reproduce the above copyright
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * notice, this list of conditions and the following disclaimer in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * documentation and/or other materials provided with the distribution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 4. Neither the name of the University nor the names of its contributors
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * may be used to endorse or promote products derived from this software
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * without specific prior written permission.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * SUCH DAMAGE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/types.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/stat.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/mman.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <err.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <errno.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdio.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdlib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <unistd.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "extern.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinierr(const char *fname)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin warn("%s", fname);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin rval = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoerr(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err(1, "stdout");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Print `len' bytes from the file associated with `mip', starting at
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * absolute file offset `startoff'. May move map window.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmapprint(struct mapinfo *mip, off_t startoff, off_t len)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while (len > 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (startoff < mip->mapoff || startoff >= mip->mapoff +
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (off_t)mip->maplen) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (maparound(mip, startoff) != 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = (mip->mapoff + mip->maplen) - startoff;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (n > len)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = len;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin WR(mip->start + (startoff - mip->mapoff), n);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin startoff += n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin len -= n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Move the map window so that it contains the byte at absolute file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * offset `offset'. The start of the map window will be TAILMAPLEN
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * aligned.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmaparound(struct mapinfo *mip, off_t offset)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mip->maplen = TAILMAPLEN;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((off_t)mip->maplen > mip->maxoff - mip->mapoff)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mip->maplen = mip->maxoff - mip->mapoff;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (mip->maplen == 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin abort();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mip->fd, mip->mapoff)) == MAP_FAILED)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin