1 /* $NetBSD: head.c,v 1.23 2010/03/31 21:55:23 joerg Exp $ */
4 * Copyright (c) 1980, 1987, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: head.c,v 1.23 2010/03/31 21:55:23 joerg Exp $");
46 #include <sys/types.h>
60 * head - give the first few lines of a stream or of each of a set of files
62 * Bill Joy UCB August 24, 1977
65 static void head(FILE *, intmax_t, intmax_t);
66 static void obsolete(char *[]);
67 __dead
static void usage(void);
71 main(int argc
, char *argv
[])
83 (void)setlocale(LC_ALL
, "");
87 while ((ch
= getopt(argc
, argv
, "c:n:qv")) != -1)
91 bytecnt
= strtoimax(optarg
, &ep
, 10);
92 if ((bytecnt
== INTMAX_MAX
&& errno
== ERANGE
) ||
94 errx(1, "illegal byte count -- %s", optarg
);
99 linecnt
= strtoimax(optarg
, &ep
, 10);
100 if ((linecnt
== INTMAX_MAX
&& errno
== ERANGE
) ||
102 errx(1, "illegal line count -- %s", optarg
);
123 for (first
= 1; *argv
; ++argv
) {
124 if ((fp
= fopen(*argv
, "r")) == NULL
) {
129 if (vflag
|| (qflag
== 0 && argc
> 1)) {
130 (void)printf("%s==> %s <==\n",
131 first
? "" : "\n", *argv
);
134 head(fp
, linecnt
, bytecnt
);
138 head(stdin
, linecnt
, bytecnt
);
143 head(FILE *fp
, intmax_t cnt
, intmax_t bytecnt
)
152 if (bytecnt
> (intmax_t)sizeof(buf
))
156 rv
= fread(buf
, 1, len
, fp
);
158 break; /* Distinguish EOF and error? */
159 rv2
= fwrite(buf
, 1, rv
, stdout
);
162 errx(1, "EOF on stdout");
164 err(1, "failure writing to stdout");
169 while ((ch
= getc(fp
)) != EOF
) {
170 if (putchar(ch
) == EOF
)
172 if (ch
== '\n' && --cnt
== 0)
179 obsolete(char *argv
[])
183 while ((ap
= *++argv
)) {
184 /* Return if "--" or not "-[0-9]*". */
185 if (ap
[0] != '-' || ap
[1] == '-' ||
186 !isdigit((unsigned char)ap
[1]))
188 if ((ap
= malloc(strlen(*argv
) + 2)) == NULL
)
192 (void)strcpy(ap
+ 2, *argv
+ 1);
201 (void)fprintf(stderr
, "usage: %s [-n lines] [file ...]\n",