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