head.c revision 40114
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 4427314Scharnierstatic const char rcsid[] = 4540114Sdes "$Id: head.c,v 1.8 1997/07/11 06:13:18 charnier Exp $"; 461590Srgrimes#endif /* not lint */ 471590Srgrimes 481590Srgrimes#include <sys/types.h> 4923693Speter 5023693Speter#include <ctype.h> 5127314Scharnier#include <err.h> 5223693Speter#include <stdio.h> 531590Srgrimes#include <stdlib.h> 541590Srgrimes#include <string.h> 5523693Speter#include <unistd.h> 561590Srgrimes 571590Srgrimes/* 581590Srgrimes * head - give the first few lines of a stream or of each of a set of files 591590Srgrimes * 601590Srgrimes * Bill Joy UCB August 24, 1977 611590Srgrimes */ 621590Srgrimes 631590Srgrimesvoid head __P((FILE *, int)); 6424665Salexvoid head_bytes __P((FILE *, int)); 651590Srgrimesvoid obsolete __P((char *[])); 661590Srgrimesvoid usage __P((void)); 671590Srgrimes 681590Srgrimesint eval; 691590Srgrimes 701590Srgrimesint 711590Srgrimesmain(argc, argv) 721590Srgrimes int argc; 731590Srgrimes char *argv[]; 741590Srgrimes{ 751590Srgrimes register int ch; 761590Srgrimes FILE *fp; 7724665Salex int first, linecnt = -1, bytecnt = -1; 781590Srgrimes char *ep; 791590Srgrimes 801590Srgrimes obsolete(argv); 8124665Salex while ((ch = getopt(argc, argv, "n:c:")) != -1) 821590Srgrimes switch(ch) { 8324665Salex case 'c': 8424665Salex bytecnt = strtol(optarg, &ep, 10); 8524665Salex if (*ep || bytecnt <= 0) 8627314Scharnier errx(1, "illegal byte count -- %s", optarg); 8724665Salex break; 881590Srgrimes case 'n': 891590Srgrimes linecnt = strtol(optarg, &ep, 10); 901590Srgrimes if (*ep || linecnt <= 0) 9127314Scharnier errx(1, "illegal line count -- %s", optarg); 921590Srgrimes break; 931590Srgrimes case '?': 941590Srgrimes default: 951590Srgrimes usage(); 961590Srgrimes } 971590Srgrimes argc -= optind; 981590Srgrimes argv += optind; 991590Srgrimes 10024665Salex if (linecnt != -1 && bytecnt != -1) 10127314Scharnier errx(1, "can't combine line and byte counts"); 10224665Salex if (linecnt == -1 ) 10324665Salex linecnt = 10; 10424665Salex if (*argv) { 1051590Srgrimes for (first = 1; *argv; ++argv) { 1061590Srgrimes if ((fp = fopen(*argv, "r")) == NULL) { 10727314Scharnier warn("%s", *argv); 10827328Scharnier eval = 1; 1091590Srgrimes continue; 1101590Srgrimes } 1111590Srgrimes if (argc > 1) { 1121590Srgrimes (void)printf("%s==> %s <==\n", 1131590Srgrimes first ? "" : "\n", *argv); 1141590Srgrimes first = 0; 1151590Srgrimes } 11624665Salex if (bytecnt == -1) 11724665Salex head(fp, linecnt); 11824665Salex else 11924665Salex head_bytes(fp, bytecnt); 1201590Srgrimes (void)fclose(fp); 1211590Srgrimes } 12224665Salex } 12324665Salex else if (bytecnt == -1) 12424665Salex head(stdin, linecnt); 1251590Srgrimes else 12624665Salex head_bytes(stdin, bytecnt); 12724665Salex 1281590Srgrimes exit(eval); 1291590Srgrimes} 1301590Srgrimes 1311590Srgrimesvoid 1321590Srgrimeshead(fp, cnt) 1331590Srgrimes FILE *fp; 1341590Srgrimes register int cnt; 1351590Srgrimes{ 1361590Srgrimes register int ch; 1371590Srgrimes 13814269Swosch while (cnt && (ch = getc(fp)) != EOF) { 1391590Srgrimes if (putchar(ch) == EOF) 14027314Scharnier err(1, "stdout"); 1411590Srgrimes if (ch == '\n') 14210064Sjoerg cnt--; 1431590Srgrimes } 1441590Srgrimes} 1451590Srgrimes 1461590Srgrimesvoid 14724665Salexhead_bytes(fp, cnt) 14824665Salex FILE *fp; 14924665Salex register int cnt; 15024665Salex{ 15124665Salex char buf[4096]; 15224665Salex register int readlen; 15324665Salex 15424665Salex while (cnt) { 15524665Salex if (cnt < sizeof(buf)) 15624665Salex readlen = cnt; 15724665Salex else 15824665Salex readlen = sizeof(buf); 15924665Salex readlen = fread(buf, sizeof(char), readlen, fp); 16040114Sdes if (readlen == 0) 16124665Salex break; 16224665Salex if (fwrite(buf, sizeof(char), readlen, stdout) != readlen) 16327314Scharnier err(1, "stdout"); 16424665Salex cnt -= readlen; 16524665Salex } 16624665Salex} 16724665Salex 16824665Salexvoid 1691590Srgrimesobsolete(argv) 1701590Srgrimes char *argv[]; 1711590Srgrimes{ 1721590Srgrimes char *ap; 1731590Srgrimes 17424665Salex while ((ap = *++argv)) { 1751590Srgrimes /* Return if "--" or not "-[0-9]*". */ 1761590Srgrimes if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1])) 1771590Srgrimes return; 1781590Srgrimes if ((ap = malloc(strlen(*argv) + 2)) == NULL) 17927314Scharnier err(1, NULL); 1801590Srgrimes ap[0] = '-'; 1811590Srgrimes ap[1] = 'n'; 1821590Srgrimes (void)strcpy(ap + 2, *argv + 1); 1831590Srgrimes *argv = ap; 1841590Srgrimes } 1851590Srgrimes} 1861590Srgrimes 1871590Srgrimesvoid 1881590Srgrimesusage() 1891590Srgrimes{ 19027314Scharnier (void)fprintf(stderr, "usage: head [-n lines] [-c bytes] [file ...]\n"); 1911590Srgrimes exit(1); 1921590Srgrimes} 193