misc.c revision 87712
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 * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
3787712Smarkm#include <sys/cdefs.h>
3887712Smarkm
3987712Smarkm__FBSDID("$FreeBSD: head/usr.bin/tail/misc.c 87712 2001-12-12 00:01:16Z markm $");
4087712Smarkm
411590Srgrimes#ifndef lint
4287712Smarkmstatic const char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
4369528Sasmodai#endif
441590Srgrimes
451590Srgrimes#include <sys/types.h>
461590Srgrimes#include <sys/stat.h>
4774876Sdwmalone#include <sys/mman.h>
4887712Smarkm
4987712Smarkm#include <err.h>
501590Srgrimes#include <errno.h>
511590Srgrimes#include <stdio.h>
521590Srgrimes#include <stdlib.h>
531590Srgrimes#include <string.h>
5487712Smarkm#include <unistd.h>
5587712Smarkm
561590Srgrimes#include "extern.h"
571590Srgrimes
581590Srgrimesvoid
591590Srgrimesierr()
601590Srgrimes{
6117833Sadam	warn("%s", fname);
6217833Sadam	rval = 1;
631590Srgrimes}
641590Srgrimes
651590Srgrimesvoid
661590Srgrimesoerr()
671590Srgrimes{
6817825Speter	err(1, "stdout");
691590Srgrimes}
7074876Sdwmalone
7174876Sdwmalone/*
7274876Sdwmalone * Print `len' bytes from the file associated with `mip', starting at
7374876Sdwmalone * absolute file offset `startoff'. May move map window.
7474876Sdwmalone */
7574876Sdwmaloneint
7674876Sdwmalonemapprint(mip, startoff, len)
7774876Sdwmalone	struct mapinfo *mip;
7874876Sdwmalone	off_t startoff, len;
7974876Sdwmalone{
8074876Sdwmalone	int n;
8174876Sdwmalone
8274876Sdwmalone	while (len > 0) {
8374876Sdwmalone		if (startoff < mip->mapoff || startoff >= mip->mapoff +
8474876Sdwmalone		    mip->maplen) {
8574876Sdwmalone			if (maparound(mip, startoff) != 0)
8674876Sdwmalone				return (1);
8774876Sdwmalone		}
8874876Sdwmalone		n = (mip->mapoff + mip->maplen) - startoff;
8974876Sdwmalone		if (n > len)
9074876Sdwmalone			n = len;
9174876Sdwmalone		WR(mip->start + (startoff - mip->mapoff), n);
9274876Sdwmalone		startoff += n;
9374876Sdwmalone		len -= n;
9474876Sdwmalone	}
9574876Sdwmalone	return (0);
9674876Sdwmalone}
9774876Sdwmalone
9874876Sdwmalone/*
9974876Sdwmalone * Move the map window so that it contains the byte at absolute file
10074876Sdwmalone * offset `offset'. The start of the map window will be TAILMAPLEN
10174876Sdwmalone * aligned.
10274876Sdwmalone */
10374876Sdwmaloneint
10474876Sdwmalonemaparound(mip, offset)
10574876Sdwmalone	struct mapinfo *mip;
10674876Sdwmalone	off_t offset;
10774876Sdwmalone{
10874876Sdwmalone
10974876Sdwmalone	if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
11074876Sdwmalone		return (1);
11174876Sdwmalone
11274876Sdwmalone	mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
11374876Sdwmalone	mip->maplen = TAILMAPLEN;
11474876Sdwmalone	if (mip->maplen > mip->maxoff - mip->mapoff)
11574876Sdwmalone		mip->maplen = mip->maxoff - mip->mapoff;
11674876Sdwmalone	if (mip->maplen <= 0)
11774876Sdwmalone		abort();
11874876Sdwmalone	if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED,
11974876Sdwmalone	     mip->fd, mip->mapoff)) == MAP_FAILED)
12074876Sdwmalone		return (1);
12174876Sdwmalone
12274876Sdwmalone	return (0);
12374876Sdwmalone}
124