xref: /dragonfly/bin/cat/cat.c (revision 9348a738)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)cat.c	8.2 (Berkeley) 4/27/95
34  * $FreeBSD: src/bin/cat/cat.c,v 1.14.2.8 2002/06/29 05:09:26 tjr Exp $
35  * $DragonFly: src/bin/cat/cat.c,v 1.16 2007/02/04 21:06:34 pavalos Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #ifndef NO_UDOM_SUPPORT
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <errno.h>
44 #endif
45 
46 #include <ctype.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <locale.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 static int bflag, eflag, nflag, sflag, tflag, vflag, rval;
57 static const char *filename;
58 
59 static void	scanfiles(char **, int);
60 static void	cook_cat(FILE *);
61 static void	raw_cat(int);
62 
63 #ifndef NO_UDOM_SUPPORT
64 static int	udom_open(const char *, int);
65 #endif
66 
67 static void
68 usage(void)
69 {
70 	fprintf(stderr,
71 		"usage: cat [-benstuv] [file ...]\n");
72 	exit(EXIT_FAILURE);
73 }
74 
75 int
76 main(int argc, char **argv)
77 {
78 	int ch;
79 
80 	setlocale(LC_CTYPE, "");
81 
82 	while ((ch = getopt(argc, argv, "benstuv")) != -1)
83 		switch (ch) {
84 		case 'b':
85 			bflag = nflag = 1;	/* -b implies -n */
86 			break;
87 		case 'e':
88 			eflag = vflag = 1;	/* -e implies -v */
89 			break;
90 		case 'n':
91 			nflag = 1;
92 			break;
93 		case 's':
94 			sflag = 1;
95 			break;
96 		case 't':
97 			tflag = vflag = 1;	/* -t implies -v */
98 			break;
99 		case 'u':
100 			setbuf(stdout, NULL);
101 			break;
102 		case 'v':
103 			vflag = 1;
104 			break;
105 		default:
106 			usage();
107 			/* NOTREACHED */
108 		}
109 	argv += optind;
110 
111 	if (bflag || eflag || nflag || sflag || tflag || vflag)
112 		scanfiles(argv, 1);
113 	else
114 		scanfiles(argv, 0);
115 	if (fclose(stdout))
116 		err(1, "stdout");
117 	exit(rval);
118 	/* NOTREACHED */
119 }
120 
121 static void
122 scanfiles(char **argv, int cooked)
123 {
124 	char *path;
125 	FILE *fp;
126 	int fd;
127 	int i;
128 
129 	i = 0;
130 
131 	while ((path = argv[i]) != NULL || i == 0) {
132 		if (path == NULL || strcmp(path, "-") == 0) {
133 			filename = "stdin";
134 			fd = STDIN_FILENO;
135 		} else {
136 			filename = path;
137 			fd = open(path, O_RDONLY);
138 #ifndef NO_UDOM_SUPPORT
139 			if (fd < 0 && errno == EOPNOTSUPP)
140 				fd = udom_open(path, O_RDONLY);
141 #endif
142 		}
143 		if (fd < 0) {
144 			warn("%s", path);
145 			rval = 1;
146 		} else if (cooked) {
147 			if (fd == STDIN_FILENO) {
148 				cook_cat(stdin);
149 			} else {
150 				fp = fdopen(fd, "r");
151 				if (fp == NULL)
152 					err(1, "%s", path);
153 				cook_cat(fp);
154 				fclose(fp);
155 			}
156 		} else {
157 			raw_cat(fd);
158 			if (fd != STDIN_FILENO)
159 				close(fd);
160 		}
161 		if (path == NULL)
162 			break;
163 		++i;
164 	}
165 }
166 
167 static void
168 cook_cat(FILE *fp)
169 {
170 	int ch, gobble, line, prev;
171 
172 	/* Reset EOF condition on stdin. */
173 	if (fp == stdin && feof(stdin))
174 		clearerr(stdin);
175 
176 	line = gobble = 0;
177 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
178 		if (prev == '\n') {
179 			if (sflag) {
180 				if (ch == '\n') {
181 					if (gobble)
182 						continue;
183 					gobble = 1;
184 				} else
185 					gobble = 0;
186 			}
187 			if (nflag) {
188 				if (!bflag || ch != '\n') {
189 					fprintf(stdout, "%6d\t", ++line);
190 					if (ferror(stdout))
191 						break;
192 				} else if (eflag) {
193 					fprintf(stdout, "%6s\t", "");
194 					if (ferror(stdout))
195 						break;
196 				}
197 			}
198 		}
199 		if (ch == '\n') {
200 			if (eflag && 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 
234 static void
235 raw_cat(int rfd)
236 {
237 	int off;
238 	size_t nbsize;
239 	ssize_t nr, nw;
240 	struct stat rst;
241 	static struct stat ost;
242 	static size_t bsize;
243 	static char *buf;
244 
245 	/*
246 	 * Figure out the block size to use.  Use the larger of stdout vs
247 	 * the passed descriptor.  The minimum blocksize we use is BUFSIZ.
248 	 */
249 	if (ost.st_blksize == 0) {
250 		if (fstat(STDOUT_FILENO, &ost))
251 			err(1, "<stdout>");
252 		if (ost.st_blksize < BUFSIZ)
253 			ost.st_blksize = BUFSIZ;
254 	}
255 	/*
256 	 * note: rst.st_blksize can be 0, but it is handled ok.
257 	 */
258 	if (fstat(rfd, &rst))
259 		err(1, "%s", filename);
260 
261 	nbsize = MAX(ost.st_blksize, rst.st_blksize);
262 	if (bsize != nbsize) {
263 		bsize = nbsize;
264 		if ((buf = realloc(buf, bsize)) == NULL)
265 			err(1, "malloc failed");
266 	}
267 	while ((nr = read(rfd, buf, bsize)) > 0) {
268 		for (off = 0; nr; nr -= nw, off += nw) {
269 			nw = write(STDOUT_FILENO, buf + off, nr);
270 			if (nw < 0)
271 				err(1, "stdout");
272 		}
273 	}
274 	if (nr < 0) {
275 		warn("%s", filename);
276 		rval = 1;
277 	}
278 }
279 
280 #ifndef NO_UDOM_SUPPORT
281 
282 static int
283 udom_open(const char *path, int flags)
284 {
285 	struct sockaddr_un sou;
286 	int fd;
287 	unsigned int len;
288 
289 	bzero(&sou, sizeof(sou));
290 
291 	/*
292 	 * Construct the unix domain socket address and attempt to connect
293 	 */
294 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
295 	if (fd >= 0) {
296 		sou.sun_family = AF_UNIX;
297 		if ((len = strlcpy(sou.sun_path, path,
298 		    sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
299 			errno = ENAMETOOLONG;
300 			return (-1);
301 		}
302 		len = offsetof(struct sockaddr_un, sun_path[len+1]);
303 
304 		if (connect(fd, (void *)&sou, len) < 0) {
305 			close(fd);
306 			fd = -1;
307 		}
308 	}
309 
310 	/*
311 	 * handle the open flags by shutting down appropriate directions
312 	 */
313 	if (fd >= 0) {
314 		switch (flags & O_ACCMODE) {
315 		case O_RDONLY:
316 			if (shutdown(fd, SHUT_WR) == -1)
317 				warn(NULL);
318 			break;
319 		case O_WRONLY:
320 			if (shutdown(fd, SHUT_RD) == -1)
321 				warn(NULL);
322 			break;
323 		default:
324 			break;
325 		}
326 	}
327 	return(fd);
328 }
329 
330 #endif
331