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