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