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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)cat.c 8.2 (Berkeley) 4/27/95
38 * $FreeBSD: src/bin/cat/cat.c,v 1.14.2.8 2002/06/29 05:09:26 tjr Exp $
39 * $DragonFly: src/bin/cat/cat.c,v 1.13 2004/11/07 23:44:38 dillon Exp $
42 #include <sys/param.h>
44 #ifndef NO_UDOM_SUPPORT
45 #include <sys/socket.h>
60 static int bflag
, eflag
, nflag
, sflag
, tflag
, vflag
, rval
;
61 static const char *filename
;
63 static void scanfiles (char **, int);
64 static void cook_cat (FILE *);
65 static void raw_cat (int);
67 #ifndef NO_UDOM_SUPPORT
68 static int udom_open (const char *path
, int flags
);
75 "usage: cat [-benstuv] [-] [file ...]\n");
80 main(int argc
, char **argv
)
84 setlocale(LC_CTYPE
, "");
86 while ((ch
= getopt(argc
, argv
, "benstuv")) != -1)
89 bflag
= nflag
= 1; /* -b implies -n */
92 eflag
= vflag
= 1; /* -e implies -v */
101 tflag
= vflag
= 1; /* -t implies -v */
104 setbuf(stdout
, NULL
);
115 if (bflag
|| eflag
|| nflag
|| sflag
|| tflag
|| vflag
)
126 scanfiles(char **argv
, int cooked
)
135 while ((path
= argv
[i
]) != NULL
|| i
== 0) {
136 if (path
== NULL
|| strcmp(path
, "-") == 0) {
141 fd
= open(path
, O_RDONLY
);
142 #ifndef NO_UDOM_SUPPORT
143 if (fd
< 0 && errno
== EOPNOTSUPP
)
144 fd
= udom_open(path
, O_RDONLY
);
151 if (fd
== STDIN_FILENO
) {
154 fp
= fdopen(fd
, "r");
162 if (fd
!= STDIN_FILENO
)
174 int ch
, gobble
, line
, prev
;
176 /* Reset EOF condition on stdin. */
177 if (fp
== stdin
&& feof(stdin
))
181 for (prev
= '\n'; (ch
= getc(fp
)) != EOF
; prev
= ch
) {
185 if (!gobble
&& putchar(ch
) == EOF
)
190 if (nflag
&& !bflag
) {
191 fprintf(stdout
, "%6d\t", ++line
);
196 fprintf(stdout
, "%6d\t", ++line
);
204 if (putchar('$') == EOF
)
206 } else if (ch
== '\t') {
208 if (putchar('^') == EOF
|| putchar('I') == EOF
)
213 if (!isascii(ch
) && !isprint(ch
)) {
214 if (putchar('M') == EOF
|| putchar('-') == EOF
)
219 if (putchar('^') == EOF
||
220 putchar(ch
== '\177' ? '?' :
226 if (putchar(ch
) == EOF
)
230 warn("%s", filename
);
246 static struct stat ost
;
251 * Figure out the block size to use. Use the larger of stdout vs
252 * the passed descriptor. The minimum blocksize we use is BUFSIZ.
254 if (ost
.st_blksize
== 0) {
255 if (fstat(STDOUT_FILENO
, &ost
))
257 if (ost
.st_blksize
< BUFSIZ
)
258 ost
.st_blksize
= BUFSIZ
;
261 * note: rst.st_blksize can be 0, but it is handled ok.
263 if (fstat(rfd
, &rst
))
264 err(1, "%s", filename
);
266 nbsize
= MAX(ost
.st_blksize
, rst
.st_blksize
);
267 if (bsize
!= nbsize
) {
269 if ((buf
= realloc(buf
, bsize
)) == NULL
)
270 err(1, "malloc failed");
272 while ((nr
= read(rfd
, buf
, bsize
)) > 0) {
273 for (off
= 0; nr
; nr
-= nw
, off
+= nw
) {
274 nw
= write(STDOUT_FILENO
, buf
+ off
, nr
);
280 warn("%s", filename
);
285 #ifndef NO_UDOM_SUPPORT
288 udom_open(const char *path
, int flags
)
290 struct sockaddr_un sou
;
294 bzero(&sou
, sizeof(sou
));
297 * Construct the unix domain socket address and attempt to connect
299 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
301 sou
.sun_family
= AF_UNIX
;
302 snprintf(sou
.sun_path
, sizeof(sou
.sun_path
), "%s", path
);
303 len
= strlen(sou
.sun_path
);
304 len
= offsetof(struct sockaddr_un
, sun_path
[len
+1]);
306 if (connect(fd
, (void *)&sou
, len
) < 0) {
313 * handle the open flags by shutting down appropriate directions
316 switch (flags
& O_ACCMODE
) {
318 if (shutdown(fd
, SHUT_WR
) == -1)
322 if (shutdown(fd
, SHUT_RD
) == -1)