2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
53 #ifndef NO_UDOM_SUPPORT
54 #include <sys/socket.h>
72 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
74 static const char *filename;
76 static void usage(void) __dead2;
77 static void scanfiles(char *argv[], int cooked);
78 static void cook_cat(FILE *);
79 static void raw_cat(int);
81 #ifndef NO_UDOM_SUPPORT
82 static int udom_open(const char *path, int flags);
86 * Memory strategy threshold, in pages: if physmem is larger than this,
89 #define PHYSPAGES_THRESHOLD (32 * 1024)
91 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
92 #define BUFSIZE_MAX (2 * 1024 * 1024)
95 * Small (default) buffer size in bytes. It's inefficient for this to be
96 * smaller than MAXPHYS.
98 #define BUFSIZE_SMALL (MAXPHYS)
101 main(int argc, char *argv[])
104 struct flock stdout_lock;
106 setlocale(LC_CTYPE, "");
108 while ((ch = getopt(argc, argv, "belnstuv")) != -1)
111 bflag = nflag = 1; /* -b implies -n */
114 eflag = vflag = 1; /* -e implies -v */
126 tflag = vflag = 1; /* -t implies -v */
129 setbuf(stdout, NULL);
140 stdout_lock.l_len = 0;
141 stdout_lock.l_start = 0;
142 stdout_lock.l_type = F_WRLCK;
143 stdout_lock.l_whence = SEEK_SET;
144 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
145 err(EXIT_FAILURE, "stdout");
148 if (bflag || eflag || nflag || sflag || tflag || vflag)
162 fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n");
168 scanfiles(char *argv[], int cooked)
176 while ((path = argv[i]) != NULL || i == 0) {
177 if (path == NULL || strcmp(path, "-") == 0) {
182 fd = open(path, O_RDONLY);
183 #ifndef NO_UDOM_SUPPORT
184 if (fd < 0 && errno == EOPNOTSUPP)
185 fd = udom_open(path, O_RDONLY);
192 if (fd == STDIN_FILENO)
195 fp = fdopen(fd, "r");
201 if (fd != STDIN_FILENO)
213 int ch, gobble, line, prev;
216 /* Reset EOF condition on stdin. */
217 if (fp == stdin && feof(stdin))
221 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
232 if (!bflag || ch != '\n') {
233 (void)fprintf(stdout, "%6d\t", ++line);
237 (void)fprintf(stdout, "%6s\t", "");
244 if (eflag && putchar('$') == EOF)
246 } else if (ch == '\t') {
248 if (putchar('^') == EOF || putchar('I') == EOF)
253 (void)ungetc(ch, fp);
255 * Our getwc(3) doesn't change file position
258 if ((wch = getwc(fp)) == WEOF) {
259 if (ferror(fp) && errno == EILSEQ) {
261 /* Resync attempt. */
262 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
263 if ((ch = getc(fp)) == EOF)
270 if (!iswascii(wch) && !iswprint(wch)) {
272 if (putchar('M') == EOF || putchar('-') == EOF)
278 ch = (ch == '\177') ? '?' : (ch | 0100);
279 if (putchar('^') == EOF || putchar(ch) == EOF)
283 if (putwchar(wch) == WEOF)
288 if (putchar(ch) == EOF)
292 warn("%s", filename);
307 static char *buf = NULL;
310 wfd = fileno(stdout);
312 if (fstat(wfd, &sbuf))
314 if (S_ISREG(sbuf.st_mode)) {
315 /* If there's plenty of RAM, use a large copy buffer */
316 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
317 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
319 bsize = BUFSIZE_SMALL;
321 bsize = sbuf.st_blksize;
322 pagesize = sysconf(_SC_PAGESIZE);
324 bsize = MAX(bsize, (size_t)pagesize);
326 if ((buf = malloc(bsize)) == NULL)
327 err(1, "malloc() failure of IO buffer");
329 while ((nr = read(rfd, buf, bsize)) > 0)
330 for (off = 0; nr; nr -= nw, off += nw)
331 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
334 warn("%s", filename);
339 #ifndef NO_UDOM_SUPPORT
342 udom_open(const char *path, int flags)
344 struct addrinfo hints, *res, *res0;
345 char rpath[PATH_MAX];
350 * Construct the unix domain socket address and attempt to connect.
352 bzero(&hints, sizeof(hints));
353 hints.ai_family = AF_LOCAL;
354 if (realpath(path, rpath) == NULL)
356 error = getaddrinfo(rpath, NULL, &hints, &res0);
358 warn("%s", gai_strerror(error));
362 for (res = res0; res != NULL; res = res->ai_next) {
363 fd = socket(res->ai_family, res->ai_socktype,
369 error = connect(fd, res->ai_addr, res->ai_addrlen);
380 * handle the open flags by shutting down appropriate directions
383 switch(flags & O_ACCMODE) {
385 if (shutdown(fd, SHUT_WR) == -1)
389 if (shutdown(fd, SHUT_RD) == -1)