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