xref: /freebsd/bin/cat/cat.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
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
38 static 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
45 static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
46 #endif
47 #endif /* not lint */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
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 
69 int bflag, eflag, nflag, sflag, tflag, vflag;
70 int rval;
71 const char *filename;
72 
73 static void usage(void);
74 static void scanfiles(char **argv, int cooked);
75 static void cook_cat(FILE *);
76 static void raw_cat(int);
77 
78 #ifndef NO_UDOM_SUPPORT
79 static int udom_open(const char *path, int flags);
80 #endif
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	int ch;
86 
87 	setlocale(LC_CTYPE, "");
88 
89 	while ((ch = getopt(argc, argv, "benstuv")) != -1)
90 		switch (ch) {
91 		case 'b':
92 			bflag = nflag = 1;	/* -b implies -n */
93 			break;
94 		case 'e':
95 			eflag = vflag = 1;	/* -e implies -v */
96 			break;
97 		case 'n':
98 			nflag = 1;
99 			break;
100 		case 's':
101 			sflag = 1;
102 			break;
103 		case 't':
104 			tflag = vflag = 1;	/* -t implies -v */
105 			break;
106 		case 'u':
107 			setbuf(stdout, NULL);
108 			break;
109 		case 'v':
110 			vflag = 1;
111 			break;
112 		default:
113 			usage();
114 		}
115 	argv += optind;
116 
117 	if (bflag || eflag || nflag || sflag || tflag || vflag)
118 		scanfiles(argv, 1);
119 	else
120 		scanfiles(argv, 0);
121 	if (fclose(stdout))
122 		err(1, "stdout");
123 	exit(rval);
124 	/* NOTREACHED */
125 }
126 
127 static void
128 usage(void)
129 {
130 	fprintf(stderr, "usage: cat [-benstuv] [file ...]\n");
131 	exit(1);
132 	/* NOTREACHED */
133 }
134 
135 void
136 scanfiles(char **argv, int cooked)
137 {
138 	int i = 0;
139 	char *path;
140 	FILE *fp;
141 
142 	while ((path = argv[i]) != NULL || i == 0) {
143 		int fd;
144 
145 		if (path == NULL || strcmp(path, "-") == 0) {
146 			filename = "stdin";
147 			fd = STDIN_FILENO;
148 		} else {
149 			filename = path;
150 			fd = open(path, O_RDONLY);
151 #ifndef NO_UDOM_SUPPORT
152 			if (fd < 0 && errno == EOPNOTSUPP)
153 				fd = udom_open(path, O_RDONLY);
154 #endif
155 		}
156 		if (fd < 0) {
157 			warn("%s", path);
158 			rval = 1;
159 		} else if (cooked) {
160 			if (fd == STDIN_FILENO)
161 				cook_cat(stdin);
162 			else {
163 				fp = fdopen(fd, "r");
164 				cook_cat(fp);
165 				fclose(fp);
166 			}
167 		} else {
168 			raw_cat(fd);
169 			if (fd != STDIN_FILENO)
170 				close(fd);
171 		}
172 		if (path == NULL)
173 			break;
174 		++i;
175 	}
176 }
177 
178 static void
179 cook_cat(FILE *fp)
180 {
181 	int ch, gobble, line, prev;
182 
183 	/* Reset EOF condition on stdin. */
184 	if (fp == stdin && feof(stdin))
185 		clearerr(stdin);
186 
187 	line = gobble = 0;
188 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
189 		if (prev == '\n') {
190 			if (sflag) {
191 				if (ch == '\n') {
192 					if (gobble)
193 						continue;
194 					gobble = 1;
195 				} else
196 					gobble = 0;
197 			}
198 			if (nflag && (!bflag || ch != '\n')) {
199 				(void)fprintf(stdout, "%6d\t", ++line);
200 				if (ferror(stdout))
201 					break;
202 			}
203 		}
204 		if (ch == '\n') {
205 			if (eflag && putchar('$') == EOF)
206 				break;
207 		} else if (ch == '\t') {
208 			if (tflag) {
209 				if (putchar('^') == EOF || putchar('I') == EOF)
210 					break;
211 				continue;
212 			}
213 		} else if (vflag) {
214 			if (!isascii(ch) && !isprint(ch)) {
215 				if (putchar('M') == EOF || putchar('-') == EOF)
216 					break;
217 				ch = toascii(ch);
218 			}
219 			if (iscntrl(ch)) {
220 				if (putchar('^') == EOF ||
221 				    putchar(ch == '\177' ? '?' :
222 				    ch | 0100) == EOF)
223 					break;
224 				continue;
225 			}
226 		}
227 		if (putchar(ch) == EOF)
228 			break;
229 	}
230 	if (ferror(fp)) {
231 		warn("%s", filename);
232 		rval = 1;
233 		clearerr(fp);
234 	}
235 	if (ferror(stdout))
236 		err(1, "stdout");
237 }
238 
239 static void
240 raw_cat(int rfd)
241 {
242 	int off, wfd;
243 	ssize_t nr, nw;
244 	static size_t bsize;
245 	static char *buf = NULL;
246 	struct stat sbuf;
247 
248 	wfd = fileno(stdout);
249 	if (buf == NULL) {
250 		if (fstat(wfd, &sbuf))
251 			err(1, "%s", filename);
252 		bsize = MAX(sbuf.st_blksize, 1024);
253 		if ((buf = malloc(bsize)) == NULL)
254 			err(1, "buffer");
255 	}
256 	while ((nr = read(rfd, buf, bsize)) > 0)
257 		for (off = 0; nr; nr -= nw, off += nw)
258 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
259 				err(1, "stdout");
260 	if (nr < 0) {
261 		warn("%s", filename);
262 		rval = 1;
263 	}
264 }
265 
266 #ifndef NO_UDOM_SUPPORT
267 
268 static int
269 udom_open(const char *path, int flags)
270 {
271 	struct sockaddr_un sou;
272 	int fd;
273 	unsigned int len;
274 
275 	bzero(&sou, sizeof(sou));
276 
277 	/*
278 	 * Construct the unix domain socket address and attempt to connect
279 	 */
280 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
281 	if (fd >= 0) {
282 		sou.sun_family = AF_UNIX;
283 		if ((len = strlcpy(sou.sun_path, path,
284 		    sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
285 			errno = ENAMETOOLONG;
286 			return (-1);
287 		}
288 		len = offsetof(struct sockaddr_un, sun_path[len+1]);
289 
290 		if (connect(fd, (void *)&sou, len) < 0) {
291 			close(fd);
292 			fd = -1;
293 		}
294 	}
295 
296 	/*
297 	 * handle the open flags by shutting down appropriate directions
298 	 */
299 	if (fd >= 0) {
300 		switch(flags & O_ACCMODE) {
301 		case O_RDONLY:
302 			if (shutdown(fd, SHUT_WR) == -1)
303 				perror("cat");
304 			break;
305 		case O_WRONLY:
306 			if (shutdown(fd, SHUT_RD) == -1)
307 				perror("cat");
308 			break;
309 		default:
310 			break;
311 		}
312 	}
313 	return(fd);
314 }
315 
316 #endif
317