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