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 * 4. 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>
69 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
71 static const char *filename;
73 static void usage(void) __dead2;
74 static void scanfiles(char *argv[], int cooked);
75 static void cook_cat(FILE *);
76 static void raw_cat(int);
78 #ifndef NO_UDOM_SUPPORT
79 static int udom_open(const char *path, int flags);
83 * Memory strategy threshold, in pages: if physmem is larger than this,
86 #define PHYSPAGES_THRESHOLD (32 * 1024)
88 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
89 #define BUFSIZE_MAX (2 * 1024 * 1024)
92 * Small (default) buffer size in bytes. It's inefficient for this to be
93 * smaller than MAXPHYS.
95 #define BUFSIZE_SMALL (MAXPHYS)
98 main(int argc, char *argv[])
101 struct flock stdout_lock;
103 setlocale(LC_CTYPE, "");
105 while ((ch = getopt(argc, argv, "belnstuv")) != -1)
108 bflag = nflag = 1; /* -b implies -n */
111 eflag = vflag = 1; /* -e implies -v */
123 tflag = vflag = 1; /* -t implies -v */
126 setbuf(stdout, NULL);
137 stdout_lock.l_len = 0;
138 stdout_lock.l_start = 0;
139 stdout_lock.l_type = F_WRLCK;
140 stdout_lock.l_whence = SEEK_SET;
141 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
142 err(EXIT_FAILURE, "stdout");
145 if (bflag || eflag || nflag || sflag || tflag || vflag)
159 fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n");
165 scanfiles(char *argv[], int cooked)
173 while ((path = argv[i]) != NULL || i == 0) {
174 if (path == NULL || strcmp(path, "-") == 0) {
179 fd = open(path, O_RDONLY);
180 #ifndef NO_UDOM_SUPPORT
181 if (fd < 0 && errno == EOPNOTSUPP)
182 fd = udom_open(path, O_RDONLY);
189 if (fd == STDIN_FILENO)
192 fp = fdopen(fd, "r");
198 if (fd != STDIN_FILENO)
210 int ch, gobble, line, prev;
213 /* Reset EOF condition on stdin. */
214 if (fp == stdin && feof(stdin))
218 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
228 if (nflag && (!bflag || ch != '\n')) {
229 (void)fprintf(stdout, "%6d\t", ++line);
235 if (eflag && putchar('$') == EOF)
237 } else if (ch == '\t') {
239 if (putchar('^') == EOF || putchar('I') == EOF)
244 (void)ungetc(ch, fp);
246 * Our getwc(3) doesn't change file position
249 if ((wch = getwc(fp)) == WEOF) {
250 if (ferror(fp) && errno == EILSEQ) {
252 /* Resync attempt. */
253 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
254 if ((ch = getc(fp)) == EOF)
261 if (!iswascii(wch) && !iswprint(wch)) {
263 if (putchar('M') == EOF || putchar('-') == EOF)
269 ch = (ch == '\177') ? '?' : (ch | 0100);
270 if (putchar('^') == EOF || putchar(ch) == EOF)
274 if (putwchar(wch) == WEOF)
279 if (putchar(ch) == EOF)
283 warn("%s", filename);
297 static char *buf = NULL;
300 wfd = fileno(stdout);
302 if (fstat(wfd, &sbuf))
304 if (S_ISREG(sbuf.st_mode)) {
305 /* If there's plenty of RAM, use a large copy buffer */
306 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
307 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
309 bsize = BUFSIZE_SMALL;
311 bsize = MAX(sbuf.st_blksize,
312 (blksize_t)sysconf(_SC_PAGESIZE));
313 if ((buf = malloc(bsize)) == NULL)
314 err(1, "malloc() failure of IO buffer");
316 while ((nr = read(rfd, buf, bsize)) > 0)
317 for (off = 0; nr; nr -= nw, off += nw)
318 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
321 warn("%s", filename);
326 #ifndef NO_UDOM_SUPPORT
329 udom_open(const char *path, int flags)
331 struct addrinfo hints, *res, *res0;
332 char rpath[PATH_MAX];
337 * Construct the unix domain socket address and attempt to connect.
339 bzero(&hints, sizeof(hints));
340 hints.ai_family = AF_LOCAL;
341 if (realpath(path, rpath) == NULL)
343 error = getaddrinfo(rpath, NULL, &hints, &res0);
345 warn("%s", gai_strerror(error));
349 for (res = res0; res != NULL; res = res->ai_next) {
350 fd = socket(res->ai_family, res->ai_socktype,
356 error = connect(fd, res->ai_addr, res->ai_addrlen);
367 * handle the open flags by shutting down appropriate directions
370 switch(flags & O_ACCMODE) {
372 if (shutdown(fd, SHUT_WR) == -1)
376 if (shutdown(fd, SHUT_RD) == -1)