]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/cat/cat.c
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: src/bin/cat/cat.c,v 1.32 2005/01/10 08:39:20 imp Exp $");
49 #include <sys/param.h>
51 #ifndef NO_UDOM_SUPPORT
52 #include <sys/socket.h>
67 int bflag
, eflag
, nflag
, sflag
, tflag
, vflag
;
71 static void usage(void);
72 static void scanfiles(char *argv
[], int cooked
);
73 static void cook_cat(FILE *);
74 static void raw_cat(int);
76 #ifndef NO_UDOM_SUPPORT
77 static int udom_open(const char *path
, int flags
);
81 main(int argc
, char *argv
[])
85 setlocale(LC_CTYPE
, "");
87 while ((ch
= getopt(argc
, argv
, "benstuv")) != -1)
90 bflag
= nflag
= 1; /* -b implies -n */
93 eflag
= vflag
= 1; /* -e implies -v */
102 tflag
= vflag
= 1; /* -t implies -v */
105 setbuf(stdout
, NULL
);
115 if (bflag
|| eflag
|| nflag
|| sflag
|| tflag
|| vflag
)
128 fprintf(stderr
, "usage: cat [-benstuv] [file ...]\n");
134 scanfiles(char *argv
[], int 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
)
179 int ch
, gobble
, line
, prev
;
181 /* Reset EOF condition on stdin. */
182 if (fp
== stdin
&& feof(stdin
))
186 for (prev
= '\n'; (ch
= getc(fp
)) != EOF
; prev
= ch
) {
196 if (nflag
&& (!bflag
|| ch
!= '\n')) {
197 (void)fprintf(stdout
, "%6d\t", ++line
);
203 if (eflag
&& putchar('$') == EOF
)
205 } else if (ch
== '\t') {
207 if (putchar('^') == EOF
|| putchar('I') == EOF
)
212 if (!isascii(ch
) && !isprint(ch
)) {
213 if (putchar('M') == EOF
|| putchar('-') == EOF
)
218 if (putchar('^') == EOF
||
219 putchar(ch
== '\177' ? '?' :
225 if (putchar(ch
) == EOF
)
229 warn("%s", filename
);
243 static char *buf
= NULL
;
246 wfd
= fileno(stdout
);
248 if (fstat(wfd
, &sbuf
))
249 err(1, "%s", filename
);
250 bsize
= MAX(sbuf
.st_blksize
, 1024);
251 if ((buf
= malloc(bsize
)) == NULL
)
254 while ((nr
= read(rfd
, buf
, bsize
)) > 0)
255 for (off
= 0; nr
; nr
-= nw
, off
+= nw
)
256 if ((nw
= write(wfd
, buf
+ off
, (size_t)nr
)) < 0)
259 warn("%s", filename
);
264 #ifndef NO_UDOM_SUPPORT
267 udom_open(const char *path
, int flags
)
269 struct sockaddr_un sou
;
273 bzero(&sou
, sizeof(sou
));
276 * Construct the unix domain socket address and attempt to connect
278 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
280 sou
.sun_family
= AF_UNIX
;
281 if ((len
= strlcpy(sou
.sun_path
, path
,
282 sizeof(sou
.sun_path
))) >= sizeof(sou
.sun_path
)) {
283 errno
= ENAMETOOLONG
;
286 len
= offsetof(struct sockaddr_un
, sun_path
[len
+1]);
288 if (connect(fd
, (void *)&sou
, len
) < 0) {
295 * handle the open flags by shutting down appropriate directions
298 switch(flags
& O_ACCMODE
) {
300 if (shutdown(fd
, SHUT_WR
) == -1)
304 if (shutdown(fd
, SHUT_RD
) == -1)