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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char const copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
47 static const char rcsid[] =
48 "$FreeBSD: src/bin/cat/cat.c,v 1.14.2.8 2002/06/29 05:09:26 tjr Exp $";
51 #include <sys/param.h>
53 #ifndef NO_UDOM_SUPPORT
54 #include <sys/socket.h>
69 int bflag, eflag, nflag, sflag, tflag, vflag;
73 int main __P((int argc, char *argv[]));
75 static void scanfiles __P((char **argv, int cooked));
76 static void cook_cat __P((FILE *));
77 static void raw_cat __P((int));
79 #ifndef NO_UDOM_SUPPORT
80 static int udom_open __P((const char *path, int flags));
90 setlocale(LC_CTYPE, "");
92 while ((ch = getopt(argc, argv, "benstuv")) != -1)
95 bflag = nflag = 1; /* -b implies -n */
98 eflag = vflag = 1; /* -e implies -v */
107 tflag = vflag = 1; /* -t implies -v */
110 setbuf(stdout, NULL);
117 "usage: cat [-benstuv] [-] [file ...]\n");
122 if (bflag || eflag || nflag || sflag || tflag || vflag)
132 scanfiles(argv, cooked)
140 while ((path = argv[i]) != NULL || i == 0) {
143 if (path == NULL || strcmp(path, "-") == 0) {
148 fd = open(path, O_RDONLY);
149 #ifndef NO_UDOM_SUPPORT
150 if (fd < 0 && errno == EOPNOTSUPP)
151 fd = udom_open(path, O_RDONLY);
158 if (fd == STDIN_FILENO)
161 fp = fdopen(fd, "r");
167 if (fd != STDIN_FILENO)
180 register int ch, gobble, line, prev;
182 /* Reset EOF condition on stdin. */
183 if (fp == stdin && feof(stdin))
187 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
191 if (!gobble && putchar(ch) == EOF)
196 if (nflag && !bflag) {
197 fprintf(stdout, "%6d\t", ++line);
202 fprintf(stdout, "%6d\t", ++line);
210 if (putchar('$') == EOF)
212 } else if (ch == '\t') {
214 if (putchar('^') == EOF || putchar('I') == EOF)
219 if (!isascii(ch) && !isprint(ch)) {
220 if (putchar('M') == EOF || putchar('-') == EOF)
225 if (putchar('^') == EOF ||
226 putchar(ch == '\177' ? '?' :
232 if (putchar(ch) == EOF)
236 warn("%s", filename);
248 register int off, wfd;
251 static char *buf = NULL;
254 wfd = fileno(stdout);
256 if (fstat(wfd, &sbuf))
257 err(1, "%s", filename);
258 bsize = MAX(sbuf.st_blksize, 1024);
259 if ((buf = malloc(bsize)) == NULL)
262 while ((nr = read(rfd, buf, bsize)) > 0)
263 for (off = 0; nr; nr -= nw, off += nw)
264 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
267 warn("%s", filename);
272 #ifndef NO_UDOM_SUPPORT
275 udom_open(path, flags)
279 struct sockaddr_un sou;
283 bzero(&sou, sizeof(sou));
286 * Construct the unix domain socket address and attempt to connect
288 fd = socket(AF_UNIX, SOCK_STREAM, 0);
290 sou.sun_family = AF_UNIX;
291 snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path);
292 len = strlen(sou.sun_path);
293 len = offsetof(struct sockaddr_un, sun_path[len+1]);
295 if (connect(fd, (void *)&sou, len) < 0) {
302 * handle the open flags by shutting down appropriate directions
305 switch(flags & O_ACCMODE) {
307 if (shutdown(fd, SHUT_WR) == -1)
311 if (shutdown(fd, SHUT_RD) == -1)