head.c revision 1590
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
351590Srgrimesstatic 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
411590Srgrimesstatic char sccsid[] = "@(#)head.c	8.1 (Berkeley) 6/6/93";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes#include <sys/types.h>
451590Srgrimes#include <errno.h>
461590Srgrimes#include <stdlib.h>
471590Srgrimes#include <string.h>
481590Srgrimes#include <stdio.h>
491590Srgrimes#include <ctype.h>
501590Srgrimes
511590Srgrimes/*
521590Srgrimes * head - give the first few lines of a stream or of each of a set of files
531590Srgrimes *
541590Srgrimes * Bill Joy UCB August 24, 1977
551590Srgrimes */
561590Srgrimes
571590Srgrimesvoid err __P((int, const char *, ...));
581590Srgrimesvoid head __P((FILE *, int));
591590Srgrimesvoid obsolete __P((char *[]));
601590Srgrimesvoid usage __P((void));
611590Srgrimes
621590Srgrimesint eval;
631590Srgrimes
641590Srgrimesint
651590Srgrimesmain(argc, argv)
661590Srgrimes	int argc;
671590Srgrimes	char *argv[];
681590Srgrimes{
691590Srgrimes	register int ch;
701590Srgrimes	FILE *fp;
711590Srgrimes	int first, linecnt;
721590Srgrimes	char *ep;
731590Srgrimes
741590Srgrimes	obsolete(argv);
751590Srgrimes	linecnt = 10;
761590Srgrimes	while ((ch = getopt(argc, argv, "n:")) != EOF)
771590Srgrimes		switch(ch) {
781590Srgrimes		case 'n':
791590Srgrimes			linecnt = strtol(optarg, &ep, 10);
801590Srgrimes			if (*ep || linecnt <= 0)
811590Srgrimes				err(1, "illegal line count -- %s", optarg);
821590Srgrimes			break;
831590Srgrimes		case '?':
841590Srgrimes		default:
851590Srgrimes			usage();
861590Srgrimes		}
871590Srgrimes	argc -= optind;
881590Srgrimes	argv += optind;
891590Srgrimes
901590Srgrimes	if (*argv)
911590Srgrimes		for (first = 1; *argv; ++argv) {
921590Srgrimes			if ((fp = fopen(*argv, "r")) == NULL) {
931590Srgrimes				err(0, "%s: %s", *argv, strerror(errno));
941590Srgrimes				continue;
951590Srgrimes			}
961590Srgrimes			if (argc > 1) {
971590Srgrimes				(void)printf("%s==> %s <==\n",
981590Srgrimes				    first ? "" : "\n", *argv);
991590Srgrimes				first = 0;
1001590Srgrimes			}
1011590Srgrimes			head(fp, linecnt);
1021590Srgrimes			(void)fclose(fp);
1031590Srgrimes		}
1041590Srgrimes	else
1051590Srgrimes		head(stdin, linecnt);
1061590Srgrimes	exit(eval);
1071590Srgrimes}
1081590Srgrimes
1091590Srgrimesvoid
1101590Srgrimeshead(fp, cnt)
1111590Srgrimes	FILE *fp;
1121590Srgrimes	register int cnt;
1131590Srgrimes{
1141590Srgrimes	register int ch;
1151590Srgrimes
1161590Srgrimes	while (cnt--)
1171590Srgrimes		while ((ch = getc(fp)) != EOF) {
1181590Srgrimes			if (putchar(ch) == EOF)
1191590Srgrimes				err(1, "stdout: %s", strerror(errno));
1201590Srgrimes			if (ch == '\n')
1211590Srgrimes				break;
1221590Srgrimes		}
1231590Srgrimes}
1241590Srgrimes
1251590Srgrimesvoid
1261590Srgrimesobsolete(argv)
1271590Srgrimes	char *argv[];
1281590Srgrimes{
1291590Srgrimes	char *ap;
1301590Srgrimes
1311590Srgrimes	while (ap = *++argv) {
1321590Srgrimes		/* Return if "--" or not "-[0-9]*". */
1331590Srgrimes		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1341590Srgrimes			return;
1351590Srgrimes		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
1361590Srgrimes			err(1, "%s", strerror(errno));
1371590Srgrimes		ap[0] = '-';
1381590Srgrimes		ap[1] = 'n';
1391590Srgrimes		(void)strcpy(ap + 2, *argv + 1);
1401590Srgrimes		*argv = ap;
1411590Srgrimes	}
1421590Srgrimes}
1431590Srgrimes
1441590Srgrimesvoid
1451590Srgrimesusage()
1461590Srgrimes{
1471590Srgrimes	(void)fprintf(stderr, "usage: head [-n lines] [file ...]\n");
1481590Srgrimes	exit(1);
1491590Srgrimes}
1501590Srgrimes
1511590Srgrimes#if __STDC__
1521590Srgrimes#include <stdarg.h>
1531590Srgrimes#else
1541590Srgrimes#include <varargs.h>
1551590Srgrimes#endif
1561590Srgrimes
1571590Srgrimesvoid
1581590Srgrimes#if __STDC__
1591590Srgrimeserr(int fatal, const char *fmt, ...)
1601590Srgrimes#else
1611590Srgrimeserr(fatal, fmt, va_alist)
1621590Srgrimes	int fatal;
1631590Srgrimes	char *fmt;
1641590Srgrimes        va_dcl
1651590Srgrimes#endif
1661590Srgrimes{
1671590Srgrimes	va_list ap;
1681590Srgrimes#if __STDC__
1691590Srgrimes	va_start(ap, fmt);
1701590Srgrimes#else
1711590Srgrimes	va_start(ap);
1721590Srgrimes#endif
1731590Srgrimes	(void)fprintf(stderr, "head: ");
1741590Srgrimes	(void)vfprintf(stderr, fmt, ap);
1751590Srgrimes	va_end(ap);
1761590Srgrimes	(void)fprintf(stderr, "\n");
1771590Srgrimes	if (fatal)
1781590Srgrimes		exit(1);
1791590Srgrimes	eval = 1;
1801590Srgrimes}
181