2010-04-01 01:55:23 +04:00
|
|
|
/* $NetBSD: head.c,v 1.23 2010/03/31 21:55:23 joerg Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-10-18 17:15:18 +04:00
|
|
|
* Copyright (c) 1980, 1987, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 17:15:18 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-18 17:15:18 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
|
|
|
|
#else
|
2010-04-01 01:55:23 +04:00
|
|
|
__RCSID("$NetBSD: head.c,v 1.23 2010/03/31 21:55:23 joerg Exp $");
|
1997-10-18 17:15:18 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-10-18 17:15:18 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-10-19 06:23:41 +04:00
|
|
|
#include <err.h>
|
1997-10-18 17:15:18 +04:00
|
|
|
#include <errno.h>
|
2010-04-01 01:52:11 +04:00
|
|
|
#include <inttypes.h>
|
1998-01-27 20:00:19 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1993-07-15 21:05:00 +04:00
|
|
|
#include <stdlib.h>
|
1997-10-18 17:15:18 +04:00
|
|
|
#include <string.h>
|
1993-10-13 21:33:45 +03:00
|
|
|
#include <unistd.h>
|
1993-07-15 21:05:00 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* head - give the first few lines of a stream or of each of a set of files
|
|
|
|
*
|
|
|
|
* Bill Joy UCB August 24, 1977
|
|
|
|
*/
|
|
|
|
|
2010-04-01 01:52:11 +04:00
|
|
|
static void head(FILE *, intmax_t, intmax_t);
|
2010-03-31 19:01:02 +04:00
|
|
|
static void obsolete(char *[]);
|
|
|
|
__dead static void usage(void);
|
1997-10-18 17:15:18 +04:00
|
|
|
|
|
|
|
|
1993-08-28 02:30:10 +04:00
|
|
|
int
|
2010-03-31 19:01:02 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 06:23:41 +04:00
|
|
|
int ch;
|
1997-10-18 17:15:18 +04:00
|
|
|
FILE *fp;
|
1998-01-27 20:00:19 +03:00
|
|
|
int first;
|
2010-04-01 01:52:11 +04:00
|
|
|
uintmax_t linecnt;
|
|
|
|
uintmax_t bytecnt;
|
1997-10-18 17:15:18 +04:00
|
|
|
char *ep;
|
2010-03-31 19:01:51 +04:00
|
|
|
int eval = 0;
|
2004-05-04 14:57:42 +04:00
|
|
|
int qflag = 0;
|
|
|
|
int vflag = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-01-27 20:00:19 +03:00
|
|
|
(void)setlocale(LC_ALL, "");
|
1997-10-18 17:15:18 +04:00
|
|
|
obsolete(argv);
|
|
|
|
linecnt = 10;
|
2004-05-04 14:57:42 +04:00
|
|
|
bytecnt = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "c:n:qv")) != -1)
|
1997-10-18 17:15:18 +04:00
|
|
|
switch(ch) {
|
2004-05-04 14:57:42 +04:00
|
|
|
case 'c':
|
|
|
|
errno = 0;
|
2010-04-01 01:52:11 +04:00
|
|
|
bytecnt = strtoimax(optarg, &ep, 10);
|
|
|
|
if ((bytecnt == INTMAX_MAX && errno == ERANGE) ||
|
|
|
|
*ep || bytecnt <= 0)
|
2010-04-01 01:55:23 +04:00
|
|
|
errx(1, "illegal byte count -- %s", optarg);
|
2004-05-04 14:57:42 +04:00
|
|
|
break;
|
|
|
|
|
1993-07-15 21:05:00 +04:00
|
|
|
case 'n':
|
2003-04-18 07:21:00 +04:00
|
|
|
errno = 0;
|
2010-04-01 01:52:11 +04:00
|
|
|
linecnt = strtoimax(optarg, &ep, 10);
|
|
|
|
if ((linecnt == INTMAX_MAX && errno == ERANGE) ||
|
|
|
|
*ep || linecnt <= 0)
|
2010-04-01 01:55:23 +04:00
|
|
|
errx(1, "illegal line count -- %s", optarg);
|
1993-07-15 21:05:00 +04:00
|
|
|
break;
|
|
|
|
|
2004-05-04 14:57:42 +04:00
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
vflag = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
qflag = 0;
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-10-18 17:15:18 +04:00
|
|
|
case '?':
|
1993-07-15 21:05:00 +04:00
|
|
|
default:
|
1997-10-18 17:15:18 +04:00
|
|
|
usage();
|
1993-07-15 21:05:00 +04:00
|
|
|
}
|
1997-10-18 17:15:18 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-07-15 21:05:00 +04:00
|
|
|
|
1997-10-18 17:15:18 +04:00
|
|
|
if (*argv)
|
|
|
|
for (first = 1; *argv; ++argv) {
|
|
|
|
if ((fp = fopen(*argv, "r")) == NULL) {
|
1998-01-27 20:00:19 +03:00
|
|
|
warn("%s", *argv);
|
|
|
|
eval = 1;
|
1997-10-18 17:15:18 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-05-04 14:57:42 +04:00
|
|
|
if (vflag || (qflag == 0 && argc > 1)) {
|
1997-10-18 17:15:18 +04:00
|
|
|
(void)printf("%s==> %s <==\n",
|
|
|
|
first ? "" : "\n", *argv);
|
|
|
|
first = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-05-04 14:57:42 +04:00
|
|
|
head(fp, linecnt, bytecnt);
|
1997-10-18 17:15:18 +04:00
|
|
|
(void)fclose(fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-18 17:15:18 +04:00
|
|
|
else
|
2004-05-04 14:57:42 +04:00
|
|
|
head(stdin, linecnt, bytecnt);
|
1997-10-18 17:15:18 +04:00
|
|
|
exit(eval);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1993-07-15 21:05:00 +04:00
|
|
|
|
2010-03-31 19:01:02 +04:00
|
|
|
static void
|
2010-04-01 01:52:11 +04:00
|
|
|
head(FILE *fp, intmax_t cnt, intmax_t bytecnt)
|
1997-10-18 17:15:18 +04:00
|
|
|
{
|
2010-04-01 01:52:11 +04:00
|
|
|
char buf[65536];
|
|
|
|
size_t len, rv, rv2;
|
1997-10-19 06:23:41 +04:00
|
|
|
int ch;
|
1993-07-15 21:05:00 +04:00
|
|
|
|
2010-04-01 01:52:11 +04:00
|
|
|
if (bytecnt) {
|
|
|
|
while (bytecnt) {
|
|
|
|
len = sizeof(buf);
|
|
|
|
if (bytecnt > (intmax_t)sizeof(buf))
|
|
|
|
len = sizeof(buf);
|
|
|
|
else
|
|
|
|
len = bytecnt;
|
|
|
|
rv = fread(buf, 1, len, fp);
|
|
|
|
if (rv == 0)
|
|
|
|
break; /* Distinguish EOF and error? */
|
|
|
|
rv2 = fwrite(buf, 1, rv, stdout);
|
|
|
|
if (rv2 != rv) {
|
|
|
|
if (feof(stdout))
|
|
|
|
errx(1, "EOF on stdout");
|
|
|
|
else
|
|
|
|
err(1, "failure writing to stdout");
|
|
|
|
}
|
|
|
|
bytecnt -= rv;
|
|
|
|
}
|
|
|
|
} else {
|
1997-10-18 17:15:18 +04:00
|
|
|
while ((ch = getc(fp)) != EOF) {
|
|
|
|
if (putchar(ch) == EOF)
|
1998-01-27 20:00:19 +03:00
|
|
|
err(1, "stdout");
|
2010-04-01 01:52:11 +04:00
|
|
|
if (ch == '\n' && --cnt == 0)
|
1997-10-18 17:15:18 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-04-01 01:52:11 +04:00
|
|
|
}
|
1997-10-18 17:15:18 +04:00
|
|
|
}
|
|
|
|
|
2010-03-31 19:01:02 +04:00
|
|
|
static void
|
|
|
|
obsolete(char *argv[])
|
1993-07-15 21:05:00 +04:00
|
|
|
{
|
1997-10-18 17:15:18 +04:00
|
|
|
char *ap;
|
|
|
|
|
|
|
|
while ((ap = *++argv)) {
|
|
|
|
/* Return if "--" or not "-[0-9]*". */
|
1998-12-19 19:37:28 +03:00
|
|
|
if (ap[0] != '-' || ap[1] == '-' ||
|
|
|
|
!isdigit((unsigned char)ap[1]))
|
1997-10-18 17:15:18 +04:00
|
|
|
return;
|
|
|
|
if ((ap = malloc(strlen(*argv) + 2)) == NULL)
|
1999-11-09 18:06:30 +03:00
|
|
|
err(1, NULL);
|
1997-10-18 17:15:18 +04:00
|
|
|
ap[0] = '-';
|
|
|
|
ap[1] = 'n';
|
|
|
|
(void)strcpy(ap + 2, *argv + 1);
|
|
|
|
*argv = ap;
|
|
|
|
}
|
1993-07-15 21:05:00 +04:00
|
|
|
}
|
|
|
|
|
2010-03-31 19:01:02 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
1997-10-18 17:15:18 +04:00
|
|
|
{
|
2001-02-20 02:03:42 +03:00
|
|
|
|
2004-01-06 02:23:32 +03:00
|
|
|
(void)fprintf(stderr, "usage: %s [-n lines] [file ...]\n",
|
2001-02-20 02:03:42 +03:00
|
|
|
getprogname());
|
1997-10-18 17:15:18 +04:00
|
|
|
exit(1);
|
|
|
|
}
|