head.c revision 165955
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 * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527314Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127314Scharnier#if 0
4223693Speterstatic char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
4327314Scharnier#endif
441590Srgrimes#endif /* not lint */
4599112Sobrien#include <sys/cdefs.h>
4699112Sobrien__FBSDID("$FreeBSD: head/usr.bin/head/head.c 165955 2007-01-11 20:23:01Z brooks $");
471590Srgrimes
481590Srgrimes#include <sys/types.h>
4923693Speter
5023693Speter#include <ctype.h>
5127314Scharnier#include <err.h>
52165940Sbrooks#include <inttypes.h>
5323693Speter#include <stdio.h>
541590Srgrimes#include <stdlib.h>
551590Srgrimes#include <string.h>
5623693Speter#include <unistd.h>
571590Srgrimes
581590Srgrimes/*
591590Srgrimes * head - give the first few lines of a stream or of each of a set of files
601590Srgrimes *
611590Srgrimes * Bill Joy UCB August 24, 1977
621590Srgrimes */
631590Srgrimes
6493441Sdwmalonestatic void head(FILE *, int);
65165940Sbrooksstatic void head_bytes(FILE *, off_t);
6693441Sdwmalonestatic void obsolete(char *[]);
6793441Sdwmalonestatic void usage(void);
681590Srgrimes
691590Srgrimesint
7095652Smarkmmain(int argc, char *argv[])
711590Srgrimes{
7293441Sdwmalone	int ch;
731590Srgrimes	FILE *fp;
74165940Sbrooks	int first, linecnt = -1, eval = 0;
75165940Sbrooks	off_t bytecnt = -1;
761590Srgrimes	char *ep;
771590Srgrimes
781590Srgrimes	obsolete(argv);
7924665Salex	while ((ch = getopt(argc, argv, "n:c:")) != -1)
801590Srgrimes		switch(ch) {
8124665Salex		case 'c':
82165940Sbrooks			bytecnt = strtoimax(optarg, &ep, 10);
8324665Salex			if (*ep || bytecnt <= 0)
8427314Scharnier				errx(1, "illegal byte count -- %s", optarg);
8524665Salex			break;
861590Srgrimes		case 'n':
871590Srgrimes			linecnt = strtol(optarg, &ep, 10);
881590Srgrimes			if (*ep || linecnt <= 0)
8927314Scharnier				errx(1, "illegal line count -- %s", optarg);
901590Srgrimes			break;
911590Srgrimes		case '?':
921590Srgrimes		default:
931590Srgrimes			usage();
941590Srgrimes		}
951590Srgrimes	argc -= optind;
961590Srgrimes	argv += optind;
971590Srgrimes
9824665Salex	if (linecnt != -1 && bytecnt != -1)
9927314Scharnier		errx(1, "can't combine line and byte counts");
10024665Salex	if (linecnt == -1 )
10124665Salex		linecnt = 10;
10224665Salex	if (*argv) {
1031590Srgrimes		for (first = 1; *argv; ++argv) {
1041590Srgrimes			if ((fp = fopen(*argv, "r")) == NULL) {
10527314Scharnier				warn("%s", *argv);
10627328Scharnier				eval = 1;
1071590Srgrimes				continue;
1081590Srgrimes			}
1091590Srgrimes			if (argc > 1) {
1101590Srgrimes				(void)printf("%s==> %s <==\n",
1111590Srgrimes				    first ? "" : "\n", *argv);
1121590Srgrimes				first = 0;
1131590Srgrimes			}
11424665Salex			if (bytecnt == -1)
11524665Salex				head(fp, linecnt);
11624665Salex			else
11724665Salex				head_bytes(fp, bytecnt);
1181590Srgrimes			(void)fclose(fp);
1191590Srgrimes		}
12085859Salfred	} else if (bytecnt == -1)
12124665Salex		head(stdin, linecnt);
1221590Srgrimes	else
12324665Salex		head_bytes(stdin, bytecnt);
12424665Salex
1251590Srgrimes	exit(eval);
1261590Srgrimes}
1271590Srgrimes
12893441Sdwmalonestatic void
12995652Smarkmhead(FILE *fp, int cnt)
1301590Srgrimes{
13185861Salfred	char *cp;
13293441Sdwmalone	size_t error, readlen;
1331590Srgrimes
13485861Salfred	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
13585861Salfred		error = fwrite(cp, sizeof(char), readlen, stdout);
13685861Salfred		if (error != readlen)
13785861Salfred			err(1, "stdout");
13885861Salfred		cnt--;
13985861Salfred	}
1401590Srgrimes}
1411590Srgrimes
14293441Sdwmalonestatic void
143165940Sbrookshead_bytes(FILE *fp, off_t cnt)
14424665Salex{
14524665Salex	char buf[4096];
14693441Sdwmalone	size_t readlen;
14724665Salex
14824665Salex	while (cnt) {
149165955Sbrooks		if ((uintmax_t)cnt < sizeof(buf))
15024665Salex			readlen = cnt;
15124665Salex		else
15224665Salex			readlen = sizeof(buf);
15324665Salex		readlen = fread(buf, sizeof(char), readlen, fp);
15440114Sdes		if (readlen == 0)
15524665Salex			break;
15624665Salex		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
15727314Scharnier			err(1, "stdout");
15824665Salex		cnt -= readlen;
15924665Salex	}
16024665Salex}
16124665Salex
16293441Sdwmalonestatic void
16395652Smarkmobsolete(char *argv[])
1641590Srgrimes{
1651590Srgrimes	char *ap;
1661590Srgrimes
16724665Salex	while ((ap = *++argv)) {
1681590Srgrimes		/* Return if "--" or not "-[0-9]*". */
1691590Srgrimes		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1701590Srgrimes			return;
1711590Srgrimes		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
17227314Scharnier			err(1, NULL);
1731590Srgrimes		ap[0] = '-';
1741590Srgrimes		ap[1] = 'n';
1751590Srgrimes		(void)strcpy(ap + 2, *argv + 1);
1761590Srgrimes		*argv = ap;
1771590Srgrimes	}
1781590Srgrimes}
1791590Srgrimes
18093441Sdwmalonestatic void
18195652Smarkmusage(void)
1821590Srgrimes{
18385859Salfred
18489767Sdwmalone	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1851590Srgrimes	exit(1);
1861590Srgrimes}
187