xref: /freebsd/bin/cat/cat.c (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
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 int
100 main(int argc, char *argv[])
101 {
102 	int ch;
103 	struct flock stdout_lock;
104 
105 	setlocale(LC_CTYPE, "");
106 
107 	while ((ch = getopt(argc, argv, "belnstuv")) != -1)
108 		switch (ch) {
109 		case 'b':
110 			bflag = nflag = 1;	/* -b implies -n */
111 			break;
112 		case 'e':
113 			eflag = vflag = 1;	/* -e implies -v */
114 			break;
115 		case 'l':
116 			lflag = 1;
117 			break;
118 		case 'n':
119 			nflag = 1;
120 			break;
121 		case 's':
122 			sflag = 1;
123 			break;
124 		case 't':
125 			tflag = vflag = 1;	/* -t implies -v */
126 			break;
127 		case 'u':
128 			setbuf(stdout, NULL);
129 			break;
130 		case 'v':
131 			vflag = 1;
132 			break;
133 		default:
134 			usage();
135 		}
136 	argv += optind;
137 
138 	if (lflag) {
139 		stdout_lock.l_len = 0;
140 		stdout_lock.l_start = 0;
141 		stdout_lock.l_type = F_WRLCK;
142 		stdout_lock.l_whence = SEEK_SET;
143 		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
144 			err(EXIT_FAILURE, "stdout");
145 	}
146 
147 	if (bflag || eflag || nflag || sflag || tflag || vflag)
148 		scanfiles(argv, 1);
149 	else
150 		scanfiles(argv, 0);
151 	if (fclose(stdout))
152 		err(1, "stdout");
153 	exit(rval);
154 	/* NOTREACHED */
155 }
156 
157 static void
158 usage(void)
159 {
160 
161 	fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n");
162 	exit(1);
163 	/* NOTREACHED */
164 }
165 
166 static void
167 scanfiles(char *argv[], int cooked)
168 {
169 	int fd, i;
170 	char *path;
171 	FILE *fp;
172 
173 	i = 0;
174 	fd = -1;
175 	while ((path = argv[i]) != NULL || i == 0) {
176 		if (path == NULL || strcmp(path, "-") == 0) {
177 			filename = "stdin";
178 			fd = STDIN_FILENO;
179 		} else {
180 			filename = path;
181 			fd = open(path, O_RDONLY);
182 #ifndef NO_UDOM_SUPPORT
183 			if (fd < 0 && errno == EOPNOTSUPP)
184 				fd = udom_open(path, O_RDONLY);
185 #endif
186 		}
187 		if (fd < 0) {
188 			warn("%s", path);
189 			rval = 1;
190 		} else if (cooked) {
191 			if (fd == STDIN_FILENO)
192 				cook_cat(stdin);
193 			else {
194 				fp = fdopen(fd, "r");
195 				cook_cat(fp);
196 				fclose(fp);
197 			}
198 		} else {
199 			raw_cat(fd);
200 			if (fd != STDIN_FILENO)
201 				close(fd);
202 		}
203 		if (path == NULL)
204 			break;
205 		++i;
206 	}
207 }
208 
209 static void
210 cook_cat(FILE *fp)
211 {
212 	int ch, gobble, line, prev;
213 	wint_t wch;
214 
215 	/* Reset EOF condition on stdin. */
216 	if (fp == stdin && feof(stdin))
217 		clearerr(stdin);
218 
219 	line = gobble = 0;
220 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
221 		if (prev == '\n') {
222 			if (sflag) {
223 				if (ch == '\n') {
224 					if (gobble)
225 						continue;
226 					gobble = 1;
227 				} else
228 					gobble = 0;
229 			}
230 			if (nflag) {
231 				if (!bflag || ch != '\n') {
232 					(void)fprintf(stdout, "%6d\t", ++line);
233 					if (ferror(stdout))
234 						break;
235 				} else if (eflag) {
236 					(void)fprintf(stdout, "%6s\t", "");
237 					if (ferror(stdout))
238 						break;
239 				}
240 			}
241 		}
242 		if (ch == '\n') {
243 			if (eflag && putchar('$') == EOF)
244 				break;
245 		} else if (ch == '\t') {
246 			if (tflag) {
247 				if (putchar('^') == EOF || putchar('I') == EOF)
248 					break;
249 				continue;
250 			}
251 		} else if (vflag) {
252 			(void)ungetc(ch, fp);
253 			/*
254 			 * Our getwc(3) doesn't change file position
255 			 * on error.
256 			 */
257 			if ((wch = getwc(fp)) == WEOF) {
258 				if (ferror(fp) && errno == EILSEQ) {
259 					clearerr(fp);
260 					/* Resync attempt. */
261 					memset(&fp->_mbstate, 0, sizeof(mbstate_t));
262 					if ((ch = getc(fp)) == EOF)
263 						break;
264 					wch = ch;
265 					goto ilseq;
266 				} else
267 					break;
268 			}
269 			if (!iswascii(wch) && !iswprint(wch)) {
270 ilseq:
271 				if (putchar('M') == EOF || putchar('-') == EOF)
272 					break;
273 				wch = toascii(wch);
274 			}
275 			if (iswcntrl(wch)) {
276 				ch = toascii(wch);
277 				ch = (ch == '\177') ? '?' : (ch | 0100);
278 				if (putchar('^') == EOF || putchar(ch) == EOF)
279 					break;
280 				continue;
281 			}
282 			if (putwchar(wch) == WEOF)
283 				break;
284 			ch = -1;
285 			continue;
286 		}
287 		if (putchar(ch) == EOF)
288 			break;
289 	}
290 	if (ferror(fp)) {
291 		warn("%s", filename);
292 		rval = 1;
293 		clearerr(fp);
294 	}
295 	if (ferror(stdout))
296 		err(1, "stdout");
297 }
298 
299 static void
300 raw_cat(int rfd)
301 {
302 	long pagesize;
303 	int off, wfd;
304 	ssize_t nr, nw;
305 	static size_t bsize;
306 	static char *buf = NULL;
307 	struct stat sbuf;
308 
309 	wfd = fileno(stdout);
310 	if (buf == NULL) {
311 		if (fstat(wfd, &sbuf))
312 			err(1, "stdout");
313 		if (S_ISREG(sbuf.st_mode)) {
314 			/* If there's plenty of RAM, use a large copy buffer */
315 			if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
316 				bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
317 			else
318 				bsize = BUFSIZE_SMALL;
319 		} else {
320 			bsize = sbuf.st_blksize;
321 			pagesize = sysconf(_SC_PAGESIZE);
322 			if (pagesize > 0)
323 				bsize = MAX(bsize, (size_t)pagesize);
324 		}
325 		if ((buf = malloc(bsize)) == NULL)
326 			err(1, "malloc() failure of IO buffer");
327 	}
328 	while ((nr = read(rfd, buf, bsize)) > 0)
329 		for (off = 0; nr; nr -= nw, off += nw)
330 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
331 				err(1, "stdout");
332 	if (nr < 0) {
333 		warn("%s", filename);
334 		rval = 1;
335 	}
336 }
337 
338 #ifndef NO_UDOM_SUPPORT
339 
340 static int
341 udom_open(const char *path, int flags)
342 {
343 	struct addrinfo hints, *res, *res0;
344 	char rpath[PATH_MAX];
345 	int fd = -1;
346 	int error;
347 
348 	/*
349 	 * Construct the unix domain socket address and attempt to connect.
350 	 */
351 	bzero(&hints, sizeof(hints));
352 	hints.ai_family = AF_LOCAL;
353 	if (realpath(path, rpath) == NULL)
354 		return (-1);
355 	error = getaddrinfo(rpath, NULL, &hints, &res0);
356 	if (error) {
357 		warn("%s", gai_strerror(error));
358 		errno = EINVAL;
359 		return (-1);
360 	}
361 	for (res = res0; res != NULL; res = res->ai_next) {
362 		fd = socket(res->ai_family, res->ai_socktype,
363 		    res->ai_protocol);
364 		if (fd < 0) {
365 			freeaddrinfo(res0);
366 			return (-1);
367 		}
368 		error = connect(fd, res->ai_addr, res->ai_addrlen);
369 		if (error == 0)
370 			break;
371 		else {
372 			close(fd);
373 			fd = -1;
374 		}
375 	}
376 	freeaddrinfo(res0);
377 
378 	/*
379 	 * handle the open flags by shutting down appropriate directions
380 	 */
381 	if (fd >= 0) {
382 		switch(flags & O_ACCMODE) {
383 		case O_RDONLY:
384 			if (shutdown(fd, SHUT_WR) == -1)
385 				warn(NULL);
386 			break;
387 		case O_WRONLY:
388 			if (shutdown(fd, SHUT_RD) == -1)
389 				warn(NULL);
390 			break;
391 		default:
392 			break;
393 		}
394 	}
395 	return (fd);
396 }
397 
398 #endif
399