2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char const copyright[] =
36 "@(#) Copyright (c) 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #ifndef NO_UDOM_SUPPORT
52 #include <sys/socket.h>
70 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
72 static const char *filename;
74 static void usage(void) __dead2;
75 static void scanfiles(char *argv[], int cooked);
76 static void cook_cat(FILE *);
77 static void raw_cat(int);
79 #ifndef NO_UDOM_SUPPORT
80 static int udom_open(const char *path, int flags);
84 * Memory strategy threshold, in pages: if physmem is larger than this,
87 #define PHYSPAGES_THRESHOLD (32 * 1024)
89 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
90 #define BUFSIZE_MAX (2 * 1024 * 1024)
93 * Small (default) buffer size in bytes. It's inefficient for this to be
94 * smaller than MAXPHYS.
96 #define BUFSIZE_SMALL (MAXPHYS)
99 main(int argc, char *argv[])
102 struct flock stdout_lock;
104 setlocale(LC_CTYPE, "");
106 while ((ch = getopt(argc, argv, "belnstuv")) != -1)
109 bflag = nflag = 1; /* -b implies -n */
112 eflag = vflag = 1; /* -e implies -v */
124 tflag = vflag = 1; /* -t implies -v */
127 setbuf(stdout, NULL);
138 stdout_lock.l_len = 0;
139 stdout_lock.l_start = 0;
140 stdout_lock.l_type = F_WRLCK;
141 stdout_lock.l_whence = SEEK_SET;
142 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
143 err(EXIT_FAILURE, "stdout");
146 if (bflag || eflag || nflag || sflag || tflag || vflag)
160 fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n");
166 scanfiles(char *argv[], int cooked)
174 while ((path = argv[i]) != NULL || i == 0) {
175 if (path == NULL || strcmp(path, "-") == 0) {
180 fd = open(path, O_RDONLY);
181 #ifndef NO_UDOM_SUPPORT
182 if (fd < 0 && errno == EOPNOTSUPP)
183 fd = udom_open(path, O_RDONLY);
190 if (fd == STDIN_FILENO)
193 fp = fdopen(fd, "r");
199 if (fd != STDIN_FILENO)
211 int ch, gobble, line, prev;
214 /* Reset EOF condition on stdin. */
215 if (fp == stdin && feof(stdin))
219 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
229 if (nflag && (!bflag || ch != '\n')) {
230 (void)fprintf(stdout, "%6d\t", ++line);
236 if (eflag && putchar('$') == EOF)
238 } else if (ch == '\t') {
240 if (putchar('^') == EOF || putchar('I') == EOF)
245 (void)ungetc(ch, fp);
247 * Our getwc(3) doesn't change file position
250 if ((wch = getwc(fp)) == WEOF) {
251 if (ferror(fp) && errno == EILSEQ) {
253 /* Resync attempt. */
254 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
255 if ((ch = getc(fp)) == EOF)
262 if (!iswascii(wch) && !iswprint(wch)) {
264 if (putchar('M') == EOF || putchar('-') == EOF)
270 ch = (ch == '\177') ? '?' : (ch | 0100);
271 if (putchar('^') == EOF || putchar(ch) == EOF)
275 if (putwchar(wch) == WEOF)
280 if (putchar(ch) == EOF)
284 warn("%s", filename);
298 static char *buf = NULL;
301 wfd = fileno(stdout);
303 if (fstat(wfd, &sbuf))
305 if (S_ISREG(sbuf.st_mode)) {
306 /* If there's plenty of RAM, use a large copy buffer */
307 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
308 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
310 bsize = BUFSIZE_SMALL;
312 bsize = MAX(sbuf.st_blksize,
313 (blksize_t)sysconf(_SC_PAGESIZE));
314 if ((buf = malloc(bsize)) == NULL)
315 err(1, "malloc() failure of IO buffer");
317 while ((nr = read(rfd, buf, bsize)) > 0)
318 for (off = 0; nr; nr -= nw, off += nw)
319 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
322 warn("%s", filename);
327 #ifndef NO_UDOM_SUPPORT
330 udom_open(const char *path, int flags)
332 struct addrinfo hints, *res, *res0;
333 char rpath[PATH_MAX];
338 * Construct the unix domain socket address and attempt to connect.
340 bzero(&hints, sizeof(hints));
341 hints.ai_family = AF_LOCAL;
342 if (realpath(path, rpath) == NULL)
344 error = getaddrinfo(rpath, NULL, &hints, &res0);
346 warn("%s", gai_strerror(error));
350 for (res = res0; res != NULL; res = res->ai_next) {
351 fd = socket(res->ai_family, res->ai_socktype,
357 error = connect(fd, res->ai_addr, res->ai_addrlen);
368 * handle the open flags by shutting down appropriate directions
371 switch(flags & O_ACCMODE) {
373 if (shutdown(fd, SHUT_WR) == -1)
377 if (shutdown(fd, SHUT_RD) == -1)