diff --git a/usr.bin/head/Makefile b/usr.bin/head/Makefile index 44da853de640..0bab5688c5ea 100644 --- a/usr.bin/head/Makefile +++ b/usr.bin/head/Makefile @@ -1,5 +1,5 @@ -# $NetBSD: Makefile,v 1.3 1997/01/09 20:19:48 tls Exp $ -# from: @(#)Makefile 5.3 (Berkeley) 5/11/90 +# $NetBSD: Makefile,v 1.4 1997/10/18 13:15:18 mrg Exp $ +# from: @(#)Makefile 8.1 (Berkeley) 6/6/93 PROG= head diff --git a/usr.bin/head/head.1 b/usr.bin/head/head.1 index 3b7d015ecc53..a9fe9ce59926 100644 --- a/usr.bin/head/head.1 +++ b/usr.bin/head/head.1 @@ -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. -.\" All rights reserved. +.\" Copyright (c) 1980, 1990, 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 @@ -31,27 +31,37 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)head.1 6.6 (Berkeley) 7/24/91 -.\" $NetBSD: head.1,v 1.5 1997/01/09 20:19:49 tls Exp $ +.\" from: @(#)head.1 8.1 (Berkeley) 6/6/93 .\" .Dd July 14, 1993 .Dt HEAD 1 .Os .Sh NAME .Nm head -.Nd give first few lines +.Nd display first lines of a file .Sh SYNOPSIS .Nm head .Op Fl n Ar count -.Op Ar +.Op Ar file ... .Sh DESCRIPTION -This filter copies the first +This filter displays the first .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 .Ar count -is omitted it defaults to -10. +is omitted it defaults to 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 The historic command line syntax of .Nm head diff --git a/usr.bin/head/head.c b/usr.bin/head/head.c index b5b0ca4990aa..aa3d1b4d4ba9 100644 --- a/usr.bin/head/head.c +++ b/usr.bin/head/head.c @@ -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. - * All rights reserved. + * Copyright (c) 1980, 1987, 1992, 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 @@ -33,89 +33,127 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)head.c 5.5 (Berkeley) 6/1/90";*/ -static char rcsid[] = "$NetBSD: head.c,v 1.6 1997/01/09 20:19:50 tls Exp $"; +#if 0 +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 */ +#include + +#include +#include #include #include -#include +#include #include -static void usage (); - /* * head - give the first few lines of a stream or of each of a set of files * * 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 main(argc, argv) - int argc; - char **argv; + int argc; + char *argv[]; { - register int ch, cnt; - int firsttime, linecnt = 10; + register int ch; + FILE *fp; + int first, linecnt; + char *ep; - /* handle obsolete -number syntax */ - if (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) { - if ((linecnt = atoi(argv[1] + 1)) < 0) { - usage (); - } - argc--; argv++; - } - - while ((ch = getopt (argc, argv, "n:")) != EOF) - switch (ch) { + obsolete(argv); + linecnt = 10; + while ((ch = getopt(argc, argv, "n:")) != EOF) + switch(ch) { case 'n': - if ((linecnt = atoi(optarg)) < 0) - usage (); + linecnt = strtol(optarg, &ep, 10); + if (*ep || linecnt <= 0) + err(1, "illegal line count -- %s", optarg); break; + case '?': default: - usage(); + usage(); } - argc -= optind, argv += optind; + argc -= optind; + argv += optind; - /* setlinebuf(stdout); */ - for (firsttime = 1; ; firsttime = 0) { - if (!*argv) { - if (!firsttime) - exit(0); - } - else { - if (!freopen(*argv, "r", stdin)) { - fprintf(stderr, "head: can't read %s.\n", *argv); - exit(1); + if (*argv) + for (first = 1; *argv; ++argv) { + if ((fp = fopen(*argv, "r")) == NULL) { + err(0, "%s: %s", *argv, strerror(errno)); + continue; } if (argc > 1) { - if (!firsttime) - putchar('\n'); - printf("==> %s <==\n", *argv); + (void)printf("%s==> %s <==\n", + first ? "" : "\n", *argv); + first = 0; } - ++argv; + head(fp, linecnt); + (void)fclose(fp); } - for (cnt = linecnt; cnt; --cnt) - while ((ch = getchar()) != EOF) - if (putchar(ch) == '\n') - break; - } - /*NOTREACHED*/ + else + head(stdin, linecnt); + exit(eval); } - -static void -usage () +void +head(fp, cnt) + FILE *fp; + register int cnt; { - fputs("usage: head [-n line_count] [file ...]\n", stderr); + register int ch; + + while (cnt--) + while ((ch = getc(fp)) != EOF) { + if (putchar(ch) == EOF) + err(1, "stdout: %s", strerror(errno)); + if (ch == '\n') + break; + } +} + +void +obsolete(argv) + char *argv[]; +{ + 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); } -