xref: /titanic_50/usr/src/cmd/tail/misc.c (revision 209e49b2ff611e7d61ff58e13756ae67f51be550)
1*209e49b2SChris Love /*
2*209e49b2SChris Love  * Copyright (c) 1991, 1993
3*209e49b2SChris Love  *	The Regents of the University of California.  All rights reserved.
4*209e49b2SChris Love  *
5*209e49b2SChris Love  * This code is derived from software contributed to Berkeley by
6*209e49b2SChris Love  * Edward Sze-Tyan Wang.
7*209e49b2SChris Love  *
8*209e49b2SChris Love  * Redistribution and use in source and binary forms, with or without
9*209e49b2SChris Love  * modification, are permitted provided that the following conditions
10*209e49b2SChris Love  * are met:
11*209e49b2SChris Love  * 1. Redistributions of source code must retain the above copyright
12*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer.
13*209e49b2SChris Love  * 2. Redistributions in binary form must reproduce the above copyright
14*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer in the
15*209e49b2SChris Love  *    documentation and/or other materials provided with the distribution.
16*209e49b2SChris Love  * 4. Neither the name of the University nor the names of its contributors
17*209e49b2SChris Love  *    may be used to endorse or promote products derived from this software
18*209e49b2SChris Love  *    without specific prior written permission.
19*209e49b2SChris Love  *
20*209e49b2SChris Love  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*209e49b2SChris Love  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*209e49b2SChris Love  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*209e49b2SChris Love  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*209e49b2SChris Love  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*209e49b2SChris Love  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*209e49b2SChris Love  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*209e49b2SChris Love  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*209e49b2SChris Love  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*209e49b2SChris Love  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*209e49b2SChris Love  * SUCH DAMAGE.
31*209e49b2SChris Love  */
32*209e49b2SChris Love 
33*209e49b2SChris Love #include <sys/types.h>
34*209e49b2SChris Love #include <sys/stat.h>
35*209e49b2SChris Love #include <sys/mman.h>
36*209e49b2SChris Love 
37*209e49b2SChris Love #include <err.h>
38*209e49b2SChris Love #include <errno.h>
39*209e49b2SChris Love #include <stdio.h>
40*209e49b2SChris Love #include <stdlib.h>
41*209e49b2SChris Love #include <string.h>
42*209e49b2SChris Love #include <unistd.h>
43*209e49b2SChris Love 
44*209e49b2SChris Love #include "extern.h"
45*209e49b2SChris Love 
46*209e49b2SChris Love void
ierr(const char * fname)47*209e49b2SChris Love ierr(const char *fname)
48*209e49b2SChris Love {
49*209e49b2SChris Love 	warn("%s", fname);
50*209e49b2SChris Love 	rval = 1;
51*209e49b2SChris Love }
52*209e49b2SChris Love 
53*209e49b2SChris Love void
oerr(void)54*209e49b2SChris Love oerr(void)
55*209e49b2SChris Love {
56*209e49b2SChris Love 	err(1, "stdout");
57*209e49b2SChris Love }
58*209e49b2SChris Love 
59*209e49b2SChris Love /*
60*209e49b2SChris Love  * Print `len' bytes from the file associated with `mip', starting at
61*209e49b2SChris Love  * absolute file offset `startoff'. May move map window.
62*209e49b2SChris Love  */
63*209e49b2SChris Love int
mapprint(struct mapinfo * mip,off_t startoff,off_t len)64*209e49b2SChris Love mapprint(struct mapinfo *mip, off_t startoff, off_t len)
65*209e49b2SChris Love {
66*209e49b2SChris Love 	int n;
67*209e49b2SChris Love 
68*209e49b2SChris Love 	while (len > 0) {
69*209e49b2SChris Love 		if (startoff < mip->mapoff || startoff >= mip->mapoff +
70*209e49b2SChris Love 		    (off_t)mip->maplen) {
71*209e49b2SChris Love 			if (maparound(mip, startoff) != 0)
72*209e49b2SChris Love 				return (1);
73*209e49b2SChris Love 		}
74*209e49b2SChris Love 		n = (mip->mapoff + mip->maplen) - startoff;
75*209e49b2SChris Love 		if (n > len)
76*209e49b2SChris Love 			n = len;
77*209e49b2SChris Love 		WR(mip->start + (startoff - mip->mapoff), n);
78*209e49b2SChris Love 		startoff += n;
79*209e49b2SChris Love 		len -= n;
80*209e49b2SChris Love 	}
81*209e49b2SChris Love 	return (0);
82*209e49b2SChris Love }
83*209e49b2SChris Love 
84*209e49b2SChris Love /*
85*209e49b2SChris Love  * Move the map window so that it contains the byte at absolute file
86*209e49b2SChris Love  * offset `offset'. The start of the map window will be TAILMAPLEN
87*209e49b2SChris Love  * aligned.
88*209e49b2SChris Love  */
89*209e49b2SChris Love int
maparound(struct mapinfo * mip,off_t offset)90*209e49b2SChris Love maparound(struct mapinfo *mip, off_t offset)
91*209e49b2SChris Love {
92*209e49b2SChris Love 
93*209e49b2SChris Love 	if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
94*209e49b2SChris Love 		return (1);
95*209e49b2SChris Love 
96*209e49b2SChris Love 	mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
97*209e49b2SChris Love 	mip->maplen = TAILMAPLEN;
98*209e49b2SChris Love 	if ((off_t)mip->maplen > mip->maxoff - mip->mapoff)
99*209e49b2SChris Love 		mip->maplen = mip->maxoff - mip->mapoff;
100*209e49b2SChris Love 	if (mip->maplen == 0)
101*209e49b2SChris Love 		abort();
102*209e49b2SChris Love 	if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED,
103*209e49b2SChris Love 	    mip->fd, mip->mapoff)) == MAP_FAILED)
104*209e49b2SChris Love 		return (1);
105*209e49b2SChris Love 
106*209e49b2SChris Love 	return (0);
107*209e49b2SChris Love }
108