misc.c revision 193488
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 193488 2009-06-05 09:08:53Z brian $");
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
59193488Sbrianierr(const char *fname)
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
76137157Spaulmapprint(struct mapinfo *mip, off_t startoff, off_t len)
7774876Sdwmalone{
7874876Sdwmalone	int n;
7974876Sdwmalone
8074876Sdwmalone	while (len > 0) {
8174876Sdwmalone		if (startoff < mip->mapoff || startoff >= mip->mapoff +
82139994Sdwmalone		    (off_t)mip->maplen) {
8374876Sdwmalone			if (maparound(mip, startoff) != 0)
8474876Sdwmalone				return (1);
8574876Sdwmalone		}
8674876Sdwmalone		n = (mip->mapoff + mip->maplen) - startoff;
8774876Sdwmalone		if (n > len)
8874876Sdwmalone			n = len;
8974876Sdwmalone		WR(mip->start + (startoff - mip->mapoff), n);
9074876Sdwmalone		startoff += n;
9174876Sdwmalone		len -= n;
9274876Sdwmalone	}
9374876Sdwmalone	return (0);
9474876Sdwmalone}
9574876Sdwmalone
9674876Sdwmalone/*
9774876Sdwmalone * Move the map window so that it contains the byte at absolute file
9874876Sdwmalone * offset `offset'. The start of the map window will be TAILMAPLEN
9974876Sdwmalone * aligned.
10074876Sdwmalone */
10174876Sdwmaloneint
102137157Spaulmaparound(struct mapinfo *mip, off_t offset)
10374876Sdwmalone{
10474876Sdwmalone
10574876Sdwmalone	if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
10674876Sdwmalone		return (1);
10774876Sdwmalone
10874876Sdwmalone	mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
10974876Sdwmalone	mip->maplen = TAILMAPLEN;
110139994Sdwmalone	if ((off_t)mip->maplen > mip->maxoff - mip->mapoff)
11174876Sdwmalone		mip->maplen = mip->maxoff - mip->mapoff;
11274876Sdwmalone	if (mip->maplen <= 0)
11374876Sdwmalone		abort();
11474876Sdwmalone	if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED,
11574876Sdwmalone	     mip->fd, mip->mapoff)) == MAP_FAILED)
11674876Sdwmalone		return (1);
11774876Sdwmalone
11874876Sdwmalone	return (0);
11974876Sdwmalone}
120