Home | History | Annotate | Line # | Download | only in head
head.c revision 1.15
      1 /*	$NetBSD: head.c,v 1.15 2003/04/18 03:21:01 lukem 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.15 2003/04/18 03:21:01 lukem 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 			errno = 0;
     93 			linecnt = strtol(optarg, &ep, 10);
     94 			if ((linecnt == LONG_MIN || linecnt == LONG_MAX) &&
     95 			    errno == ERANGE)
     96 				err(1, "illegal line count -- %s", optarg);
     97 			else if (*ep || linecnt <= 0)
     98 				errx(1, "illegal line count -- %s", optarg);
     99 			break;
    100 
    101 		case '?':
    102 		default:
    103 			usage();
    104 		}
    105 	argc -= optind;
    106 	argv += optind;
    107 
    108 	if (*argv)
    109 		for (first = 1; *argv; ++argv) {
    110 			if ((fp = fopen(*argv, "r")) == NULL) {
    111 				warn("%s", *argv);
    112 				eval = 1;
    113 				continue;
    114 			}
    115 			if (argc > 1) {
    116 				(void)printf("%s==> %s <==\n",
    117 				    first ? "" : "\n", *argv);
    118 				first = 0;
    119 			}
    120 			head(fp, linecnt);
    121 			(void)fclose(fp);
    122 		}
    123 	else
    124 		head(stdin, linecnt);
    125 	exit(eval);
    126 }
    127 
    128 void
    129 head(fp, cnt)
    130 	FILE *fp;
    131 	long cnt;
    132 {
    133 	int ch;
    134 
    135 	while (cnt--)
    136 		while ((ch = getc(fp)) != EOF) {
    137 			if (putchar(ch) == EOF)
    138 				err(1, "stdout");
    139 			if (ch == '\n')
    140 				break;
    141 		}
    142 }
    143 
    144 void
    145 obsolete(argv)
    146 	char *argv[];
    147 {
    148 	char *ap;
    149 
    150 	while ((ap = *++argv)) {
    151 		/* Return if "--" or not "-[0-9]*". */
    152 		if (ap[0] != '-' || ap[1] == '-' ||
    153 		    !isdigit((unsigned char)ap[1]))
    154 			return;
    155 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
    156 			err(1, NULL);
    157 		ap[0] = '-';
    158 		ap[1] = 'n';
    159 		(void)strcpy(ap + 2, *argv + 1);
    160 		*argv = ap;
    161 	}
    162 }
    163 
    164 void
    165 usage()
    166 {
    167 
    168 	(void)fprintf(stderr, "Usage: %s [-n lines] [file ...]\n",
    169 	    getprogname());
    170 	exit(1);
    171 }
    172