Home | History | Annotate | Line # | Download | only in head
head.c revision 1.11
      1 /*	$NetBSD: head.c,v 1.11 1998/12/19 16:40:29 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1987, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
     45 #else
     46 __RCSID("$NetBSD: head.c,v 1.11 1998/12/19 16:40:29 christos Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 
     52 #include <ctype.h>
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <limits.h>
     56 #include <locale.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <unistd.h>
     61 
     62 /*
     63  * head - give the first few lines of a stream or of each of a set of files
     64  *
     65  * Bill Joy UCB August 24, 1977
     66  */
     67 
     68 void head __P((FILE *, long));
     69 void obsolete __P((char *[]));
     70 void usage __P((void));
     71 int main __P((int, char *[]));
     72 
     73 int eval = 0;
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char *argv[];
     79 {
     80 	int ch;
     81 	FILE *fp;
     82 	int first;
     83 	long linecnt;
     84 	char *ep;
     85 
     86 	(void)setlocale(LC_ALL, "");
     87 	obsolete(argv);
     88 	linecnt = 10;
     89 	while ((ch = getopt(argc, argv, "n:")) != -1)
     90 		switch(ch) {
     91 		case 'n':
     92 			linecnt = strtol(optarg, &ep, 10);
     93 			if ((linecnt == LONG_MIN || linecnt == LONG_MAX) &&
     94 			    errno == ERANGE)
     95 				err(1, "illegal line count -- %s", optarg);
     96 			else if (*ep || linecnt <= 0)
     97 				errx(1, "illegal line count -- %s", optarg);
     98 			break;
     99 
    100 		case '?':
    101 		default:
    102 			usage();
    103 		}
    104 	argc -= optind;
    105 	argv += optind;
    106 
    107 	if (*argv)
    108 		for (first = 1; *argv; ++argv) {
    109 			if ((fp = fopen(*argv, "r")) == NULL) {
    110 				warn("%s", *argv);
    111 				eval = 1;
    112 				continue;
    113 			}
    114 			if (argc > 1) {
    115 				(void)printf("%s==> %s <==\n",
    116 				    first ? "" : "\n", *argv);
    117 				first = 0;
    118 			}
    119 			head(fp, linecnt);
    120 			(void)fclose(fp);
    121 		}
    122 	else
    123 		head(stdin, linecnt);
    124 	exit(eval);
    125 }
    126 
    127 void
    128 head(fp, cnt)
    129 	FILE *fp;
    130 	long cnt;
    131 {
    132 	int ch;
    133 
    134 	while (cnt--)
    135 		while ((ch = getc(fp)) != EOF) {
    136 			if (putchar(ch) == EOF)
    137 				err(1, "stdout");
    138 			if (ch == '\n')
    139 				break;
    140 		}
    141 }
    142 
    143 void
    144 obsolete(argv)
    145 	char *argv[];
    146 {
    147 	char *ap;
    148 
    149 	while ((ap = *++argv)) {
    150 		/* Return if "--" or not "-[0-9]*". */
    151 		if (ap[0] != '-' || ap[1] == '-' ||
    152 		    !isdigit((unsigned char)ap[1]))
    153 			return;
    154 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
    155 			err(1, "%s", "");
    156 		ap[0] = '-';
    157 		ap[1] = 'n';
    158 		(void)strcpy(ap + 2, *argv + 1);
    159 		*argv = ap;
    160 	}
    161 }
    162 
    163 void
    164 usage()
    165 {
    166 	extern char *__progname;
    167 	(void)fprintf(stderr, "Usage: %s [-n lines] [file ...]\n", __progname);
    168 	exit(1);
    169 }
    170