11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1987, 1992, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3127314Scharnierstatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
361590Srgrimes#ifndef lint
3727314Scharnier#if 0
3823693Speterstatic char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
3927314Scharnier#endif
401590Srgrimes#endif /* not lint */
4199112Sobrien#include <sys/cdefs.h>
4299112Sobrien__FBSDID("$FreeBSD$");
431590Srgrimes
441590Srgrimes#include <sys/types.h>
4523693Speter
4623693Speter#include <ctype.h>
4727314Scharnier#include <err.h>
48165940Sbrooks#include <inttypes.h>
4923693Speter#include <stdio.h>
501590Srgrimes#include <stdlib.h>
511590Srgrimes#include <string.h>
5223693Speter#include <unistd.h>
531590Srgrimes
541590Srgrimes/*
551590Srgrimes * head - give the first few lines of a stream or of each of a set of files
561590Srgrimes *
571590Srgrimes * Bill Joy UCB August 24, 1977
581590Srgrimes */
591590Srgrimes
6093441Sdwmalonestatic void head(FILE *, int);
61165940Sbrooksstatic void head_bytes(FILE *, off_t);
6293441Sdwmalonestatic void obsolete(char *[]);
6393441Sdwmalonestatic void usage(void);
641590Srgrimes
651590Srgrimesint
6695652Smarkmmain(int argc, char *argv[])
671590Srgrimes{
6893441Sdwmalone	int ch;
691590Srgrimes	FILE *fp;
70165940Sbrooks	int first, linecnt = -1, eval = 0;
71165940Sbrooks	off_t bytecnt = -1;
721590Srgrimes	char *ep;
731590Srgrimes
741590Srgrimes	obsolete(argv);
7524665Salex	while ((ch = getopt(argc, argv, "n:c:")) != -1)
761590Srgrimes		switch(ch) {
7724665Salex		case 'c':
78165940Sbrooks			bytecnt = strtoimax(optarg, &ep, 10);
7924665Salex			if (*ep || bytecnt <= 0)
8027314Scharnier				errx(1, "illegal byte count -- %s", optarg);
8124665Salex			break;
821590Srgrimes		case 'n':
831590Srgrimes			linecnt = strtol(optarg, &ep, 10);
841590Srgrimes			if (*ep || linecnt <= 0)
8527314Scharnier				errx(1, "illegal line count -- %s", optarg);
861590Srgrimes			break;
871590Srgrimes		case '?':
881590Srgrimes		default:
891590Srgrimes			usage();
901590Srgrimes		}
911590Srgrimes	argc -= optind;
921590Srgrimes	argv += optind;
931590Srgrimes
9424665Salex	if (linecnt != -1 && bytecnt != -1)
9527314Scharnier		errx(1, "can't combine line and byte counts");
9624665Salex	if (linecnt == -1 )
9724665Salex		linecnt = 10;
9824665Salex	if (*argv) {
991590Srgrimes		for (first = 1; *argv; ++argv) {
1001590Srgrimes			if ((fp = fopen(*argv, "r")) == NULL) {
10127314Scharnier				warn("%s", *argv);
10227328Scharnier				eval = 1;
1031590Srgrimes				continue;
1041590Srgrimes			}
1051590Srgrimes			if (argc > 1) {
1061590Srgrimes				(void)printf("%s==> %s <==\n",
1071590Srgrimes				    first ? "" : "\n", *argv);
1081590Srgrimes				first = 0;
1091590Srgrimes			}
11024665Salex			if (bytecnt == -1)
11124665Salex				head(fp, linecnt);
11224665Salex			else
11324665Salex				head_bytes(fp, bytecnt);
1141590Srgrimes			(void)fclose(fp);
1151590Srgrimes		}
11685859Salfred	} else if (bytecnt == -1)
11724665Salex		head(stdin, linecnt);
1181590Srgrimes	else
11924665Salex		head_bytes(stdin, bytecnt);
12024665Salex
1211590Srgrimes	exit(eval);
1221590Srgrimes}
1231590Srgrimes
12493441Sdwmalonestatic void
12595652Smarkmhead(FILE *fp, int cnt)
1261590Srgrimes{
12785861Salfred	char *cp;
12893441Sdwmalone	size_t error, readlen;
1291590Srgrimes
13085861Salfred	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
13185861Salfred		error = fwrite(cp, sizeof(char), readlen, stdout);
13285861Salfred		if (error != readlen)
13385861Salfred			err(1, "stdout");
13485861Salfred		cnt--;
13585861Salfred	}
1361590Srgrimes}
1371590Srgrimes
13893441Sdwmalonestatic void
139165940Sbrookshead_bytes(FILE *fp, off_t cnt)
14024665Salex{
14124665Salex	char buf[4096];
14293441Sdwmalone	size_t readlen;
14324665Salex
14424665Salex	while (cnt) {
145165955Sbrooks		if ((uintmax_t)cnt < sizeof(buf))
14624665Salex			readlen = cnt;
14724665Salex		else
14824665Salex			readlen = sizeof(buf);
14924665Salex		readlen = fread(buf, sizeof(char), readlen, fp);
15040114Sdes		if (readlen == 0)
15124665Salex			break;
15224665Salex		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
15327314Scharnier			err(1, "stdout");
15424665Salex		cnt -= readlen;
15524665Salex	}
15624665Salex}
15724665Salex
15893441Sdwmalonestatic void
15995652Smarkmobsolete(char *argv[])
1601590Srgrimes{
1611590Srgrimes	char *ap;
1621590Srgrimes
16324665Salex	while ((ap = *++argv)) {
1641590Srgrimes		/* Return if "--" or not "-[0-9]*". */
1651590Srgrimes		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1661590Srgrimes			return;
1671590Srgrimes		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
16827314Scharnier			err(1, NULL);
1691590Srgrimes		ap[0] = '-';
1701590Srgrimes		ap[1] = 'n';
1711590Srgrimes		(void)strcpy(ap + 2, *argv + 1);
1721590Srgrimes		*argv = ap;
1731590Srgrimes	}
1741590Srgrimes}
1751590Srgrimes
17693441Sdwmalonestatic void
17795652Smarkmusage(void)
1781590Srgrimes{
17985859Salfred
18089767Sdwmalone	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1811590Srgrimes	exit(1);
1821590Srgrimes}
183