cat.c revision 1.18
1/*	$OpenBSD: cat.c,v 1.18 2007/03/22 18:35:07 millert 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.18 2007/03/22 18:35:07 millert 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			clearerr(fp);
144		else
145			(void)fclose(fp);
146	} while (*argv);
147}
148
149void
150cook_buf(FILE *fp)
151{
152	int ch, gobble, line, prev;
153
154	line = gobble = 0;
155	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
156		if (prev == '\n') {
157			if (sflag) {
158				if (ch == '\n') {
159					if (gobble)
160						continue;
161					gobble = 1;
162				} else
163					gobble = 0;
164			}
165			if (nflag && (!bflag || ch != '\n')) {
166				(void)fprintf(stdout, "%6d\t", ++line);
167				if (ferror(stdout))
168					break;
169			}
170		}
171		if (ch == '\n') {
172			if (eflag && putchar('$') == EOF)
173				break;
174		} else if (ch == '\t') {
175			if (tflag) {
176				if (putchar('^') == EOF || putchar('I') == EOF)
177					break;
178				continue;
179			}
180		} else if (vflag) {
181			if (!isascii(ch)) {
182				if (putchar('M') == EOF || putchar('-') == EOF)
183					break;
184				ch = toascii(ch);
185			}
186			if (iscntrl(ch)) {
187				if (putchar('^') == EOF ||
188				    putchar(ch == '\177' ? '?' :
189				    ch | 0100) == EOF)
190					break;
191				continue;
192			}
193		}
194		if (putchar(ch) == EOF)
195			break;
196	}
197	if (ferror(fp)) {
198		warn("%s", filename);
199		rval = 1;
200		clearerr(fp);
201	}
202	if (ferror(stdout))
203		err(1, "stdout");
204}
205
206void
207raw_args(char **argv)
208{
209	int fd;
210
211	fd = fileno(stdin);
212	filename = "stdin";
213	do {
214		if (*argv) {
215			if (!strcmp(*argv, "-"))
216				fd = fileno(stdin);
217			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
218				warn("%s", *argv);
219				rval = 1;
220				++argv;
221				continue;
222			}
223			filename = *argv++;
224		}
225		raw_cat(fd);
226		if (fd != fileno(stdin))
227			(void)close(fd);
228	} while (*argv);
229}
230
231void
232raw_cat(int rfd)
233{
234	int wfd;
235	ssize_t nr, nw, off;
236	static size_t bsize;
237	static char *buf = NULL;
238	struct stat sbuf;
239
240	wfd = fileno(stdout);
241	if (buf == NULL) {
242		if (fstat(wfd, &sbuf))
243			err(1, "stdout");
244		bsize = MAX(sbuf.st_blksize, BUFSIZ);
245		if ((buf = malloc(bsize)) == NULL)
246			err(1, "malloc");
247	}
248	while ((nr = read(rfd, buf, bsize)) != -1 && nr != 0)
249		for (off = 0; nr; nr -= nw, off += nw)
250			if ((nw = write(wfd, buf + off, (size_t)nr)) == 0 ||
251			     nw == -1)
252				err(1, "stdout");
253	if (nr < 0) {
254		warn("%s", filename);
255		rval = 1;
256	}
257}
258