summaryrefslogtreecommitdiff
path: root/usr.bin/tail
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/tail')
-rw-r--r--usr.bin/tail/Makefile4
-rw-r--r--usr.bin/tail/extern.h8
-rw-r--r--usr.bin/tail/forward.c46
-rw-r--r--usr.bin/tail/misc.c70
-rw-r--r--usr.bin/tail/reverse.c32
-rw-r--r--usr.bin/tail/tail.c7
6 files changed, 117 insertions, 50 deletions
diff --git a/usr.bin/tail/Makefile b/usr.bin/tail/Makefile
index 6b94b2c250e..5001de0616c 100644
--- a/usr.bin/tail/Makefile
+++ b/usr.bin/tail/Makefile
@@ -1,6 +1,6 @@
-# $OpenBSD: Makefile,v 1.4 1997/09/21 11:51:04 deraadt Exp $
+# $OpenBSD: Makefile,v 1.5 1999/02/03 02:09:30 millert Exp $
PROG= tail
-SRCS= forward.c read.c reverse.c tail.c
+SRCS= forward.c misc.c read.c reverse.c tail.c
.include <bsd.prog.mk>
diff --git a/usr.bin/tail/extern.h b/usr.bin/tail/extern.h
index 3bcd1ad5e13..a87460ac699 100644
--- a/usr.bin/tail/extern.h
+++ b/usr.bin/tail/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.3 1997/01/12 23:43:04 millert Exp $ */
+/* $OpenBSD: extern.h,v 1.4 1999/02/03 02:09:30 millert Exp $ */
/* $NetBSD: extern.h,v 1.3 1994/11/23 07:42:00 jtc Exp $ */
/*-
@@ -36,9 +36,6 @@
* @(#)extern.h 8.1 (Berkeley) 6/6/93
*/
-#define ierr() (err(0, fname))
-#define oerr() (err(1, "stdout"))
-
#define WR(p, size) \
if (write(STDOUT_FILENO, p, size) != size) \
oerr();
@@ -51,5 +48,8 @@ void reverse __P((FILE *, enum STYLE, long, struct stat *));
void bytes __P((FILE *, off_t));
void lines __P((FILE *, off_t));
+void ierr __P((void));
+void oerr __P((void));
+
extern int fflag, rflag, rval;
extern char *fname;
diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c
index ae48d56791c..f77edd00a6f 100644
--- a/usr.bin/tail/forward.c
+++ b/usr.bin/tail/forward.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: forward.c,v 1.6 1997/05/30 19:33:44 millert Exp $ */
+/* $OpenBSD: forward.c,v 1.7 1999/02/03 02:09:30 millert Exp $ */
/* $NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson Exp $ */
/*-
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
#endif
-static char rcsid[] = "$OpenBSD: forward.c,v 1.6 1997/05/30 19:33:44 millert Exp $";
+static char rcsid[] = "$OpenBSD: forward.c,v 1.7 1999/02/03 02:09:30 millert Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -59,7 +59,7 @@ static char rcsid[] = "$OpenBSD: forward.c,v 1.6 1997/05/30 19:33:44 millert Exp
#include "extern.h"
-static void rlines __P((FILE *, long, struct stat *));
+static int rlines __P((FILE *, long, struct stat *));
/*
* forward -- display the file, from an offset, forward.
@@ -136,7 +136,8 @@ forward(fp, style, off, sbp)
return;
}
} else if (off == 0) {
- while (getc(fp) != EOF);
+ while (getc(fp) != EOF)
+ ;
if (ferror(fp)) {
ierr();
return;
@@ -145,16 +146,17 @@ forward(fp, style, off, sbp)
bytes(fp, off);
break;
case RLINES:
- if (S_ISREG(sbp->st_mode))
+ if (S_ISREG(sbp->st_mode)) {
if (!off) {
if (fseek(fp, 0L, SEEK_END) == -1) {
ierr();
return;
}
- } else
- rlines(fp, off, sbp);
- else if (off == 0) {
- while (getc(fp) != EOF);
+ } else if (rlines(fp, off, sbp) != 0)
+ lines(fp, off);
+ } else if (off == 0) {
+ while (getc(fp) != EOF)
+ ;
if (ferror(fp)) {
ierr();
return;
@@ -199,7 +201,7 @@ forward(fp, style, off, sbp)
/*
* rlines -- display the last offset lines of the file.
*/
-static void
+static int
rlines(fp, off, sbp)
FILE *fp;
long off;
@@ -210,18 +212,14 @@ rlines(fp, off, sbp)
char *start;
if (!(size = sbp->st_size))
- return;
+ return (0);
- if (size > SIZE_T_MAX) {
- errx(0, "%s: %s", fname, strerror(EFBIG));
- return;
- }
+ if (size > SIZE_T_MAX)
+ return (1);
- if ((start = mmap(NULL, (size_t)size,
- PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
- errx(0, "%s: %s", fname, strerror(EFBIG));
- return;
- }
+ if ((start = mmap(NULL, (size_t)size, PROT_READ, 0, fileno(fp),
+ (off_t)0)) == (caddr_t)-1)
+ return (1);
/* Last char is special, ignore whether newline or not. */
for (p = start + size - 1; --size;)
@@ -235,10 +233,12 @@ rlines(fp, off, sbp)
WR(p, size);
if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
ierr();
- return;
+ return (1);
}
if (munmap(start, (size_t)sbp->st_size)) {
- err(0, fname);
- return;
+ ierr();
+ return (1);
}
+
+ return (0);
}
diff --git a/usr.bin/tail/misc.c b/usr.bin/tail/misc.c
new file mode 100644
index 00000000000..f997a0da727
--- /dev/null
+++ b/usr.bin/tail/misc.c
@@ -0,0 +1,70 @@
+/* $OpenBSD: misc.c,v 1.4 1999/02/03 02:09:30 millert Exp $ */
+
+/*-
+ * 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. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. 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.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
+#else
+static char rcsid[] = "$OpenBSD: misc.c,v 1.4 1999/02/03 02:09:30 millert Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "extern.h"
+
+void
+ierr()
+{
+
+ warn("%s", fname);
+ rval = 1;
+}
+
+void
+oerr()
+{
+
+ err(1, "stdout");
+}
diff --git a/usr.bin/tail/reverse.c b/usr.bin/tail/reverse.c
index 9711616978b..13dd65795fc 100644
--- a/usr.bin/tail/reverse.c
+++ b/usr.bin/tail/reverse.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: reverse.c,v 1.3 1997/01/12 23:43:06 millert Exp $ */
+/* $OpenBSD: reverse.c,v 1.4 1999/02/03 02:09:30 millert Exp $ */
/* $NetBSD: reverse.c,v 1.6 1994/11/23 07:42:10 jtc Exp $ */
/*-
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)reverse.c 8.1 (Berkeley) 6/6/93";
#endif
-static char rcsid[] = "$OpenBSD: reverse.c,v 1.3 1997/01/12 23:43:06 millert Exp $";
+static char rcsid[] = "$OpenBSD: reverse.c,v 1.4 1999/02/03 02:09:30 millert Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -59,7 +59,7 @@ static char rcsid[] = "$OpenBSD: reverse.c,v 1.3 1997/01/12 23:43:06 millert Exp
#include "extern.h"
static void r_buf __P((FILE *));
-static void r_reg __P((FILE *, enum STYLE, long, struct stat *));
+static int r_reg __P((FILE *, enum STYLE, long, struct stat *));
/*
* reverse -- display input in reverse order by line.
@@ -89,9 +89,7 @@ reverse(fp, style, off, sbp)
if (style != REVERSE && off == 0)
return;
- if (S_ISREG(sbp->st_mode))
- r_reg(fp, style, off, sbp);
- else
+ if (!S_ISREG(sbp->st_mode) || r_reg(fp, style, off, sbp) != 0)
switch(style) {
case FBYTES:
case RBYTES:
@@ -110,7 +108,7 @@ reverse(fp, style, off, sbp)
/*
* r_reg -- display a regular file in reverse order by line.
*/
-static void
+static int
r_reg(fp, style, off, sbp)
FILE *fp;
register enum STYLE style;
@@ -123,18 +121,14 @@ r_reg(fp, style, off, sbp)
char *start;
if (!(size = sbp->st_size))
- return;
+ return (0);
- if (size > SIZE_T_MAX) {
- errx(0, "%s: %s", fname, strerror(EFBIG));
- return;
- }
+ if (size > SIZE_T_MAX)
+ return (1);
- if ((start = mmap(NULL, (size_t)size,
- PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
- errx(0, "%s: %s", fname, strerror(EFBIG));
- return;
- }
+ if ((start = mmap(NULL, (size_t)size, PROT_READ, 0, fileno(fp),
+ (off_t)0)) == (caddr_t)-1)
+ return (1);
p = start + size - 1;
if (style == RBYTES && off < size)
@@ -153,7 +147,9 @@ r_reg(fp, style, off, sbp)
if (llen)
WR(p, llen);
if (munmap(start, (size_t)sbp->st_size))
- err(0, fname);
+ ierr();
+
+ return (0);
}
typedef struct bf {
diff --git a/usr.bin/tail/tail.c b/usr.bin/tail/tail.c
index 70aa03547b7..8aae71e370c 100644
--- a/usr.bin/tail/tail.c
+++ b/usr.bin/tail/tail.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tail.c,v 1.4 1997/01/15 23:43:18 millert Exp $ */
+/* $OpenBSD: tail.c,v 1.5 1999/02/03 02:09:30 millert Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -46,7 +46,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)tail.c 8.1 (Berkeley) 6/6/93";
#endif
-static char rcsid[] = "$OpenBSD: tail.c,v 1.4 1997/01/15 23:43:18 millert Exp $";
+static char rcsid[] = "$OpenBSD: tail.c,v 1.5 1999/02/03 02:09:30 millert Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -158,7 +158,7 @@ main(argc, argv)
* If style not specified, the default is the whole file for -r, and
* the last 10 lines if not -r.
*/
- if (style == NOTSET)
+ if (style == NOTSET) {
if (rflag) {
off = 0;
style = REVERSE;
@@ -166,6 +166,7 @@ main(argc, argv)
off = 10;
style = RLINES;
}
+ }
if (*argv)
for (first = 1; (fname = *argv++);) {