head.c revision 85859
155714Skris/*
255714Skris * Copyright (c) 1980, 1987, 1992, 1993
355714Skris *	The Regents of the University of California.  All rights reserved.
455714Skris *
555714Skris * Redistribution and use in source and binary forms, with or without
655714Skris * modification, are permitted provided that the following conditions
755714Skris * are met:
855714Skris * 1. Redistributions of source code must retain the above copyright
955714Skris *    notice, this list of conditions and the following disclaimer.
1055714Skris * 2. Redistributions in binary form must reproduce the above copyright
1155714Skris *    notice, this list of conditions and the following disclaimer in the
1255714Skris *    documentation and/or other materials provided with the distribution.
1355714Skris * 3. All advertising materials mentioning features or use of this software
1455714Skris *    must display the following acknowledgement:
1555714Skris *	This product includes software developed by the University of
1655714Skris *	California, Berkeley and its contributors.
1755714Skris * 4. Neither the name of the University nor the names of its contributors
1855714Skris *    may be used to endorse or promote products derived from this software
1955714Skris *    without specific prior written permission.
2055714Skris *
2155714Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155714Skris * SUCH DAMAGE.
3255714Skris */
3355714Skris
3455714Skris#ifndef lint
3555714Skrisstatic const char copyright[] =
3655714Skris"@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
3755714Skris	The Regents of the University of California.  All rights reserved.\n";
3855714Skris#endif /* not lint */
3955714Skris
4055714Skris#ifndef lint
4155714Skris#if 0
4255714Skrisstatic char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
4355714Skris#endif
4455714Skrisstatic const char rcsid[] =
4555714Skris  "$FreeBSD: head/usr.bin/head/head.c 85859 2001-11-02 09:21:11Z alfred $";
4655714Skris#endif /* not lint */
4755714Skris
4855714Skris#include <sys/types.h>
4955714Skris
5055714Skris#include <ctype.h>
5155714Skris#include <err.h>
5255714Skris#include <stdio.h>
5355714Skris#include <stdlib.h>
5455714Skris#include <string.h>
55#include <unistd.h>
56
57/*
58 * head - give the first few lines of a stream or of each of a set of files
59 *
60 * Bill Joy UCB August 24, 1977
61 */
62
63void head __P((FILE *, int));
64void head_bytes __P((FILE *, int));
65void obsolete __P((char *[]));
66void usage __P((void));
67
68int
69main(argc, argv)
70	int argc;
71	char *argv[];
72{
73	register int ch;
74	FILE *fp;
75	int first, linecnt = -1, bytecnt = -1, eval = 0;
76	char *ep;
77
78	obsolete(argv);
79	while ((ch = getopt(argc, argv, "n:c:")) != -1)
80		switch(ch) {
81		case 'c':
82			bytecnt = strtol(optarg, &ep, 10);
83			if (*ep || bytecnt <= 0)
84				errx(1, "illegal byte count -- %s", optarg);
85			break;
86		case 'n':
87			linecnt = strtol(optarg, &ep, 10);
88			if (*ep || linecnt <= 0)
89				errx(1, "illegal line count -- %s", optarg);
90			break;
91		case '?':
92		default:
93			usage();
94		}
95	argc -= optind;
96	argv += optind;
97
98	if (linecnt != -1 && bytecnt != -1)
99		errx(1, "can't combine line and byte counts");
100	if (linecnt == -1 )
101		linecnt = 10;
102	if (*argv) {
103		for (first = 1; *argv; ++argv) {
104			if ((fp = fopen(*argv, "r")) == NULL) {
105				warn("%s", *argv);
106				eval = 1;
107				continue;
108			}
109			if (argc > 1) {
110				(void)printf("%s==> %s <==\n",
111				    first ? "" : "\n", *argv);
112				first = 0;
113			}
114			if (bytecnt == -1)
115				head(fp, linecnt);
116			else
117				head_bytes(fp, bytecnt);
118			(void)fclose(fp);
119		}
120	} else if (bytecnt == -1)
121		head(stdin, linecnt);
122	else
123		head_bytes(stdin, bytecnt);
124
125	exit(eval);
126}
127
128void
129head(fp, cnt)
130	FILE *fp;
131	register int cnt;
132{
133	register int ch;
134
135	while (cnt && (ch = getc(fp)) != EOF) {
136			if (putchar(ch) == EOF)
137				err(1, "stdout");
138			if (ch == '\n')
139				cnt--;
140		}
141}
142
143void
144head_bytes(fp, cnt)
145	FILE *fp;
146	register int cnt;
147{
148	char buf[4096];
149	register int readlen;
150
151	while (cnt) {
152		if (cnt < sizeof(buf))
153			readlen = cnt;
154		else
155			readlen = sizeof(buf);
156		readlen = fread(buf, sizeof(char), readlen, fp);
157		if (readlen == 0)
158			break;
159		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
160			err(1, "stdout");
161		cnt -= readlen;
162	}
163}
164
165void
166obsolete(argv)
167	char *argv[];
168{
169	char *ap;
170
171	while ((ap = *++argv)) {
172		/* Return if "--" or not "-[0-9]*". */
173		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
174			return;
175		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
176			err(1, NULL);
177		ap[0] = '-';
178		ap[1] = 'n';
179		(void)strcpy(ap + 2, *argv + 1);
180		*argv = ap;
181	}
182}
183
184void
185usage()
186{
187
188	(void)fprintf(stderr, "usage: head [-n lines] [-c bytes] [file ...]\n");
189	exit(1);
190}
191