xref: /freebsd/bin/cat/cat.c (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kevin Fall.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #ifndef lint
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 #endif
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
46 #endif
47 #endif /* not lint */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #ifndef NO_UDOM_SUPPORT
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <netdb.h>
57 #endif
58 
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <locale.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <wchar.h>
69 #include <wctype.h>
70 
71 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
72 static int rval;
73 static const char *filename;
74 
75 static void usage(void) __dead2;
76 static void scanfiles(char *argv[], int cooked);
77 static void cook_cat(FILE *);
78 static void raw_cat(int);
79 
80 #ifndef NO_UDOM_SUPPORT
81 static int udom_open(const char *path, int flags);
82 #endif
83 
84 /*
85  * Memory strategy threshold, in pages: if physmem is larger than this,
86  * use a large buffer.
87  */
88 #define	PHYSPAGES_THRESHOLD (32 * 1024)
89 
90 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
91 #define	BUFSIZE_MAX (2 * 1024 * 1024)
92 
93 /*
94  * Small (default) buffer size in bytes. It's inefficient for this to be
95  * smaller than MAXPHYS.
96  */
97 #define	BUFSIZE_SMALL (MAXPHYS)
98 
99 
100 /*
101  * For the bootstrapped cat binary (needed for locked appending to METALOG), we
102  * disable all flags except -l and -u to avoid non-portable function calls.
103  * In the future we may instead want to write a small portable bootstrap tool
104  * that locks the output file before writing to it. However, for now
105  * bootstrapping cat without multibyte support is the simpler solution.
106  */
107 #ifdef BOOTSTRAP_CAT
108 #define SUPPORTED_FLAGS "lu"
109 #else
110 #define SUPPORTED_FLAGS "belnstuv"
111 #endif
112 
113 int
114 main(int argc, char *argv[])
115 {
116 	int ch;
117 	struct flock stdout_lock;
118 
119 	setlocale(LC_CTYPE, "");
120 
121 	while ((ch = getopt(argc, argv, SUPPORTED_FLAGS)) != -1)
122 		switch (ch) {
123 		case 'b':
124 			bflag = nflag = 1;	/* -b implies -n */
125 			break;
126 		case 'e':
127 			eflag = vflag = 1;	/* -e implies -v */
128 			break;
129 		case 'l':
130 			lflag = 1;
131 			break;
132 		case 'n':
133 			nflag = 1;
134 			break;
135 		case 's':
136 			sflag = 1;
137 			break;
138 		case 't':
139 			tflag = vflag = 1;	/* -t implies -v */
140 			break;
141 		case 'u':
142 			setbuf(stdout, NULL);
143 			break;
144 		case 'v':
145 			vflag = 1;
146 			break;
147 		default:
148 			usage();
149 		}
150 	argv += optind;
151 
152 	if (lflag) {
153 		stdout_lock.l_len = 0;
154 		stdout_lock.l_start = 0;
155 		stdout_lock.l_type = F_WRLCK;
156 		stdout_lock.l_whence = SEEK_SET;
157 		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
158 			err(EXIT_FAILURE, "stdout");
159 	}
160 
161 	if (bflag || eflag || nflag || sflag || tflag || vflag)
162 		scanfiles(argv, 1);
163 	else
164 		scanfiles(argv, 0);
165 	if (fclose(stdout))
166 		err(1, "stdout");
167 	exit(rval);
168 	/* NOTREACHED */
169 }
170 
171 static void
172 usage(void)
173 {
174 
175 	fprintf(stderr, "usage: cat [-" SUPPORTED_FLAGS "] [file ...]\n");
176 	exit(1);
177 	/* NOTREACHED */
178 }
179 
180 static void
181 scanfiles(char *argv[], int cooked)
182 {
183 	int fd, i;
184 	char *path;
185 	FILE *fp;
186 
187 	i = 0;
188 	fd = -1;
189 	while ((path = argv[i]) != NULL || i == 0) {
190 		if (path == NULL || strcmp(path, "-") == 0) {
191 			filename = "stdin";
192 			fd = STDIN_FILENO;
193 		} else {
194 			filename = path;
195 			fd = open(path, O_RDONLY);
196 #ifndef NO_UDOM_SUPPORT
197 			if (fd < 0 && errno == EOPNOTSUPP)
198 				fd = udom_open(path, O_RDONLY);
199 #endif
200 		}
201 		if (fd < 0) {
202 			warn("%s", path);
203 			rval = 1;
204 #ifndef BOOTSTRAP_CAT
205 		} else if (cooked) {
206 			if (fd == STDIN_FILENO)
207 				cook_cat(stdin);
208 			else {
209 				fp = fdopen(fd, "r");
210 				cook_cat(fp);
211 				fclose(fp);
212 			}
213 #endif
214 		} else {
215 			raw_cat(fd);
216 			if (fd != STDIN_FILENO)
217 				close(fd);
218 		}
219 		if (path == NULL)
220 			break;
221 		++i;
222 	}
223 }
224 
225 #ifndef BOOTSTRAP_CAT
226 static void
227 cook_cat(FILE *fp)
228 {
229 	int ch, gobble, line, prev;
230 	wint_t wch;
231 
232 	/* Reset EOF condition on stdin. */
233 	if (fp == stdin && feof(stdin))
234 		clearerr(stdin);
235 
236 	line = gobble = 0;
237 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
238 		if (prev == '\n') {
239 			if (sflag) {
240 				if (ch == '\n') {
241 					if (gobble)
242 						continue;
243 					gobble = 1;
244 				} else
245 					gobble = 0;
246 			}
247 			if (nflag) {
248 				if (!bflag || ch != '\n') {
249 					(void)fprintf(stdout, "%6d\t", ++line);
250 					if (ferror(stdout))
251 						break;
252 				} else if (eflag) {
253 					(void)fprintf(stdout, "%6s\t", "");
254 					if (ferror(stdout))
255 						break;
256 				}
257 			}
258 		}
259 		if (ch == '\n') {
260 			if (eflag && putchar('$') == EOF)
261 				break;
262 		} else if (ch == '\t') {
263 			if (tflag) {
264 				if (putchar('^') == EOF || putchar('I') == EOF)
265 					break;
266 				continue;
267 			}
268 		} else if (vflag) {
269 			(void)ungetc(ch, fp);
270 			/*
271 			 * Our getwc(3) doesn't change file position
272 			 * on error.
273 			 */
274 			if ((wch = getwc(fp)) == WEOF) {
275 				if (ferror(fp) && errno == EILSEQ) {
276 					clearerr(fp);
277 					/* Resync attempt. */
278 					memset(&fp->_mbstate, 0, sizeof(mbstate_t));
279 					if ((ch = getc(fp)) == EOF)
280 						break;
281 					wch = ch;
282 					goto ilseq;
283 				} else
284 					break;
285 			}
286 			if (!iswascii(wch) && !iswprint(wch)) {
287 ilseq:
288 				if (putchar('M') == EOF || putchar('-') == EOF)
289 					break;
290 				wch = toascii(wch);
291 			}
292 			if (iswcntrl(wch)) {
293 				ch = toascii(wch);
294 				ch = (ch == '\177') ? '?' : (ch | 0100);
295 				if (putchar('^') == EOF || putchar(ch) == EOF)
296 					break;
297 				continue;
298 			}
299 			if (putwchar(wch) == WEOF)
300 				break;
301 			ch = -1;
302 			continue;
303 		}
304 		if (putchar(ch) == EOF)
305 			break;
306 	}
307 	if (ferror(fp)) {
308 		warn("%s", filename);
309 		rval = 1;
310 		clearerr(fp);
311 	}
312 	if (ferror(stdout))
313 		err(1, "stdout");
314 }
315 #endif /* BOOTSTRAP_CAT */
316 
317 static void
318 raw_cat(int rfd)
319 {
320 	long pagesize;
321 	int off, wfd;
322 	ssize_t nr, nw;
323 	static size_t bsize;
324 	static char *buf = NULL;
325 	struct stat sbuf;
326 
327 	wfd = fileno(stdout);
328 	if (buf == NULL) {
329 		if (fstat(wfd, &sbuf))
330 			err(1, "stdout");
331 		if (S_ISREG(sbuf.st_mode)) {
332 			/* If there's plenty of RAM, use a large copy buffer */
333 			if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
334 				bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
335 			else
336 				bsize = BUFSIZE_SMALL;
337 		} else {
338 			bsize = sbuf.st_blksize;
339 			pagesize = sysconf(_SC_PAGESIZE);
340 			if (pagesize > 0)
341 				bsize = MAX(bsize, (size_t)pagesize);
342 		}
343 		if ((buf = malloc(bsize)) == NULL)
344 			err(1, "malloc() failure of IO buffer");
345 	}
346 	while ((nr = read(rfd, buf, bsize)) > 0)
347 		for (off = 0; nr; nr -= nw, off += nw)
348 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
349 				err(1, "stdout");
350 	if (nr < 0) {
351 		warn("%s", filename);
352 		rval = 1;
353 	}
354 }
355 
356 #ifndef NO_UDOM_SUPPORT
357 
358 static int
359 udom_open(const char *path, int flags)
360 {
361 	struct addrinfo hints, *res, *res0;
362 	char rpath[PATH_MAX];
363 	int fd = -1;
364 	int error;
365 
366 	/*
367 	 * Construct the unix domain socket address and attempt to connect.
368 	 */
369 	bzero(&hints, sizeof(hints));
370 	hints.ai_family = AF_LOCAL;
371 	if (realpath(path, rpath) == NULL)
372 		return (-1);
373 	error = getaddrinfo(rpath, NULL, &hints, &res0);
374 	if (error) {
375 		warn("%s", gai_strerror(error));
376 		errno = EINVAL;
377 		return (-1);
378 	}
379 	for (res = res0; res != NULL; res = res->ai_next) {
380 		fd = socket(res->ai_family, res->ai_socktype,
381 		    res->ai_protocol);
382 		if (fd < 0) {
383 			freeaddrinfo(res0);
384 			return (-1);
385 		}
386 		error = connect(fd, res->ai_addr, res->ai_addrlen);
387 		if (error == 0)
388 			break;
389 		else {
390 			close(fd);
391 			fd = -1;
392 		}
393 	}
394 	freeaddrinfo(res0);
395 
396 	/*
397 	 * handle the open flags by shutting down appropriate directions
398 	 */
399 	if (fd >= 0) {
400 		switch(flags & O_ACCMODE) {
401 		case O_RDONLY:
402 			if (shutdown(fd, SHUT_WR) == -1)
403 				warn(NULL);
404 			break;
405 		case O_WRONLY:
406 			if (shutdown(fd, SHUT_RD) == -1)
407 				warn(NULL);
408 			break;
409 		default:
410 			break;
411 		}
412 	}
413 	return (fd);
414 }
415 
416 #endif
417