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