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