xref: /freebsd/usr.bin/head/head.c (revision 3c5ba95ad12285ad37c182a4bfc1b240ec6d18a7)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/types.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <getopt.h>
51 #include <inttypes.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 /*
58  * head - give the first few lines of a stream or of each of a set of files
59  *
60  * Bill Joy UCB August 24, 1977
61  */
62 
63 static void head(FILE *, int);
64 static void head_bytes(FILE *, off_t);
65 static void obsolete(char *[]);
66 static void usage(void);
67 
68 static const struct option long_opts[] =
69 {
70 	{"bytes",	required_argument,	NULL, 'c'},
71 	{"lines",	required_argument,	NULL, 'n'},
72 	{NULL,		no_argument,		NULL, 0}
73 };
74 
75 int
76 main(int argc, char *argv[])
77 {
78 	int ch;
79 	FILE *fp;
80 	int first, linecnt = -1, eval = 0;
81 	off_t bytecnt = -1;
82 	char *ep;
83 
84 	obsolete(argv);
85 	while ((ch = getopt_long(argc, argv, "+n:c:", long_opts, NULL)) != -1)
86 		switch(ch) {
87 		case 'c':
88 			bytecnt = strtoimax(optarg, &ep, 10);
89 			if (*ep || bytecnt <= 0)
90 				errx(1, "illegal byte count -- %s", optarg);
91 			break;
92 		case 'n':
93 			linecnt = strtol(optarg, &ep, 10);
94 			if (*ep || linecnt <= 0)
95 				errx(1, "illegal line count -- %s", optarg);
96 			break;
97 		case '?':
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	if (linecnt != -1 && bytecnt != -1)
105 		errx(1, "can't combine line and byte counts");
106 	if (linecnt == -1 )
107 		linecnt = 10;
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 			if (bytecnt == -1)
121 				head(fp, linecnt);
122 			else
123 				head_bytes(fp, bytecnt);
124 			(void)fclose(fp);
125 		}
126 	} else if (bytecnt == -1)
127 		head(stdin, linecnt);
128 	else
129 		head_bytes(stdin, bytecnt);
130 
131 	exit(eval);
132 }
133 
134 static void
135 head(FILE *fp, int cnt)
136 {
137 	char *cp;
138 	size_t error, readlen;
139 
140 	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
141 		error = fwrite(cp, sizeof(char), readlen, stdout);
142 		if (error != readlen)
143 			err(1, "stdout");
144 		cnt--;
145 	}
146 }
147 
148 static void
149 head_bytes(FILE *fp, off_t cnt)
150 {
151 	char buf[4096];
152 	size_t readlen;
153 
154 	while (cnt) {
155 		if ((uintmax_t)cnt < sizeof(buf))
156 			readlen = cnt;
157 		else
158 			readlen = sizeof(buf);
159 		readlen = fread(buf, sizeof(char), readlen, fp);
160 		if (readlen == 0)
161 			break;
162 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
163 			err(1, "stdout");
164 		cnt -= readlen;
165 	}
166 }
167 
168 static void
169 obsolete(char *argv[])
170 {
171 	char *ap;
172 
173 	while ((ap = *++argv)) {
174 		/* Return if "--" or not "-[0-9]*". */
175 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
176 			return;
177 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
178 			err(1, NULL);
179 		ap[0] = '-';
180 		ap[1] = 'n';
181 		(void)strcpy(ap + 2, *argv + 1);
182 		*argv = ap;
183 	}
184 }
185 
186 static void
187 usage(void)
188 {
189 
190 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
191 	exit(1);
192 }
193