11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1991, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Edward Sze-Tyan Wang.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
3387712Smarkm#include <sys/cdefs.h>
3487712Smarkm
3587712Smarkm__FBSDID("$FreeBSD$");
3687712Smarkm
371590Srgrimes#ifndef lint
3887712Smarkmstatic const char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
3969528Sasmodai#endif
401590Srgrimes
411590Srgrimes#include <sys/types.h>
421590Srgrimes#include <sys/stat.h>
4374876Sdwmalone#include <sys/mman.h>
4487712Smarkm
4587712Smarkm#include <err.h>
461590Srgrimes#include <errno.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <stdlib.h>
491590Srgrimes#include <string.h>
5087712Smarkm#include <unistd.h>
5187712Smarkm
521590Srgrimes#include "extern.h"
531590Srgrimes
541590Srgrimesvoid
55193488Sbrianierr(const char *fname)
561590Srgrimes{
5717833Sadam	warn("%s", fname);
5817833Sadam	rval = 1;
591590Srgrimes}
601590Srgrimes
611590Srgrimesvoid
62201382Sedoerr(void)
631590Srgrimes{
6417825Speter	err(1, "stdout");
651590Srgrimes}
6674876Sdwmalone
6774876Sdwmalone/*
6874876Sdwmalone * Print `len' bytes from the file associated with `mip', starting at
6974876Sdwmalone * absolute file offset `startoff'. May move map window.
7074876Sdwmalone */
7174876Sdwmaloneint
72137157Spaulmapprint(struct mapinfo *mip, off_t startoff, off_t len)
7374876Sdwmalone{
7474876Sdwmalone	int n;
7574876Sdwmalone
7674876Sdwmalone	while (len > 0) {
7774876Sdwmalone		if (startoff < mip->mapoff || startoff >= mip->mapoff +
78139994Sdwmalone		    (off_t)mip->maplen) {
7974876Sdwmalone			if (maparound(mip, startoff) != 0)
8074876Sdwmalone				return (1);
8174876Sdwmalone		}
8274876Sdwmalone		n = (mip->mapoff + mip->maplen) - startoff;
8374876Sdwmalone		if (n > len)
8474876Sdwmalone			n = len;
8574876Sdwmalone		WR(mip->start + (startoff - mip->mapoff), n);
8674876Sdwmalone		startoff += n;
8774876Sdwmalone		len -= n;
8874876Sdwmalone	}
8974876Sdwmalone	return (0);
9074876Sdwmalone}
9174876Sdwmalone
9274876Sdwmalone/*
9374876Sdwmalone * Move the map window so that it contains the byte at absolute file
9474876Sdwmalone * offset `offset'. The start of the map window will be TAILMAPLEN
9574876Sdwmalone * aligned.
9674876Sdwmalone */
9774876Sdwmaloneint
98137157Spaulmaparound(struct mapinfo *mip, off_t offset)
9974876Sdwmalone{
10074876Sdwmalone
10174876Sdwmalone	if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
10274876Sdwmalone		return (1);
10374876Sdwmalone
10474876Sdwmalone	mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
10574876Sdwmalone	mip->maplen = TAILMAPLEN;
106139994Sdwmalone	if ((off_t)mip->maplen > mip->maxoff - mip->mapoff)
10774876Sdwmalone		mip->maplen = mip->maxoff - mip->mapoff;
10874876Sdwmalone	if (mip->maplen <= 0)
10974876Sdwmalone		abort();
11074876Sdwmalone	if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED,
11174876Sdwmalone	     mip->fd, mip->mapoff)) == MAP_FAILED)
11274876Sdwmalone		return (1);
11374876Sdwmalone
11474876Sdwmalone	return (0);
11574876Sdwmalone}
116251565Sjh
117251565Sjh/*
118251565Sjh * Print the file name without stdio buffering.
119251565Sjh */
120251565Sjhvoid
121251565Sjhprintfn(const char *fn, int print_nl)
122251565Sjh{
123251565Sjh
124251565Sjh	if (print_nl)
125251565Sjh		WR("\n", 1);
126251565Sjh	WR("==> ", 4);
127251565Sjh	WR(fn, strlen(fn));
128251565Sjh	WR(" <==\n", 5);
129251565Sjh}
130