merge lite-2; also WARNSify the 2 warnings left.

This commit is contained in:
mrg 1997-10-18 13:15:18 +00:00
parent 227b039655
commit 3ac0787450
3 changed files with 115 additions and 67 deletions

View File

@ -1,5 +1,5 @@
# $NetBSD: Makefile,v 1.3 1997/01/09 20:19:48 tls Exp $ # $NetBSD: Makefile,v 1.4 1997/10/18 13:15:18 mrg Exp $
# from: @(#)Makefile 5.3 (Berkeley) 5/11/90 # from: @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= head PROG= head

View File

@ -1,7 +1,7 @@
.\" $NetBSD: head.1,v 1.5 1997/01/09 20:19:49 tls Exp $ .\" $NetBSD: head.1,v 1.6 1997/10/18 13:15:30 mrg Exp $
.\" .\"
.\" Copyright (c) 1980, 1990 The Regents of the University of California. .\" Copyright (c) 1980, 1990, 1993
.\" All rights reserved. .\" The Regents of the University of California. All rights reserved.
.\" .\"
.\" Redistribution and use in source and binary forms, with or without .\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions .\" modification, are permitted provided that the following conditions
@ -31,27 +31,37 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" from: @(#)head.1 6.6 (Berkeley) 7/24/91 .\" from: @(#)head.1 8.1 (Berkeley) 6/6/93
.\" $NetBSD: head.1,v 1.5 1997/01/09 20:19:49 tls Exp $
.\" .\"
.Dd July 14, 1993 .Dd July 14, 1993
.Dt HEAD 1 .Dt HEAD 1
.Os .Os
.Sh NAME .Sh NAME
.Nm head .Nm head
.Nd give first few lines .Nd display first lines of a file
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm head .Nm head
.Op Fl n Ar count .Op Fl n Ar count
.Op Ar .Op Ar file ...
.Sh DESCRIPTION .Sh DESCRIPTION
This filter copies the first This filter displays the first
.Ar count .Ar count
lines of each of the specified files, or of the standard input. lines of each of the specified files, or of the standard input if no
files are specified.
If If
.Ar count .Ar count
is omitted it defaults to is omitted it defaults to 10.
10. .Pp
If more than a single file is specified, each file is preceded by a
header consisting of the string
.Dq ==> XXX <==
where
.Dq XXX
is the name of the file.
.Pp
The
.Nm head
utility exits 0 on success, and >0 if an error occurs.
.Sh COMPATIBILITY .Sh COMPATIBILITY
The historic command line syntax of The historic command line syntax of
.Nm head .Nm head

View File

@ -1,8 +1,8 @@
/* $NetBSD: head.c,v 1.6 1997/01/09 20:19:50 tls Exp $ */ /* $NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $ */
/* /*
* Copyright (c) 1980, 1987 Regents of the University of California. * Copyright (c) 1980, 1987, 1992, 1993
* All rights reserved. * The Regents of the University of California. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -33,89 +33,127 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <sys/cdefs.h>
#ifndef lint #ifndef lint
char copyright[] = __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\ The Regents of the University of California. All rights reserved.\n");
All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
/*static char sccsid[] = "from: @(#)head.c 5.5 (Berkeley) 6/1/90";*/ #if 0
static char rcsid[] = "$NetBSD: head.c,v 1.6 1997/01/09 20:19:50 tls Exp $"; static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $");
#endif
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static void usage ();
/* /*
* head - give the first few lines of a stream or of each of a set of files * head - give the first few lines of a stream or of each of a set of files
* *
* Bill Joy UCB August 24, 1977 * Bill Joy UCB August 24, 1977
*/ */
void err __P((int, const char *, ...));
void head __P((FILE *, int));
void obsolete __P((char *[]));
void usage __P((void));
int main __P((int, char *[]));
int eval;
int int
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char *argv[];
{ {
register int ch, cnt; register int ch;
int firsttime, linecnt = 10; FILE *fp;
int first, linecnt;
char *ep;
/* handle obsolete -number syntax */ obsolete(argv);
if (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) { linecnt = 10;
if ((linecnt = atoi(argv[1] + 1)) < 0) { while ((ch = getopt(argc, argv, "n:")) != EOF)
usage (); switch(ch) {
}
argc--; argv++;
}
while ((ch = getopt (argc, argv, "n:")) != EOF)
switch (ch) {
case 'n': case 'n':
if ((linecnt = atoi(optarg)) < 0) linecnt = strtol(optarg, &ep, 10);
usage (); if (*ep || linecnt <= 0)
err(1, "illegal line count -- %s", optarg);
break; break;
case '?':
default: default:
usage(); usage();
} }
argc -= optind, argv += optind; argc -= optind;
argv += optind;
/* setlinebuf(stdout); */ if (*argv)
for (firsttime = 1; ; firsttime = 0) { for (first = 1; *argv; ++argv) {
if (!*argv) { if ((fp = fopen(*argv, "r")) == NULL) {
if (!firsttime) err(0, "%s: %s", *argv, strerror(errno));
exit(0); continue;
}
else {
if (!freopen(*argv, "r", stdin)) {
fprintf(stderr, "head: can't read %s.\n", *argv);
exit(1);
} }
if (argc > 1) { if (argc > 1) {
if (!firsttime) (void)printf("%s==> %s <==\n",
putchar('\n'); first ? "" : "\n", *argv);
printf("==> %s <==\n", *argv); first = 0;
} }
++argv; head(fp, linecnt);
(void)fclose(fp);
} }
for (cnt = linecnt; cnt; --cnt) else
while ((ch = getchar()) != EOF) head(stdin, linecnt);
if (putchar(ch) == '\n') exit(eval);
}
void
head(fp, cnt)
FILE *fp;
register int cnt;
{
register int ch;
while (cnt--)
while ((ch = getc(fp)) != EOF) {
if (putchar(ch) == EOF)
err(1, "stdout: %s", strerror(errno));
if (ch == '\n')
break; break;
} }
/*NOTREACHED*/
} }
void
static void obsolete(argv)
usage () char *argv[];
{ {
fputs("usage: head [-n line_count] [file ...]\n", stderr); char *ap;
while ((ap = *++argv)) {
/* Return if "--" or not "-[0-9]*". */
if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
return;
if ((ap = malloc(strlen(*argv) + 2)) == NULL)
err(1, "%s", strerror(errno));
ap[0] = '-';
ap[1] = 'n';
(void)strcpy(ap + 2, *argv + 1);
*argv = ap;
}
}
void
usage()
{
(void)fputs("usage: head [-n lines] [file ...]\n", stderr);
exit(1); exit(1);
} }