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
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)cat.c 8.2 (Berkeley) 4/27/95
34 * $FreeBSD: src/bin/cat/cat.c,v 1.14.2.8 2002/06/29 05:09:26 tjr Exp $
35 * $DragonFly: src/bin/cat/cat.c,v 1.16 2007/02/04 21:06:34 pavalos Exp $
38 #include <sys/param.h>
40 #ifndef NO_UDOM_SUPPORT
41 #include <sys/socket.h>
56 static int bflag
, eflag
, nflag
, sflag
, tflag
, vflag
, rval
;
57 static const char *filename
;
59 static void scanfiles(char **, int);
60 static void cook_cat(FILE *);
61 static void raw_cat(int);
63 #ifndef NO_UDOM_SUPPORT
64 static int udom_open(const char *, int);
71 "usage: cat [-benstuv] [file ...]\n");
76 main(int argc
, char **argv
)
80 setlocale(LC_CTYPE
, "");
82 while ((ch
= getopt(argc
, argv
, "benstuv")) != -1)
85 bflag
= nflag
= 1; /* -b implies -n */
88 eflag
= vflag
= 1; /* -e implies -v */
97 tflag
= vflag
= 1; /* -t implies -v */
100 setbuf(stdout
, NULL
);
111 if (bflag
|| eflag
|| nflag
|| sflag
|| tflag
|| vflag
)
122 scanfiles(char **argv
, int cooked
)
131 while ((path
= argv
[i
]) != NULL
|| i
== 0) {
132 if (path
== NULL
|| strcmp(path
, "-") == 0) {
137 fd
= open(path
, O_RDONLY
);
138 #ifndef NO_UDOM_SUPPORT
139 if (fd
< 0 && errno
== EOPNOTSUPP
)
140 fd
= udom_open(path
, O_RDONLY
);
147 if (fd
== STDIN_FILENO
) {
150 fp
= fdopen(fd
, "r");
158 if (fd
!= STDIN_FILENO
)
170 int ch
, gobble
, line
, prev
;
172 /* Reset EOF condition on stdin. */
173 if (fp
== stdin
&& feof(stdin
))
177 for (prev
= '\n'; (ch
= getc(fp
)) != EOF
; prev
= ch
) {
187 if (nflag
&& (!bflag
|| ch
!= '\n')) {
188 fprintf(stdout
, "%6d\t", ++line
);
194 if (eflag
&& putchar('$') == EOF
)
196 } else if (ch
== '\t') {
198 if (putchar('^') == EOF
|| putchar('I') == EOF
)
203 if (!isascii(ch
) && !isprint(ch
)) {
204 if (putchar('M') == EOF
|| putchar('-') == EOF
)
209 if (putchar('^') == EOF
||
210 putchar(ch
== '\177' ? '?' :
216 if (putchar(ch
) == EOF
)
220 warn("%s", filename
);
235 static struct stat ost
;
240 * Figure out the block size to use. Use the larger of stdout vs
241 * the passed descriptor. The minimum blocksize we use is BUFSIZ.
243 if (ost
.st_blksize
== 0) {
244 if (fstat(STDOUT_FILENO
, &ost
))
246 if (ost
.st_blksize
< BUFSIZ
)
247 ost
.st_blksize
= BUFSIZ
;
250 * note: rst.st_blksize can be 0, but it is handled ok.
252 if (fstat(rfd
, &rst
))
253 err(1, "%s", filename
);
255 nbsize
= MAX(ost
.st_blksize
, rst
.st_blksize
);
256 if (bsize
!= nbsize
) {
258 if ((buf
= realloc(buf
, bsize
)) == NULL
)
259 err(1, "malloc failed");
261 while ((nr
= read(rfd
, buf
, bsize
)) > 0) {
262 for (off
= 0; nr
; nr
-= nw
, off
+= nw
) {
263 nw
= write(STDOUT_FILENO
, buf
+ off
, nr
);
269 warn("%s", filename
);
274 #ifndef NO_UDOM_SUPPORT
277 udom_open(const char *path
, int 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 if ((len
= strlcpy(sou
.sun_path
, path
,
292 sizeof(sou
.sun_path
))) >= sizeof(sou
.sun_path
)) {
293 errno
= ENAMETOOLONG
;
296 len
= offsetof(struct sockaddr_un
, sun_path
[len
+1]);
298 if (connect(fd
, (void *)&sou
, len
) < 0) {
305 * handle the open flags by shutting down appropriate directions
308 switch (flags
& O_ACCMODE
) {
310 if (shutdown(fd
, SHUT_WR
) == -1)
314 if (shutdown(fd
, SHUT_RD
) == -1)