1*78a738b3Slukem /* $NetBSD: head.c,v 1.8 1997/10/19 02:23:45 lukem Exp $ */ 29d225a17Stls 361f28255Scgd /* 43ac07874Smrg * Copyright (c) 1980, 1987, 1992, 1993 53ac07874Smrg * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * Redistribution and use in source and binary forms, with or without 861f28255Scgd * modification, are permitted provided that the following conditions 961f28255Scgd * are met: 1061f28255Scgd * 1. Redistributions of source code must retain the above copyright 1161f28255Scgd * notice, this list of conditions and the following disclaimer. 1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1361f28255Scgd * notice, this list of conditions and the following disclaimer in the 1461f28255Scgd * documentation and/or other materials provided with the distribution. 1561f28255Scgd * 3. All advertising materials mentioning features or use of this software 1661f28255Scgd * must display the following acknowledgement: 1761f28255Scgd * This product includes software developed by the University of 1861f28255Scgd * California, Berkeley and its contributors. 1961f28255Scgd * 4. Neither the name of the University nor the names of its contributors 2061f28255Scgd * may be used to endorse or promote products derived from this software 2161f28255Scgd * without specific prior written permission. 2261f28255Scgd * 2361f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2461f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2561f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2661f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2761f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2861f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2961f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3061f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3161f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3261f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3361f28255Scgd * SUCH DAMAGE. 3461f28255Scgd */ 3561f28255Scgd 363ac07874Smrg #include <sys/cdefs.h> 3761f28255Scgd #ifndef lint 383ac07874Smrg __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\ 393ac07874Smrg The Regents of the University of California. All rights reserved.\n"); 4061f28255Scgd #endif /* not lint */ 4161f28255Scgd 4261f28255Scgd #ifndef lint 433ac07874Smrg #if 0 443ac07874Smrg static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95"; 453ac07874Smrg #else 46*78a738b3Slukem __RCSID("$NetBSD: head.c,v 1.8 1997/10/19 02:23:45 lukem Exp $"); 473ac07874Smrg #endif 4861f28255Scgd #endif /* not lint */ 4961f28255Scgd 503ac07874Smrg #include <sys/types.h> 513ac07874Smrg 523ac07874Smrg #include <ctype.h> 53*78a738b3Slukem #include <err.h> 543ac07874Smrg #include <errno.h> 5561f28255Scgd #include <stdio.h> 56aab7593aSjtc #include <stdlib.h> 573ac07874Smrg #include <string.h> 582ddbb97fSjtc #include <unistd.h> 59aab7593aSjtc 6061f28255Scgd /* 6161f28255Scgd * head - give the first few lines of a stream or of each of a set of files 6261f28255Scgd * 6361f28255Scgd * Bill Joy UCB August 24, 1977 6461f28255Scgd */ 6561f28255Scgd 663ac07874Smrg void head __P((FILE *, int)); 673ac07874Smrg void obsolete __P((char *[])); 683ac07874Smrg void usage __P((void)); 693ac07874Smrg int main __P((int, char *[])); 703ac07874Smrg 713ac07874Smrg int eval; 723ac07874Smrg 73f7c6bf57Sjtc int 7461f28255Scgd main(argc, argv) 7561f28255Scgd int argc; 763ac07874Smrg char *argv[]; 7761f28255Scgd { 78*78a738b3Slukem int ch; 793ac07874Smrg FILE *fp; 803ac07874Smrg int first, linecnt; 813ac07874Smrg char *ep; 8261f28255Scgd 833ac07874Smrg obsolete(argv); 843ac07874Smrg linecnt = 10; 85*78a738b3Slukem while ((ch = getopt(argc, argv, "n:")) != -1) 86aab7593aSjtc switch(ch) { 87aab7593aSjtc case 'n': 883ac07874Smrg linecnt = strtol(optarg, &ep, 10); 893ac07874Smrg if (*ep || linecnt <= 0) 903ac07874Smrg err(1, "illegal line count -- %s", optarg); 91aab7593aSjtc break; 92aab7593aSjtc 933ac07874Smrg case '?': 94aab7593aSjtc default: 95aab7593aSjtc usage(); 96aab7593aSjtc } 973ac07874Smrg argc -= optind; 983ac07874Smrg argv += optind; 99aab7593aSjtc 1003ac07874Smrg if (*argv) 1013ac07874Smrg for (first = 1; *argv; ++argv) { 1023ac07874Smrg if ((fp = fopen(*argv, "r")) == NULL) { 1033ac07874Smrg err(0, "%s: %s", *argv, strerror(errno)); 1043ac07874Smrg continue; 10561f28255Scgd } 10661f28255Scgd if (argc > 1) { 1073ac07874Smrg (void)printf("%s==> %s <==\n", 1083ac07874Smrg first ? "" : "\n", *argv); 1093ac07874Smrg first = 0; 11061f28255Scgd } 1113ac07874Smrg head(fp, linecnt); 1123ac07874Smrg (void)fclose(fp); 11361f28255Scgd } 1143ac07874Smrg else 1153ac07874Smrg head(stdin, linecnt); 1163ac07874Smrg exit(eval); 1173ac07874Smrg } 1183ac07874Smrg 1193ac07874Smrg void 1203ac07874Smrg head(fp, cnt) 1213ac07874Smrg FILE *fp; 122*78a738b3Slukem int cnt; 1233ac07874Smrg { 124*78a738b3Slukem int ch; 1253ac07874Smrg 1263ac07874Smrg while (cnt--) 1273ac07874Smrg while ((ch = getc(fp)) != EOF) { 1283ac07874Smrg if (putchar(ch) == EOF) 1293ac07874Smrg err(1, "stdout: %s", strerror(errno)); 1303ac07874Smrg if (ch == '\n') 13161f28255Scgd break; 13261f28255Scgd } 13361f28255Scgd } 134aab7593aSjtc 1353ac07874Smrg void 1363ac07874Smrg obsolete(argv) 1373ac07874Smrg char *argv[]; 1383ac07874Smrg { 1393ac07874Smrg char *ap; 140aab7593aSjtc 1413ac07874Smrg while ((ap = *++argv)) { 1423ac07874Smrg /* Return if "--" or not "-[0-9]*". */ 1433ac07874Smrg if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1])) 1443ac07874Smrg return; 1453ac07874Smrg if ((ap = malloc(strlen(*argv) + 2)) == NULL) 1463ac07874Smrg err(1, "%s", strerror(errno)); 1473ac07874Smrg ap[0] = '-'; 1483ac07874Smrg ap[1] = 'n'; 1493ac07874Smrg (void)strcpy(ap + 2, *argv + 1); 1503ac07874Smrg *argv = ap; 1513ac07874Smrg } 1523ac07874Smrg } 1533ac07874Smrg 1543ac07874Smrg void 155aab7593aSjtc usage() 156aab7593aSjtc { 1573ac07874Smrg (void)fputs("usage: head [-n lines] [file ...]\n", stderr); 158aab7593aSjtc exit(1); 159aab7593aSjtc } 160