2 * Copyright (c) 1980, 1987, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
55 * head - give the first few lines of a stream or of each of a set of files
57 * Bill Joy UCB August 24, 1977
60 static void head(FILE *, int);
61 static void head_bytes(FILE *, off_t);
62 static void obsolete(char *[]);
63 static void usage(void);
66 main(int argc, char *argv[])
70 int first, linecnt = -1, eval = 0;
75 while ((ch = getopt(argc, argv, "n:c:")) != -1)
78 bytecnt = strtoimax(optarg, &ep, 10);
79 if (*ep || bytecnt <= 0)
80 errx(1, "illegal byte count -- %s", optarg);
83 linecnt = strtol(optarg, &ep, 10);
84 if (*ep || linecnt <= 0)
85 errx(1, "illegal line count -- %s", optarg);
94 if (linecnt != -1 && bytecnt != -1)
95 errx(1, "can't combine line and byte counts");
99 for (first = 1; *argv; ++argv) {
100 if ((fp = fopen(*argv, "r")) == NULL) {
106 (void)printf("%s==> %s <==\n",
107 first ? "" : "\n", *argv);
113 head_bytes(fp, bytecnt);
116 } else if (bytecnt == -1)
117 head(stdin, linecnt);
119 head_bytes(stdin, bytecnt);
125 head(FILE *fp, int cnt)
128 size_t error, readlen;
130 while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
131 error = fwrite(cp, sizeof(char), readlen, stdout);
132 if (error != readlen)
139 head_bytes(FILE *fp, off_t cnt)
145 if ((uintmax_t)cnt < sizeof(buf))
148 readlen = sizeof(buf);
149 readlen = fread(buf, sizeof(char), readlen, fp);
152 if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
159 obsolete(char *argv[])
163 while ((ap = *++argv)) {
164 /* Return if "--" or not "-[0-9]*". */
165 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
167 if ((ap = malloc(strlen(*argv) + 2)) == NULL)
171 (void)strcpy(ap + 2, *argv + 1);
180 (void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");