2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static char const copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
47 static const char rcsid[] =
48 "$Id: cat.c,v 1.12 1998/09/11 05:48:06 imp Exp $";
51 #include <sys/param.h>
62 int bflag, eflag, nflag, sflag, tflag, vflag;
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));
79 setlocale(LC_CTYPE, "");
81 while ((ch = getopt(argc, argv, "benstuv")) != -1)
84 bflag = nflag = 1; /* -b implies -n */
87 eflag = vflag = 1; /* -e implies -v */
96 tflag = vflag = 1; /* -t implies -v */
99 setbuf(stdout, (char *)NULL);
105 (void)fprintf(stderr,
106 "usage: cat [-benstuv] [-] [file ...]\n");
111 if (bflag || eflag || nflag || sflag || tflag || vflag)
130 if (!strcmp(*argv, "-"))
132 else if ((fp = fopen(*argv, "r")) == NULL) {
150 register int ch, gobble, line, prev;
153 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
157 if (!gobble && putchar(ch) == EOF)
162 if (nflag && !bflag) {
163 (void)fprintf(stdout, "%6d\t", ++line);
168 (void)fprintf(stdout, "%6d\t", ++line);
176 if (putchar('$') == EOF)
178 } else if (ch == '\t') {
180 if (putchar('^') == EOF || putchar('I') == EOF)
185 if (!isascii(ch) && !isprint(ch)) {
186 if (putchar('M') == EOF || putchar('-') == EOF)
191 if (putchar('^') == EOF ||
192 putchar(ch == '\177' ? '?' :
198 if (putchar(ch) == EOF)
202 warn("%s", filename);
220 if (!strcmp(*argv, "-"))
222 else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
231 if (fd != fileno(stdin))
240 register int off, wfd;
246 wfd = fileno(stdout);
248 if (fstat(wfd, &sbuf))
249 err(1, "%s", filename);
250 bsize = MAX(sbuf.st_blksize, 1024);
251 if ((buf = malloc(bsize)) == NULL)
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)
259 warn("%s", filename);