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