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