xref: /netbsd-src/bin/cat/cat.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: cat.c,v 1.35 2002/09/13 18:07:52 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kevin Fall.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(__COPYRIGHT) && !defined(lint)
41 __COPYRIGHT(
42 "@(#) Copyright (c) 1989, 1993\n\
43 	The Regents of the University of California.  All rights reserved.\n");
44 #endif /* not lint */
45 
46 #if defined(__RCSID) && !defined(lint)
47 #if 0
48 static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
49 #else
50 __RCSID("$NetBSD: cat.c,v 1.35 2002/09/13 18:07:52 thorpej Exp $");
51 #endif
52 #endif /* not lint */
53 
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 
57 #include <ctype.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <locale.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
68 int rval;
69 const char *filename;
70 
71 int main(int, char *[]);
72 void cook_args(char *argv[]);
73 void cook_buf(FILE *);
74 void raw_args(char *argv[]);
75 void raw_cat(int);
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	int ch;
81 	struct flock stdout_lock;
82 
83 	setprogname(argv[0]);
84 	(void)setlocale(LC_ALL, "");
85 
86 	while ((ch = getopt(argc, argv, "beflnstuv")) != -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 'f':
95 			fflag = 1;
96 			break;
97 		case 'l':
98 			lflag = 1;
99 			break;
100 		case 'n':
101 			nflag = 1;
102 			break;
103 		case 's':
104 			sflag = 1;
105 			break;
106 		case 't':
107 			tflag = vflag = 1;	/* -t implies -v */
108 			break;
109 		case 'u':
110 			setbuf(stdout, NULL);
111 			break;
112 		case 'v':
113 			vflag = 1;
114 			break;
115 		default:
116 		case '?':
117 			(void)fprintf(stderr,
118 			    "usage: cat [-beflnstuv] [-] [file ...]\n");
119 			exit(1);
120 			/* NOTREACHED */
121 		}
122 	argv += optind;
123 
124 	if (lflag) {
125 		stdout_lock.l_len = 0;
126 		stdout_lock.l_start = 0;
127 		stdout_lock.l_type = F_WRLCK;
128 		stdout_lock.l_whence = SEEK_SET;
129 		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
130 			err(EXIT_FAILURE, "stdout");
131 	}
132 
133 	if (bflag || eflag || nflag || sflag || tflag || vflag)
134 		cook_args(argv);
135 	else
136 		raw_args(argv);
137 	if (fclose(stdout))
138 		err(1, "stdout");
139 	exit(rval);
140 	/* NOTREACHED */
141 }
142 
143 void
144 cook_args(char **argv)
145 {
146 	FILE *fp;
147 
148 	fp = stdin;
149 	filename = "stdin";
150 	do {
151 		if (*argv) {
152 			if (!strcmp(*argv, "-"))
153 				fp = stdin;
154 			else if ((fp = fopen(*argv,
155 			    fflag ? "rf" : "r")) == NULL) {
156 				warn("%s", *argv);
157 				rval = 1;
158 				++argv;
159 				continue;
160 			}
161 			filename = *argv++;
162 		}
163 		cook_buf(fp);
164 		if (fp != stdin)
165 			(void)fclose(fp);
166 	} while (*argv);
167 }
168 
169 void
170 cook_buf(FILE *fp)
171 {
172 	int ch, gobble, line, prev;
173 
174 	line = gobble = 0;
175 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
176 		if (prev == '\n') {
177 			if (ch == '\n') {
178 				if (sflag) {
179 					if (!gobble && putchar(ch) == EOF)
180 						break;
181 					gobble = 1;
182 					continue;
183 				}
184 				if (nflag) {
185 					if (!bflag) {
186 						(void)fprintf(stdout,
187 						    "%6d\t", ++line);
188 						if (ferror(stdout))
189 							break;
190 					} else if (eflag) {
191 						(void)fprintf(stdout,
192 						    "%6s\t", "");
193 						if (ferror(stdout))
194 							break;
195 					}
196 				}
197 			} else if (nflag) {
198 				(void)fprintf(stdout, "%6d\t", ++line);
199 				if (ferror(stdout))
200 					break;
201 			}
202 		}
203 		gobble = 0;
204 		if (ch == '\n') {
205 			if (eflag)
206 				if (putchar('$') == EOF)
207 					break;
208 		} else if (ch == '\t') {
209 			if (tflag) {
210 				if (putchar('^') == EOF || putchar('I') == EOF)
211 					break;
212 				continue;
213 			}
214 		} else if (vflag) {
215 			if (!isascii(ch)) {
216 				if (putchar('M') == EOF || putchar('-') == EOF)
217 					break;
218 				ch = toascii(ch);
219 			}
220 			if (iscntrl(ch)) {
221 				if (putchar('^') == EOF ||
222 				    putchar(ch == '\177' ? '?' :
223 				    ch | 0100) == EOF)
224 					break;
225 				continue;
226 			}
227 		}
228 		if (putchar(ch) == EOF)
229 			break;
230 	}
231 	if (ferror(fp)) {
232 		warn("%s", filename);
233 		rval = 1;
234 		clearerr(fp);
235 	}
236 	if (ferror(stdout))
237 		err(1, "stdout");
238 }
239 
240 void
241 raw_args(char **argv)
242 {
243 	int fd;
244 
245 	fd = fileno(stdin);
246 	filename = "stdin";
247 	do {
248 		if (*argv) {
249 			if (!strcmp(*argv, "-"))
250 				fd = fileno(stdin);
251 			else if (fflag) {
252 				struct stat st;
253 				fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
254 				if (fd < 0)
255 					goto skip;
256 
257 				if (fstat(fd, &st) == -1) {
258 					close(fd);
259 					goto skip;
260 				}
261 				if (!S_ISREG(st.st_mode)) {
262 					close(fd);
263 					warnx("%s: not a regular file", *argv);
264 					goto skipnomsg;
265 				}
266 			}
267 			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
268 skip:
269 				warn("%s", *argv);
270 skipnomsg:
271 				rval = 1;
272 				++argv;
273 				continue;
274 			}
275 			filename = *argv++;
276 		}
277 		raw_cat(fd);
278 		if (fd != fileno(stdin))
279 			(void)close(fd);
280 	} while (*argv);
281 }
282 
283 void
284 raw_cat(int rfd)
285 {
286 	static char *buf;
287 	static char fb_buf[BUFSIZ];
288 	static size_t bsize;
289 
290 	struct stat sbuf;
291 	ssize_t nr, nw, off;
292 	int wfd;
293 
294 	wfd = fileno(stdout);
295 	if (buf == NULL) {
296 		if (fstat(wfd, &sbuf) == 0) {
297 			bsize = sbuf.st_blksize > BUFSIZ ?
298 			    sbuf.st_blksize : BUFSIZ;
299 			buf = malloc(bsize);
300 		}
301 		if (buf == NULL) {
302 			buf = fb_buf;
303 			bsize = BUFSIZ;
304 		}
305 	}
306 	while ((nr = read(rfd, buf, bsize)) > 0)
307 		for (off = 0; nr; nr -= nw, off += nw)
308 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
309 				err(1, "stdout");
310 	if (nr < 0) {
311 		warn("%s", filename);
312 		rval = 1;
313 	}
314 }
315