Home | History | Annotate | Line # | Download | only in head
head.c revision 1.23
      1  1.23     joerg /*	$NetBSD: head.c,v 1.23 2010/03/31 21:55:23 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.23     joerg __RCSID("$NetBSD: head.c,v 1.23 2010/03/31 21:55:23 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.22     joerg #include <inttypes.h>
     52   1.9    kleink #include <limits.h>
     53   1.9    kleink #include <locale.h>
     54   1.1       cgd #include <stdio.h>
     55   1.2       jtc #include <stdlib.h>
     56   1.7       mrg #include <string.h>
     57   1.5       jtc #include <unistd.h>
     58   1.2       jtc 
     59   1.1       cgd /*
     60   1.1       cgd  * head - give the first few lines of a stream or of each of a set of files
     61   1.1       cgd  *
     62   1.1       cgd  * Bill Joy UCB August 24, 1977
     63   1.1       cgd  */
     64   1.1       cgd 
     65  1.22     joerg static void head(FILE *, intmax_t, intmax_t);
     66  1.20     joerg static void obsolete(char *[]);
     67  1.20     joerg __dead static void usage(void);
     68   1.7       mrg 
     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.22     joerg 	uintmax_t linecnt;
     77  1.22     joerg 	uintmax_t bytecnt;
     78   1.7       mrg 	char *ep;
     79  1.21     joerg 	int eval = 0;
     80  1.18       mrg 	int qflag = 0;
     81  1.18       mrg 	int vflag = 0;
     82   1.7       mrg 
     83   1.9    kleink 	(void)setlocale(LC_ALL, "");
     84   1.7       mrg 	obsolete(argv);
     85   1.7       mrg 	linecnt = 10;
     86  1.18       mrg 	bytecnt = 0;
     87  1.18       mrg 	while ((ch = getopt(argc, argv, "c:n:qv")) != -1)
     88   1.7       mrg 		switch(ch) {
     89  1.18       mrg 		case 'c':
     90  1.18       mrg 			errno = 0;
     91  1.22     joerg 			bytecnt = strtoimax(optarg, &ep, 10);
     92  1.22     joerg 			if ((bytecnt == INTMAX_MAX && errno == ERANGE) ||
     93  1.22     joerg 			    *ep || bytecnt <= 0)
     94  1.23     joerg 				errx(1, "illegal byte count -- %s", optarg);
     95  1.18       mrg 			break;
     96  1.18       mrg 
     97   1.2       jtc 		case 'n':
     98  1.15     lukem 			errno = 0;
     99  1.22     joerg 			linecnt = strtoimax(optarg, &ep, 10);
    100  1.22     joerg 			if ((linecnt == INTMAX_MAX && errno == ERANGE) ||
    101  1.22     joerg 			    *ep || linecnt <= 0)
    102  1.23     joerg 				errx(1, "illegal line count -- %s", optarg);
    103   1.2       jtc 			break;
    104   1.2       jtc 
    105  1.18       mrg 		case 'q':
    106  1.18       mrg 			qflag = 1;
    107  1.18       mrg 			vflag = 0;
    108  1.18       mrg 			break;
    109  1.18       mrg 
    110  1.18       mrg 		case 'v':
    111  1.18       mrg 			qflag = 0;
    112  1.18       mrg 			vflag = 1;
    113  1.18       mrg 			break;
    114  1.18       mrg 
    115   1.7       mrg 		case '?':
    116   1.2       jtc 		default:
    117   1.7       mrg 			usage();
    118   1.2       jtc 		}
    119   1.7       mrg 	argc -= optind;
    120   1.7       mrg 	argv += optind;
    121   1.2       jtc 
    122   1.7       mrg 	if (*argv)
    123   1.7       mrg 		for (first = 1; *argv; ++argv) {
    124   1.7       mrg 			if ((fp = fopen(*argv, "r")) == NULL) {
    125   1.9    kleink 				warn("%s", *argv);
    126   1.9    kleink 				eval = 1;
    127   1.7       mrg 				continue;
    128   1.1       cgd 			}
    129  1.18       mrg 			if (vflag || (qflag == 0 && argc > 1)) {
    130   1.7       mrg 				(void)printf("%s==> %s <==\n",
    131   1.7       mrg 				    first ? "" : "\n", *argv);
    132   1.7       mrg 				first = 0;
    133   1.1       cgd 			}
    134  1.18       mrg 			head(fp, linecnt, bytecnt);
    135   1.7       mrg 			(void)fclose(fp);
    136   1.1       cgd 		}
    137   1.7       mrg 	else
    138  1.18       mrg 		head(stdin, linecnt, bytecnt);
    139   1.7       mrg 	exit(eval);
    140   1.7       mrg }
    141   1.7       mrg 
    142  1.20     joerg static void
    143  1.22     joerg head(FILE *fp, intmax_t cnt, intmax_t bytecnt)
    144   1.7       mrg {
    145  1.22     joerg 	char buf[65536];
    146  1.22     joerg 	size_t len, rv, rv2;
    147   1.8     lukem 	int ch;
    148   1.7       mrg 
    149  1.22     joerg 	if (bytecnt) {
    150  1.22     joerg 		while (bytecnt) {
    151  1.22     joerg 			len = sizeof(buf);
    152  1.22     joerg 			if (bytecnt > (intmax_t)sizeof(buf))
    153  1.22     joerg 				len = sizeof(buf);
    154  1.22     joerg 			else
    155  1.22     joerg 				len = bytecnt;
    156  1.22     joerg 			rv = fread(buf, 1, len, fp);
    157  1.22     joerg 			if (rv == 0)
    158  1.22     joerg 				break; /* Distinguish EOF and error? */
    159  1.22     joerg 			rv2 = fwrite(buf, 1, rv, stdout);
    160  1.22     joerg 			if (rv2 != rv) {
    161  1.22     joerg 				if (feof(stdout))
    162  1.22     joerg 					errx(1, "EOF on stdout");
    163  1.22     joerg 				else
    164  1.22     joerg 					err(1, "failure writing to stdout");
    165  1.22     joerg 			}
    166  1.22     joerg 			bytecnt -= rv;
    167  1.22     joerg 		}
    168  1.22     joerg 	} else {
    169   1.7       mrg 		while ((ch = getc(fp)) != EOF) {
    170   1.7       mrg 			if (putchar(ch) == EOF)
    171   1.9    kleink 				err(1, "stdout");
    172  1.22     joerg 			if (ch == '\n' && --cnt == 0)
    173   1.7       mrg 				break;
    174   1.7       mrg 		}
    175  1.22     joerg 	}
    176   1.7       mrg }
    177   1.7       mrg 
    178  1.20     joerg static void
    179  1.20     joerg obsolete(char *argv[])
    180   1.7       mrg {
    181   1.7       mrg 	char *ap;
    182   1.7       mrg 
    183   1.7       mrg 	while ((ap = *++argv)) {
    184   1.7       mrg 		/* Return if "--" or not "-[0-9]*". */
    185  1.11  christos 		if (ap[0] != '-' || ap[1] == '-' ||
    186  1.11  christos 		    !isdigit((unsigned char)ap[1]))
    187   1.7       mrg 			return;
    188   1.7       mrg 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
    189  1.12  drochner 			err(1, NULL);
    190   1.7       mrg 		ap[0] = '-';
    191   1.7       mrg 		ap[1] = 'n';
    192   1.7       mrg 		(void)strcpy(ap + 2, *argv + 1);
    193   1.7       mrg 		*argv = ap;
    194   1.1       cgd 	}
    195   1.1       cgd }
    196   1.2       jtc 
    197  1.20     joerg static void
    198  1.20     joerg usage(void)
    199   1.2       jtc {
    200  1.14       cgd 
    201  1.17      jmmv 	(void)fprintf(stderr, "usage: %s [-n lines] [file ...]\n",
    202  1.14       cgd 	    getprogname());
    203   1.2       jtc 	exit(1);
    204   1.2       jtc }
    205