need to change iphlen earlier; icmp_error needs original ip_len; cleanups
[unix-history] / usr / src / usr.bin / head / head.c
CommitLineData
22e155fc 1/*
c482e60f 2 * Copyright (c) 1980, 1987 Regents of the University of California.
33e97575
KB
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
22e155fc
DF
6 */
7
8#ifndef lint
9char copyright[] =
c482e60f 10"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
22e155fc 11 All rights reserved.\n";
33e97575 12#endif /* not lint */
22e155fc
DF
13
14#ifndef lint
f15db449 15static char sccsid[] = "@(#)head.c 5.5 (Berkeley) %G%";
33e97575 16#endif /* not lint */
22e155fc 17
9c87fed6 18#include <stdio.h>
c482e60f 19#include <ctype.h>
9c87fed6
BJ
20/*
21 * head - give the first few lines of a stream or of each of a set of files
22 *
23 * Bill Joy UCB August 24, 1977
24 */
25
c482e60f
KB
26main(argc, argv)
27 int argc;
28 char **argv;
9c87fed6 29{
c482e60f
KB
30 register int ch, cnt;
31 int firsttime, linecnt = 10;
9c87fed6 32
c482e60f
KB
33 if (argc > 1 && argv[1][0] == '-') {
34 if (!isdigit(argv[1][1])) {
35 fprintf(stderr, "head: illegal option -- %c\n", argv[1][1]);
36 goto usage;
37 }
38 if ((linecnt = atoi(argv[1] + 1)) < 0) {
39usage: fputs("usage: head [-line_count] [file ...]\n", stderr);
40 exit(1);
41 }
42 --argc; ++argv;
43 }
44 /* setlinebuf(stdout); */
45 for (firsttime = 1, --argc, ++argv;; firsttime = 0) {
46 if (!*argv) {
47 if (!firsttime)
48 exit(0);
9c87fed6 49 }
c482e60f
KB
50 else {
51 if (!freopen(*argv, "r", stdin)) {
52 fprintf(stderr, "head: can't read %s.\n", *argv);
9c87fed6
BJ
53 exit(1);
54 }
c482e60f
KB
55 if (argc > 1) {
56 if (!firsttime)
57 putchar('\n');
58 printf("==> %s <==\n", *argv);
59 }
60 ++argv;
61 }
62 for (cnt = linecnt; cnt; --cnt)
63 while ((ch = getchar()) != EOF)
64 if (putchar(ch) == '\n')
65 break;
9c87fed6 66 }
c482e60f 67 /*NOTREACHED*/
9c87fed6 68}