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