Index: head/usr.bin/tail/extern.h =================================================================== --- head/usr.bin/tail/extern.h (revision 348841) +++ head/usr.bin/tail/extern.h (revision 348842) @@ -1,80 +1,81 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)extern.h 8.1 (Berkeley) 6/6/93 * * $FreeBSD$ */ #define WR(p, size) do { \ ssize_t res; \ res = write(STDOUT_FILENO, p, size); \ if (res != (ssize_t)size) { \ if (res == -1) \ oerr(); \ else \ errx(1, "stdout"); \ } \ } while (0) #define TAILMAPLEN (4<<20) struct mapinfo { off_t mapoff; off_t maxoff; size_t maplen; char *start; int fd; }; struct file_info { FILE *fp; char *file_name; struct stat st; }; typedef struct file_info file_info_t; enum STYLE { NOTSET = 0, FBYTES, FLINES, RBYTES, RLINES, REVERSE }; void follow(file_info_t *, enum STYLE, off_t); void forward(FILE *, const char *, enum STYLE, off_t, struct stat *); void reverse(FILE *, const char *, enum STYLE, off_t, struct stat *); int bytes(FILE *, const char *, off_t); int lines(FILE *, const char *, off_t); void ierr(const char *); void oerr(void); int mapprint(struct mapinfo *, off_t, off_t); int maparound(struct mapinfo *, off_t); void printfn(const char *, int); extern int Fflag, fflag, qflag, rflag, rval, no_files; +extern fileargs_t *fa; Index: head/usr.bin/tail/forward.c =================================================================== --- head/usr.bin/tail/forward.c (revision 348841) +++ head/usr.bin/tail/forward.c (revision 348842) @@ -1,425 +1,435 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Edward Sze-Tyan Wang. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include + #include "extern.h" static void rlines(FILE *, const char *fn, off_t, struct stat *); static int show(file_info_t *); static void set_events(file_info_t *files); /* defines for inner loop actions */ #define USE_SLEEP 0 #define USE_KQUEUE 1 #define ADD_EVENTS 2 static struct kevent *ev; static int action = USE_SLEEP; static int kq; static const file_info_t *last; /* * forward -- display the file, from an offset, forward. * * There are eight separate cases for this -- regular and non-regular * files, by bytes or lines and from the beginning or end of the file. * * FBYTES byte offset from the beginning of the file * REG seek * NOREG read, counting bytes * * FLINES line offset from the beginning of the file * REG read, counting lines * NOREG read, counting lines * * RBYTES byte offset from the end of the file * REG seek * NOREG cyclically read characters into a wrap-around buffer * * RLINES * REG mmap the file and step back until reach the correct offset. * NOREG cyclically read lines into a wrap-around array of buffers */ void forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp) { int ch; switch(style) { case FBYTES: if (off == 0) break; if (S_ISREG(sbp->st_mode)) { if (sbp->st_size < off) off = sbp->st_size; if (fseeko(fp, off, SEEK_SET) == -1) { ierr(fn); return; } } else while (off--) if ((ch = getc(fp)) == EOF) { if (ferror(fp)) { ierr(fn); return; } break; } break; case FLINES: if (off == 0) break; for (;;) { if ((ch = getc(fp)) == EOF) { if (ferror(fp)) { ierr(fn); return; } break; } if (ch == '\n' && !--off) break; } break; case RBYTES: if (S_ISREG(sbp->st_mode)) { if (sbp->st_size >= off && fseeko(fp, -off, SEEK_END) == -1) { ierr(fn); return; } } else if (off == 0) { while (getc(fp) != EOF); if (ferror(fp)) { ierr(fn); return; } } else if (bytes(fp, fn, off)) return; break; case RLINES: if (S_ISREG(sbp->st_mode)) if (!off) { if (fseeko(fp, (off_t)0, SEEK_END) == -1) { ierr(fn); return; } } else rlines(fp, fn, off, sbp); else if (off == 0) { while (getc(fp) != EOF); if (ferror(fp)) { ierr(fn); return; } } else if (lines(fp, fn, off)) return; break; default: break; } while ((ch = getc(fp)) != EOF) if (putchar(ch) == EOF) oerr(); if (ferror(fp)) { ierr(fn); return; } (void)fflush(stdout); } /* * rlines -- display the last offset lines of the file. */ static void rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp) { struct mapinfo map; off_t curoff, size; int i; if (!(size = sbp->st_size)) return; map.start = NULL; map.fd = fileno(fp); map.mapoff = map.maxoff = size; /* * Last char is special, ignore whether newline or not. Note that * size == 0 is dealt with above, and size == 1 sets curoff to -1. */ curoff = size - 2; while (curoff >= 0) { if (curoff < map.mapoff && maparound(&map, curoff) != 0) { ierr(fn); return; } for (i = curoff - map.mapoff; i >= 0; i--) if (map.start[i] == '\n' && --off == 0) break; /* `i' is either the map offset of a '\n', or -1. */ curoff = map.mapoff + i; if (i >= 0) break; } curoff++; if (mapprint(&map, curoff, size - curoff) != 0) { ierr(fn); exit(1); } /* Set the file pointer to reflect the length displayed. */ if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) { ierr(fn); return; } if (map.start != NULL && munmap(map.start, map.maplen)) { ierr(fn); return; } } static int show(file_info_t *file) { int ch; while ((ch = getc(file->fp)) != EOF) { if (last != file && no_files > 1) { if (!qflag) printfn(file->file_name, 1); last = file; } if (putchar(ch) == EOF) oerr(); } (void)fflush(stdout); if (ferror(file->fp)) { fclose(file->fp); file->fp = NULL; ierr(file->file_name); return 0; } clearerr(file->fp); return 1; } static void set_events(file_info_t *files) { int i, n = 0; file_info_t *file; struct timespec ts; struct statfs sf; ts.tv_sec = 0; ts.tv_nsec = 0; action = USE_KQUEUE; for (i = 0, file = files; i < no_files; i++, file++) { if (! file->fp) continue; if (fstatfs(fileno(file->fp), &sf) == 0 && (sf.f_flags & MNT_LOCAL) == 0) { action = USE_SLEEP; return; } if (Fflag && fileno(file->fp) != STDIN_FILENO) { EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_DELETE | NOTE_RENAME, 0, 0); n++; } EV_SET(&ev[n], fileno(file->fp), EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); n++; } if (kevent(kq, ev, n, NULL, 0, &ts) < 0) { action = USE_SLEEP; } } /* * follow -- display the file, from an offset, forward. * */ void follow(file_info_t *files, enum STYLE style, off_t off) { int active, ev_change, i, n = -1; struct stat sb2; file_info_t *file; + FILE *ftmp; struct timespec ts; /* Position each of the files */ file = files; active = 0; n = 0; for (i = 0; i < no_files; i++, file++) { if (file->fp) { active = 1; n++; if (no_files > 1 && !qflag) printfn(file->file_name, 1); forward(file->fp, file->file_name, style, off, &file->st); if (Fflag && fileno(file->fp) != STDIN_FILENO) n++; } } if (!Fflag && !active) return; last = --file; kq = kqueue(); if (kq < 0) err(1, "kqueue"); ev = malloc(n * sizeof(struct kevent)); if (! ev) err(1, "Couldn't allocate memory for kevents."); set_events(files); for (;;) { ev_change = 0; if (Fflag) { for (i = 0, file = files; i < no_files; i++, file++) { if (!file->fp) { - file->fp = fopen(file->file_name, "r"); + file->fp = + fileargs_fopen(fa, file->file_name, + "r"); if (file->fp != NULL && fstat(fileno(file->fp), &file->st) == -1) { fclose(file->fp); file->fp = NULL; } if (file->fp != NULL) ev_change++; continue; } if (fileno(file->fp) == STDIN_FILENO) continue; - if (stat(file->file_name, &sb2) == -1) { + ftmp = fileargs_fopen(fa, file->file_name, "r"); + if (ftmp == NULL || + fstat(fileno(file->fp), &sb2) == -1) { if (errno != ENOENT) ierr(file->file_name); show(file); if (file->fp != NULL) { fclose(file->fp); file->fp = NULL; } + if (ftmp != NULL) { + fclose(ftmp); + } ev_change++; continue; } if (sb2.st_ino != file->st.st_ino || sb2.st_dev != file->st.st_dev || sb2.st_nlink == 0) { show(file); - file->fp = freopen(file->file_name, "r", - file->fp); - if (file->fp != NULL) - memcpy(&file->st, &sb2, - sizeof(struct stat)); - else if (errno != ENOENT) - ierr(file->file_name); + fclose(file->fp); + file->fp = ftmp; + memcpy(&file->st, &sb2, + sizeof(struct stat)); ev_change++; + } else { + fclose(ftmp); } } } for (i = 0, file = files; i < no_files; i++, file++) if (file->fp && !show(file)) ev_change++; if (ev_change) set_events(files); switch (action) { case USE_KQUEUE: ts.tv_sec = 1; ts.tv_nsec = 0; /* * In the -F case we set a timeout to ensure that * we re-stat the file at least once every second. */ n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL); if (n < 0) err(1, "kevent"); if (n == 0) { /* timeout */ break; } else if (ev->filter == EVFILT_READ && ev->data < 0) { /* file shrank, reposition to end */ if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) { ierr(file->file_name); continue; } } break; case USE_SLEEP: (void) usleep(250000); break; } } } Index: head/usr.bin/tail/misc.c =================================================================== --- head/usr.bin/tail/misc.c (revision 348841) +++ head/usr.bin/tail/misc.c (revision 348842) @@ -1,131 +1,134 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Edward Sze-Tyan Wang. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include #include #include #include #include #include +#include +#include + #include "extern.h" void ierr(const char *fname) { warn("%s", fname); rval = 1; } void oerr(void) { err(1, "stdout"); } /* * Print `len' bytes from the file associated with `mip', starting at * absolute file offset `startoff'. May move map window. */ int mapprint(struct mapinfo *mip, off_t startoff, off_t len) { int n; while (len > 0) { if (startoff < mip->mapoff || startoff >= mip->mapoff + (off_t)mip->maplen) { if (maparound(mip, startoff) != 0) return (1); } n = (mip->mapoff + mip->maplen) - startoff; if (n > len) n = len; WR(mip->start + (startoff - mip->mapoff), n); startoff += n; len -= n; } return (0); } /* * Move the map window so that it contains the byte at absolute file * offset `offset'. The start of the map window will be TAILMAPLEN * aligned. */ int maparound(struct mapinfo *mip, off_t offset) { if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0) return (1); mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1); mip->maplen = TAILMAPLEN; if ((off_t)mip->maplen > mip->maxoff - mip->mapoff) mip->maplen = mip->maxoff - mip->mapoff; if (mip->maplen <= 0) abort(); if ((mip->start = mmap(NULL, mip->maplen, PROT_READ, MAP_SHARED, mip->fd, mip->mapoff)) == MAP_FAILED) return (1); return (0); } /* * Print the file name without stdio buffering. */ void printfn(const char *fn, int print_nl) { if (print_nl) WR("\n", 1); WR("==> ", 4); WR(fn, strlen(fn)); WR(" <==\n", 5); } Index: head/usr.bin/tail/read.c =================================================================== --- head/usr.bin/tail/read.c (revision 348841) +++ head/usr.bin/tail/read.c (revision 348842) @@ -1,211 +1,214 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Edward Sze-Tyan Wang. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include #include #include #include #include #include +#include +#include + #include "extern.h" /* * bytes -- read bytes to an offset from the end and display. * * This is the function that reads to a byte offset from the end of the input, * storing the data in a wrap-around buffer which is then displayed. If the * rflag is set, the data is displayed in lines in reverse order, and this * routine has the usual nastiness of trying to find the newlines. Otherwise, * it is displayed from the character closest to the beginning of the input to * the end. */ int bytes(FILE *fp, const char *fn, off_t off) { int ch, len, tlen; char *ep, *p, *t; int wrap; char *sp; if ((sp = p = malloc(off)) == NULL) err(1, "malloc"); for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) { *p = ch; if (++p == ep) { wrap = 1; p = sp; } } if (ferror(fp)) { ierr(fn); free(sp); return 1; } if (rflag) { for (t = p - 1, len = 0; t >= sp; --t, ++len) if (*t == '\n' && len) { WR(t + 1, len); len = 0; } if (wrap) { tlen = len; for (t = ep - 1, len = 0; t >= p; --t, ++len) if (*t == '\n') { if (len) { WR(t + 1, len); len = 0; } if (tlen) { WR(sp, tlen); tlen = 0; } } if (len) WR(t + 1, len); if (tlen) WR(sp, tlen); } } else { if (wrap && (len = ep - p)) WR(p, len); len = p - sp; if (len) WR(sp, len); } free(sp); return 0; } /* * lines -- read lines to an offset from the end and display. * * This is the function that reads to a line offset from the end of the input, * storing the data in an array of buffers which is then displayed. If the * rflag is set, the data is displayed in lines in reverse order, and this * routine has the usual nastiness of trying to find the newlines. Otherwise, * it is displayed from the line closest to the beginning of the input to * the end. */ int lines(FILE *fp, const char *fn, off_t off) { struct { int blen; u_int len; char *l; } *llines; int ch, rc; char *p, *sp; int blen, cnt, recno, wrap; if ((llines = calloc(off, sizeof(*llines))) == NULL) err(1, "calloc"); p = sp = NULL; blen = cnt = recno = wrap = 0; rc = 0; while ((ch = getc(fp)) != EOF) { if (++cnt > blen) { if ((sp = realloc(sp, blen += 1024)) == NULL) err(1, "realloc"); p = sp + cnt - 1; } *p++ = ch; if (ch == '\n') { if ((int)llines[recno].blen < cnt) { llines[recno].blen = cnt + 256; if ((llines[recno].l = realloc(llines[recno].l, llines[recno].blen)) == NULL) err(1, "realloc"); } bcopy(sp, llines[recno].l, llines[recno].len = cnt); cnt = 0; p = sp; if (++recno == off) { wrap = 1; recno = 0; } } } if (ferror(fp)) { ierr(fn); rc = 1; goto done; } if (cnt) { llines[recno].l = sp; sp = NULL; llines[recno].len = cnt; if (++recno == off) { wrap = 1; recno = 0; } } if (rflag) { for (cnt = recno - 1; cnt >= 0; --cnt) WR(llines[cnt].l, llines[cnt].len); if (wrap) for (cnt = off - 1; cnt >= recno; --cnt) WR(llines[cnt].l, llines[cnt].len); } else { if (wrap) for (cnt = recno; cnt < off; ++cnt) WR(llines[cnt].l, llines[cnt].len); for (cnt = 0; cnt < recno; ++cnt) WR(llines[cnt].l, llines[cnt].len); } done: for (cnt = 0; cnt < off; cnt++) free(llines[cnt].l); free(sp); free(llines); return (rc); } Index: head/usr.bin/tail/reverse.c =================================================================== --- head/usr.bin/tail/reverse.c (revision 348841) +++ head/usr.bin/tail/reverse.c (revision 348842) @@ -1,286 +1,289 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Edward Sze-Tyan Wang. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if 0 #ifndef lint static char sccsid[] = "@(#)reverse.c 8.1 (Berkeley) 6/6/93"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include + #include "extern.h" static void r_buf(FILE *, const char *); static void r_reg(FILE *, const char *, enum STYLE, off_t, struct stat *); /* * reverse -- display input in reverse order by line. * * There are six separate cases for this -- regular and non-regular * files by bytes, lines or the whole file. * * BYTES display N bytes * REG mmap the file and display the lines * NOREG cyclically read characters into a wrap-around buffer * * LINES display N lines * REG mmap the file and display the lines * NOREG cyclically read lines into a wrap-around array of buffers * * FILE display the entire file * REG mmap the file and display the lines * NOREG cyclically read input into a linked list of buffers */ void reverse(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp) { if (style != REVERSE && off == 0) return; if (S_ISREG(sbp->st_mode)) r_reg(fp, fn, style, off, sbp); else switch(style) { case FBYTES: case RBYTES: bytes(fp, fn, off); break; case FLINES: case RLINES: lines(fp, fn, off); break; case REVERSE: r_buf(fp, fn); break; default: break; } } /* * r_reg -- display a regular file in reverse order by line. */ static void r_reg(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp) { struct mapinfo map; off_t curoff, size, lineend; int i; if (!(size = sbp->st_size)) return; map.start = NULL; map.mapoff = map.maxoff = size; map.fd = fileno(fp); map.maplen = 0; /* * Last char is special, ignore whether newline or not. Note that * size == 0 is dealt with above, and size == 1 sets curoff to -1. */ curoff = size - 2; lineend = size; while (curoff >= 0) { if (curoff < map.mapoff || curoff >= map.mapoff + (off_t)map.maplen) { if (maparound(&map, curoff) != 0) { ierr(fn); return; } } for (i = curoff - map.mapoff; i >= 0; i--) { if (style == RBYTES && --off == 0) break; if (map.start[i] == '\n') break; } /* `i' is either the map offset of a '\n', or -1. */ curoff = map.mapoff + i; if (i < 0) continue; /* Print the line and update offsets. */ if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) { ierr(fn); return; } lineend = curoff + 1; curoff--; if (style == RLINES) off--; if (off == 0 && style != REVERSE) { /* Avoid printing anything below. */ curoff = 0; break; } } if (curoff < 0 && mapprint(&map, 0, lineend) != 0) { ierr(fn); return; } if (map.start != NULL && munmap(map.start, map.maplen)) ierr(fn); } #define BSZ (128 * 1024) typedef struct bfelem { TAILQ_ENTRY(bfelem) entries; size_t len; char l[BSZ]; } bfelem_t; /* * r_buf -- display a non-regular file in reverse order by line. * * This is the function that saves the entire input, storing the data in a * doubly linked list of buffers and then displays them in reverse order. * It has the usual nastiness of trying to find the newlines, as there's no * guarantee that a newline occurs anywhere in the file, let alone in any * particular buffer. If we run out of memory, input is discarded (and the * user warned). */ static void r_buf(FILE *fp, const char *fn) { struct bfelem *tl, *first = NULL; size_t llen; char *p; off_t enomem = 0; TAILQ_HEAD(bfhead, bfelem) head; TAILQ_INIT(&head); while (!feof(fp)) { size_t len; /* * Allocate a new block and link it into place in a doubly * linked list. If out of memory, toss the LRU block and * keep going. */ while ((tl = malloc(sizeof(bfelem_t))) == NULL) { first = TAILQ_FIRST(&head); if (TAILQ_EMPTY(&head)) err(1, "malloc"); enomem += first->len; TAILQ_REMOVE(&head, first, entries); free(first); } TAILQ_INSERT_TAIL(&head, tl, entries); /* Fill the block with input data. */ len = 0; while ((!feof(fp)) && len < BSZ) { p = tl->l + len; len += fread(p, 1, BSZ - len, fp); if (ferror(fp)) { ierr(fn); return; } } tl->len = len; } if (enomem) { warnx("warning: %jd bytes discarded", (intmax_t)enomem); rval = 1; } /* * Now print the lines in reverse order * Outline: * Scan backward for "\n", * print forward to the end of the buffers * free any buffers that start after the "\n" just found * Loop */ tl = TAILQ_LAST(&head, bfhead); first = TAILQ_FIRST(&head); while (tl != NULL) { struct bfelem *temp; for (p = tl->l + tl->len - 1, llen = 0; p >= tl->l; --p, ++llen) { int start = (tl == first && p == tl->l); if ((*p == '\n') || start) { struct bfelem *tr; if (llen && start && *p != '\n') WR(p, llen + 1); else if (llen) { WR(p + 1, llen); if (start && *p == '\n') WR(p, 1); } tr = TAILQ_NEXT(tl, entries); llen = 0; if (tr != NULL) { TAILQ_FOREACH_FROM_SAFE(tr, &head, entries, temp) { if (tr->len) WR(&tr->l, tr->len); TAILQ_REMOVE(&head, tr, entries); free(tr); } } } } tl->len = llen; tl = TAILQ_PREV(tl, bfhead, entries); } TAILQ_REMOVE(&head, first, entries); free(first); } Index: head/usr.bin/tail/tail.c =================================================================== --- head/usr.bin/tail/tail.c (revision 348841) +++ head/usr.bin/tail/tail.c (revision 348842) @@ -1,370 +1,370 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Edward Sze-Tyan Wang. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1991, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif #ifndef lint static const char sccsid[] = "@(#)tail.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include "extern.h" int Fflag, fflag, qflag, rflag, rval, no_files; +fileargs_t *fa; static file_info_t *files; static void obsolete(char **); static void usage(void); static const struct option long_opts[] = { {"blocks", required_argument, NULL, 'b'}, {"bytes", required_argument, NULL, 'c'}, {"lines", required_argument, NULL, 'n'}, {NULL, no_argument, NULL, 0} }; int main(int argc, char *argv[]) { struct stat sb; const char *fn; FILE *fp; off_t off; enum STYLE style; int i, ch, first; file_info_t *file; char *p; - fileargs_t *fa; cap_rights_t rights; - cap_rights_init(&rights, CAP_FSTAT, CAP_FCNTL, CAP_MMAP_RW); + cap_rights_init(&rights, CAP_FSTAT, CAP_FSTATFS, CAP_FCNTL, CAP_MMAP_RW); if (caph_rights_limit(STDIN_FILENO, &rights) < 0 || caph_limit_stderr() < 0 || caph_limit_stdout() < 0) err(1, "can't limit stdio rights"); /* * Tail's options are weird. First, -n10 is the same as -n-10, not * -n+10. Second, the number options are 1 based and not offsets, * so -n+1 is the first line, and -c-1 is the last byte. Third, the * number options for the -r option specify the number of things that * get displayed, not the starting point in the file. The one major * incompatibility in this version as compared to historical versions * is that the 'r' option couldn't be modified by the -lbc options, * i.e. it was always done in lines. This version treats -rc as a * number of characters in reverse order. Finally, the default for * -r is the entire file, not 10 lines. */ #define ARG(units, forward, backward) { \ if (style) \ usage(); \ off = strtoll(optarg, &p, 10) * (units); \ if (*p) \ errx(1, "illegal offset -- %s", optarg); \ switch(optarg[0]) { \ case '+': \ if (off) \ off -= (units); \ style = (forward); \ break; \ case '-': \ off = -off; \ /* FALLTHROUGH */ \ default: \ style = (backward); \ break; \ } \ } obsolete(argv); style = NOTSET; off = 0; while ((ch = getopt_long(argc, argv, "+Fb:c:fn:qr", long_opts, NULL)) != -1) switch(ch) { case 'F': /* -F is superset of (and implies) -f */ Fflag = fflag = 1; break; case 'b': ARG(512, FBYTES, RBYTES); break; case 'c': ARG(1, FBYTES, RBYTES); break; case 'f': fflag = 1; break; case 'n': ARG(1, FLINES, RLINES); break; case 'q': qflag = 1; break; case 'r': rflag = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; no_files = argc ? argc : 1; fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN); if (fa == NULL) errx(1, "unable to init casper"); caph_cache_catpages(); if (caph_enter_casper() < 0) err(1, "unable to enter capability mode"); /* * If displaying in reverse, don't permit follow option, and convert * style values. */ if (rflag) { if (fflag) usage(); if (style == FBYTES) style = RBYTES; else if (style == FLINES) style = RLINES; } /* * If style not specified, the default is the whole file for -r, and * the last 10 lines if not -r. */ if (style == NOTSET) { if (rflag) { off = 0; style = REVERSE; } else { off = 10; style = RLINES; } } if (*argv && fflag) { files = (struct file_info *) malloc(no_files * sizeof(struct file_info)); if (!files) err(1, "Couldn't malloc space for file descriptors."); for (file = files; (fn = *argv++); file++) { file->file_name = strdup(fn); if (! file->file_name) errx(1, "Couldn't malloc space for file name."); file->fp = fileargs_fopen(fa, file->file_name, "r"); if (file->fp == NULL || fstat(fileno(file->fp), &file->st)) { if (file->fp != NULL) { fclose(file->fp); file->fp = NULL; } if (!Fflag || errno != ENOENT) ierr(file->file_name); } } follow(files, style, off); for (i = 0, file = files; i < no_files; i++, file++) { free(file->file_name); } free(files); } else if (*argv) { for (first = 1; (fn = *argv++);) { if ((fp = fileargs_fopen(fa, fn, "r")) == NULL || fstat(fileno(fp), &sb)) { ierr(fn); continue; } if (argc > 1 && !qflag) { printfn(fn, !first); first = 0; } if (rflag) reverse(fp, fn, style, off, &sb); else forward(fp, fn, style, off, &sb); } } else { fn = "stdin"; if (fstat(fileno(stdin), &sb)) { ierr(fn); exit(1); } /* * Determine if input is a pipe. 4.4BSD will set the SOCKET * bit in the st_mode field for pipes. Fix this then. */ if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { errno = 0; fflag = 0; /* POSIX.2 requires this. */ } if (rflag) reverse(stdin, fn, style, off, &sb); else forward(stdin, fn, style, off, &sb); } fileargs_free(fa); exit(rval); } /* * Convert the obsolete argument form into something that getopt can handle. * This means that anything of the form [+-][0-9][0-9]*[lbc][Ffr] that isn't * the option argument for a -b, -c or -n option gets converted. */ static void obsolete(char *argv[]) { char *ap, *p, *t; size_t len; char *start; while ((ap = *++argv)) { /* Return if "--" or not an option of any form. */ if (ap[0] != '-') { if (ap[0] != '+') return; } else if (ap[1] == '-') return; switch(*++ap) { /* Old-style option. */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* Malloc space for dash, new option and argument. */ len = strlen(*argv); if ((start = p = malloc(len + 3)) == NULL) err(1, "malloc"); *p++ = '-'; /* * Go to the end of the option argument. Save off any * trailing options (-3lf) and translate any trailing * output style characters. */ t = *argv + len - 1; if (*t == 'F' || *t == 'f' || *t == 'r') { *p++ = *t; *t-- = '\0'; } switch(*t) { case 'b': *p++ = 'b'; *t = '\0'; break; case 'c': *p++ = 'c'; *t = '\0'; break; case 'l': *t = '\0'; /* FALLTHROUGH */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': *p++ = 'n'; break; default: errx(1, "illegal option -- %s", *argv); } *p++ = *argv[0]; (void)strcpy(p, ap); *argv = start; continue; /* * Options w/ arguments, skip the argument and continue * with the next option. */ case 'b': case 'c': case 'n': if (!ap[1]) ++argv; /* FALLTHROUGH */ /* Options w/o arguments, continue with the next option. */ case 'F': case 'f': case 'r': continue; /* Illegal option, return and let getopt handle it. */ default: return; } } } static void usage(void) { (void)fprintf(stderr, "usage: tail [-F | -f | -r] [-q] [-b # | -c # | -n #]" " [file ...]\n"); exit(1); }