cat.c revision 18546
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 *	$Id: cat.c,v 1.3 1995/10/03 12:46:37 bde Exp $
37 */
38
39#ifndef lint
40static char copyright[] =
41"@(#) Copyright (c) 1989, 1993\n\
42	The Regents of the University of California.  All rights reserved.\n";
43#endif /* not lint */
44
45#ifndef lint
46static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/stat.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61int bflag, eflag, nflag, sflag, tflag, vflag;
62int rval;
63char *filename;
64
65void cook_args __P((char *argv[]));
66void cook_buf __P((FILE *));
67void raw_args __P((char *argv[]));
68void raw_cat __P((int));
69
70int
71main(argc, argv)
72	int argc;
73	char *argv[];
74{
75	extern int optind;
76	int ch;
77
78	while ((ch = getopt(argc, argv, "benstuv")) != EOF)
79		switch (ch) {
80		case 'b':
81			bflag = nflag = 1;	/* -b implies -n */
82			break;
83		case 'e':
84			eflag = vflag = 1;	/* -e implies -v */
85			break;
86		case 'n':
87			nflag = 1;
88			break;
89		case 's':
90			sflag = 1;
91			break;
92		case 't':
93			tflag = vflag = 1;	/* -t implies -v */
94			break;
95		case 'u':
96			setbuf(stdout, (char *)NULL);
97			break;
98		case 'v':
99			vflag = 1;
100			break;
101		default:
102			(void)fprintf(stderr,
103			    "usage: cat [-benstuv] [-] [file ...]\n");
104			exit(1);
105		}
106	argv += optind;
107
108	if (bflag || eflag || nflag || sflag || tflag || vflag)
109		cook_args(argv);
110	else
111		raw_args(argv);
112	if (fclose(stdout))
113		err(1, "stdout");
114	exit(rval);
115}
116
117void
118cook_args(argv)
119	char **argv;
120{
121	register FILE *fp;
122
123	fp = stdin;
124	filename = "stdin";
125	do {
126		if (*argv) {
127			if (!strcmp(*argv, "-"))
128				fp = stdin;
129			else if ((fp = fopen(*argv, "r")) == NULL) {
130				warn("%s", *argv);
131				rval = 1;
132				++argv;
133				continue;
134			}
135			filename = *argv++;
136		}
137		cook_buf(fp);
138		if (fp != stdin)
139			(void)fclose(fp);
140	} while (*argv);
141}
142
143void
144cook_buf(fp)
145	register FILE *fp;
146{
147	register int ch, gobble, line, prev;
148
149	line = gobble = 0;
150	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
151		if (prev == '\n') {
152			if (ch == '\n') {
153				if (sflag) {
154					if (!gobble && putchar(ch) == EOF)
155						break;
156					gobble = 1;
157					continue;
158				}
159				if (nflag && !bflag) {
160					(void)fprintf(stdout, "%6d\t", ++line);
161					if (ferror(stdout))
162						break;
163				}
164			} else if (nflag) {
165				(void)fprintf(stdout, "%6d\t", ++line);
166				if (ferror(stdout))
167					break;
168			}
169		}
170		gobble = 0;
171		if (ch == '\n') {
172			if (eflag)
173				if (putchar('$') == EOF)
174					break;
175		} else if (ch == '\t') {
176			if (tflag) {
177				if (putchar('^') == EOF || putchar('I') == EOF)
178					break;
179				continue;
180			}
181		} else if (vflag) {
182			if (!isascii(ch)) {
183				if (putchar('M') == EOF || putchar('-') == EOF)
184					break;
185				ch = toascii(ch);
186			}
187			if (iscntrl(ch)) {
188				if (putchar('^') == EOF ||
189				    putchar(ch == '\177' ? '?' :
190				    ch | 0100) == EOF)
191					break;
192				continue;
193			}
194		}
195		if (putchar(ch) == EOF)
196			break;
197	}
198	if (ferror(fp)) {
199		warn("%s", filename);
200		rval = 1;
201		clearerr(fp);
202	}
203	if (ferror(stdout))
204		err(1, "stdout");
205}
206
207void
208raw_args(argv)
209	char **argv;
210{
211	register int fd;
212
213	fd = fileno(stdin);
214	filename = "stdin";
215	do {
216		if (*argv) {
217			if (!strcmp(*argv, "-"))
218				fd = fileno(stdin);
219			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
220				warn("%s", *argv);
221				rval = 1;
222				++argv;
223				continue;
224			}
225			filename = *argv++;
226		}
227		raw_cat(fd);
228		if (fd != fileno(stdin))
229			(void)close(fd);
230	} while (*argv);
231}
232
233void
234raw_cat(rfd)
235	register int rfd;
236{
237	register int nr, nw, off, wfd;
238	static int bsize;
239	static char *buf;
240	struct stat sbuf;
241
242	wfd = fileno(stdout);
243	if (buf == NULL) {
244		if (fstat(wfd, &sbuf))
245			err(1, "%s", filename);
246		bsize = MAX(sbuf.st_blksize, 1024);
247		if ((buf = malloc((u_int)bsize)) == NULL)
248			err(1, NULL);
249	}
250	while ((nr = read(rfd, buf, bsize)) > 0)
251		for (off = 0; nr; nr -= nw, off += nw)
252			if ((nw = write(wfd, buf + off, nr)) < 0)
253				err(1, "stdout");
254	if (nr < 0) {
255		warn("%s", filename);
256		rval = 1;
257	}
258}
259