cat.c revision 83482
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kevin Fall.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char const copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
46#endif
47static const char rcsid[] =
48  "$FreeBSD: head/bin/cat/cat.c 83482 2001-09-15 00:39:14Z dillon $";
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/stat.h>
53#ifndef NO_UDOM_SUPPORT
54#include <sys/socket.h>
55#include <sys/un.h>
56#include <errno.h>
57#endif
58
59#include <ctype.h>
60#include <err.h>
61#include <fcntl.h>
62#include <locale.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67#include <stddef.h>
68
69int bflag, eflag, nflag, sflag, tflag, vflag;
70int rval;
71const char *filename;
72
73int main __P((int argc, char *argv[]));
74
75static void scanfiles __P((char **argv, int cooked));
76static void cook_cat __P((FILE *));
77static void raw_cat __P((int));
78
79#ifndef NO_UDOM_SUPPORT
80static int udom_open __P((const char *path, int flags, int modes));
81#endif
82
83int
84main(argc, argv)
85	int argc;
86	char *argv[];
87{
88	int ch;
89
90	setlocale(LC_CTYPE, "");
91
92	while ((ch = getopt(argc, argv, "benstuv")) != -1)
93		switch (ch) {
94		case 'b':
95			bflag = nflag = 1;	/* -b implies -n */
96			break;
97		case 'e':
98			eflag = vflag = 1;	/* -e implies -v */
99			break;
100		case 'n':
101			nflag = 1;
102			break;
103		case 's':
104			sflag = 1;
105			break;
106		case 't':
107			tflag = vflag = 1;	/* -t implies -v */
108			break;
109		case 'u':
110			setbuf(stdout, NULL);
111			break;
112		case 'v':
113			vflag = 1;
114			break;
115		default:
116			(void)fprintf(stderr,
117			    "usage: cat [-benstuv] [-] [file ...]\n");
118			exit(1);
119		}
120	argv += optind;
121
122	if (bflag || eflag || nflag || sflag || tflag || vflag)
123		scanfiles(argv, 1);
124	else
125		scanfiles(argv, 0);
126	if (fclose(stdout))
127		err(1, "stdout");
128	exit(rval);
129}
130
131void
132scanfiles(argv, cooked)
133    char **argv;
134    int cooked;
135{
136	int i = 0;
137	char *path;
138
139	while ((path = argv[i]) != NULL || i == 0) {
140		int fd;
141
142		if (path == NULL || strcmp(path, "-") == 0) {
143			filename = "stdin";
144			fd = 0;
145		} else {
146			filename = path;
147			fd = open(path, O_RDONLY);
148#ifndef NO_UDOM_SUPPORT
149			if (fd < 0 && errno == EOPNOTSUPP)
150				fd = udom_open(path, O_RDONLY, 0);
151#endif
152		}
153		if (fd < 0) {
154			warn("%s", path);
155			rval = 1;
156		} else if (cooked) {
157			FILE *fp = fdopen(fd, "r");
158			cook_cat(fp);
159			fclose(fp);
160		} else {
161			raw_cat(fd);
162			close(fd);
163		}
164		if (path == NULL)
165			break;
166		++i;
167	}
168}
169
170static void
171cook_cat(fp)
172	register FILE *fp;
173{
174	register int ch, gobble, line, prev;
175
176	line = gobble = 0;
177	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
178		if (prev == '\n') {
179			if (ch == '\n') {
180				if (sflag) {
181					if (!gobble && putchar(ch) == EOF)
182						break;
183					gobble = 1;
184					continue;
185				}
186				if (nflag && !bflag) {
187					(void)fprintf(stdout, "%6d\t", ++line);
188					if (ferror(stdout))
189						break;
190				}
191			} else if (nflag) {
192				(void)fprintf(stdout, "%6d\t", ++line);
193				if (ferror(stdout))
194					break;
195			}
196		}
197		gobble = 0;
198		if (ch == '\n') {
199			if (eflag)
200				if (putchar('$') == EOF)
201					break;
202		} else if (ch == '\t') {
203			if (tflag) {
204				if (putchar('^') == EOF || putchar('I') == EOF)
205					break;
206				continue;
207			}
208		} else if (vflag) {
209			if (!isascii(ch) && !isprint(ch)) {
210				if (putchar('M') == EOF || putchar('-') == EOF)
211					break;
212				ch = toascii(ch);
213			}
214			if (iscntrl(ch)) {
215				if (putchar('^') == EOF ||
216				    putchar(ch == '\177' ? '?' :
217				    ch | 0100) == EOF)
218					break;
219				continue;
220			}
221		}
222		if (putchar(ch) == EOF)
223			break;
224	}
225	if (ferror(fp)) {
226		warn("%s", filename);
227		rval = 1;
228		clearerr(fp);
229	}
230	if (ferror(stdout))
231		err(1, "stdout");
232}
233
234static void
235raw_cat(rfd)
236	register int rfd;
237{
238	register int off, wfd;
239	ssize_t nr, nw;
240	static size_t bsize;
241	static char *buf;
242	struct stat sbuf;
243
244	wfd = fileno(stdout);
245	if (buf == NULL) {
246		if (fstat(wfd, &sbuf))
247			err(1, "%s", filename);
248		bsize = MAX(sbuf.st_blksize, 1024);
249		if ((buf = malloc(bsize)) == NULL)
250			err(1, "buffer");
251	}
252	while ((nr = read(rfd, buf, bsize)) > 0)
253		for (off = 0; nr; nr -= nw, off += nw)
254			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
255				err(1, "stdout");
256	if (nr < 0) {
257		warn("%s", filename);
258		rval = 1;
259	}
260}
261
262#ifndef NO_UDOM_SUPPORT
263
264static int
265udom_open(path, flags, modes)
266    const char *path;
267    int flags;
268    int modes;
269{
270	struct sockaddr_un sou;
271	int fd;
272	int len;
273
274	bzero(&sou, sizeof(sou));
275
276	/*
277	 * Construct the unix domain socket address and attempt to connect
278	 */
279	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
280		sou.sun_family = AF_UNIX;
281		snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path);
282		len = strlen(sou.sun_path);
283		len = offsetof(struct sockaddr_un, sun_path[len+1]);
284
285		if (connect(fd, (void *)&sou, len) < 0) {
286			close(fd);
287			fd = -1;
288		}
289	}
290
291	/*
292	 * handle the open flags by shutting down appropriate directions
293	 */
294	if (fd >= 0) {
295		switch(flags & O_ACCMODE) {
296		case O_RDONLY:
297			shutdown(fd, SHUT_WR);
298			break;
299		case O_WRONLY:
300			shutdown(fd, SHUT_RD);
301			break;
302		default:
303			break;
304		}
305	}
306	return(fd);
307}
308
309#endif
310
311