head.c revision 99112
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 99112 2002-06-30 05:25:07Z obrien $");
471590Srgrimes
4895652Smarkm#include <sys/cdefs.h>
4995652Smarkm__FBSDID("$FreeBSD: head/usr.bin/head/head.c 99112 2002-06-30 05:25:07Z obrien $");
5095652Smarkm
511590Srgrimes#include <sys/types.h>
5223693Speter
5323693Speter#include <ctype.h>
5427314Scharnier#include <err.h>
5523693Speter#include <stdio.h>
561590Srgrimes#include <stdlib.h>
571590Srgrimes#include <string.h>
5823693Speter#include <unistd.h>
591590Srgrimes
601590Srgrimes/*
611590Srgrimes * head - give the first few lines of a stream or of each of a set of files
621590Srgrimes *
631590Srgrimes * Bill Joy UCB August 24, 1977
641590Srgrimes */
651590Srgrimes
6693441Sdwmalonestatic void head(FILE *, int);
6793441Sdwmalonestatic void head_bytes(FILE *, size_t);
6893441Sdwmalonestatic void obsolete(char *[]);
6993441Sdwmalonestatic void usage(void);
701590Srgrimes
711590Srgrimesint
7295652Smarkmmain(int argc, char *argv[])
731590Srgrimes{
7493441Sdwmalone	int ch;
751590Srgrimes	FILE *fp;
7685859Salfred	int first, linecnt = -1, bytecnt = -1, eval = 0;
771590Srgrimes	char *ep;
781590Srgrimes
791590Srgrimes	obsolete(argv);
8024665Salex	while ((ch = getopt(argc, argv, "n:c:")) != -1)
811590Srgrimes		switch(ch) {
8224665Salex		case 'c':
8324665Salex			bytecnt = strtol(optarg, &ep, 10);
8424665Salex			if (*ep || bytecnt <= 0)
8527314Scharnier				errx(1, "illegal byte count -- %s", optarg);
8624665Salex			break;
871590Srgrimes		case 'n':
881590Srgrimes			linecnt = strtol(optarg, &ep, 10);
891590Srgrimes			if (*ep || linecnt <= 0)
9027314Scharnier				errx(1, "illegal line count -- %s", optarg);
911590Srgrimes			break;
921590Srgrimes		case '?':
931590Srgrimes		default:
941590Srgrimes			usage();
951590Srgrimes		}
961590Srgrimes	argc -= optind;
971590Srgrimes	argv += optind;
981590Srgrimes
9924665Salex	if (linecnt != -1 && bytecnt != -1)
10027314Scharnier		errx(1, "can't combine line and byte counts");
10124665Salex	if (linecnt == -1 )
10224665Salex		linecnt = 10;
10324665Salex	if (*argv) {
1041590Srgrimes		for (first = 1; *argv; ++argv) {
1051590Srgrimes			if ((fp = fopen(*argv, "r")) == NULL) {
10627314Scharnier				warn("%s", *argv);
10727328Scharnier				eval = 1;
1081590Srgrimes				continue;
1091590Srgrimes			}
1101590Srgrimes			if (argc > 1) {
1111590Srgrimes				(void)printf("%s==> %s <==\n",
1121590Srgrimes				    first ? "" : "\n", *argv);
1131590Srgrimes				first = 0;
1141590Srgrimes			}
11524665Salex			if (bytecnt == -1)
11624665Salex				head(fp, linecnt);
11724665Salex			else
11824665Salex				head_bytes(fp, bytecnt);
1191590Srgrimes			(void)fclose(fp);
1201590Srgrimes		}
12185859Salfred	} else if (bytecnt == -1)
12224665Salex		head(stdin, linecnt);
1231590Srgrimes	else
12424665Salex		head_bytes(stdin, bytecnt);
12524665Salex
1261590Srgrimes	exit(eval);
1271590Srgrimes}
1281590Srgrimes
12993441Sdwmalonestatic void
13095652Smarkmhead(FILE *fp, int cnt)
1311590Srgrimes{
13285861Salfred	char *cp;
13393441Sdwmalone	size_t error, readlen;
1341590Srgrimes
13585861Salfred	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
13685861Salfred		error = fwrite(cp, sizeof(char), readlen, stdout);
13785861Salfred		if (error != readlen)
13885861Salfred			err(1, "stdout");
13985861Salfred		cnt--;
14085861Salfred	}
1411590Srgrimes}
1421590Srgrimes
14393441Sdwmalonestatic void
14495652Smarkmhead_bytes(FILE *fp, size_t cnt)
14524665Salex{
14624665Salex	char buf[4096];
14793441Sdwmalone	size_t readlen;
14824665Salex
14924665Salex	while (cnt) {
15024665Salex		if (cnt < sizeof(buf))
15124665Salex			readlen = cnt;
15224665Salex		else
15324665Salex			readlen = sizeof(buf);
15424665Salex		readlen = fread(buf, sizeof(char), readlen, fp);
15540114Sdes		if (readlen == 0)
15624665Salex			break;
15724665Salex		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
15827314Scharnier			err(1, "stdout");
15924665Salex		cnt -= readlen;
16024665Salex	}
16124665Salex}
16224665Salex
16393441Sdwmalonestatic void
16495652Smarkmobsolete(char *argv[])
1651590Srgrimes{
1661590Srgrimes	char *ap;
1671590Srgrimes
16824665Salex	while ((ap = *++argv)) {
1691590Srgrimes		/* Return if "--" or not "-[0-9]*". */
1701590Srgrimes		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1711590Srgrimes			return;
1721590Srgrimes		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
17327314Scharnier			err(1, NULL);
1741590Srgrimes		ap[0] = '-';
1751590Srgrimes		ap[1] = 'n';
1761590Srgrimes		(void)strcpy(ap + 2, *argv + 1);
1771590Srgrimes		*argv = ap;
1781590Srgrimes	}
1791590Srgrimes}
1801590Srgrimes
18193441Sdwmalonestatic void
18295652Smarkmusage(void)
1831590Srgrimes{
18485859Salfred
18589767Sdwmalone	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1861590Srgrimes	exit(1);
1871590Srgrimes}
188