xref: /freebsd/bin/cat/cat.c (revision 4b88c807ea9c629dc5691abc7e3cac9ea0d776dd)
14b88c807SRodney W. Grimes /*
24b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kevin Fall.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
384b88c807SRodney W. Grimes static char copyright[] =
394b88c807SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
404b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
434b88c807SRodney W. Grimes #ifndef lint
444b88c807SRodney W. Grimes static char sccsid[] = "@(#)cat.c	8.1 (Berkeley) 7/19/93";
454b88c807SRodney W. Grimes #endif /* not lint */
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes #include <sys/param.h>
484b88c807SRodney W. Grimes #include <sys/stat.h>
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include <ctype.h>
514b88c807SRodney W. Grimes #include <err.h>
524b88c807SRodney W. Grimes #include <errno.h>
534b88c807SRodney W. Grimes #include <fcntl.h>
544b88c807SRodney W. Grimes #include <stdio.h>
554b88c807SRodney W. Grimes #include <stdlib.h>
564b88c807SRodney W. Grimes #include <string.h>
574b88c807SRodney W. Grimes #include <unistd.h>
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes int bflag, eflag, nflag, sflag, tflag, vflag;
604b88c807SRodney W. Grimes int rval;
614b88c807SRodney W. Grimes char *filename;
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes void cook_args __P((char *argv[]));
644b88c807SRodney W. Grimes void cook_buf __P((FILE *));
654b88c807SRodney W. Grimes void raw_args __P((char *argv[]));
664b88c807SRodney W. Grimes void raw_cat __P((int));
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes int
694b88c807SRodney W. Grimes main(argc, argv)
704b88c807SRodney W. Grimes 	int argc;
714b88c807SRodney W. Grimes 	char *argv[];
724b88c807SRodney W. Grimes {
734b88c807SRodney W. Grimes 	extern int optind;
744b88c807SRodney W. Grimes 	int ch;
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 	while ((ch = getopt(argc, argv, "benstuv")) != EOF)
774b88c807SRodney W. Grimes 		switch (ch) {
784b88c807SRodney W. Grimes 		case 'b':
794b88c807SRodney W. Grimes 			bflag = nflag = 1;	/* -b implies -n */
804b88c807SRodney W. Grimes 			break;
814b88c807SRodney W. Grimes 		case 'e':
824b88c807SRodney W. Grimes 			eflag = vflag = 1;	/* -e implies -v */
834b88c807SRodney W. Grimes 			break;
844b88c807SRodney W. Grimes 		case 'n':
854b88c807SRodney W. Grimes 			nflag = 1;
864b88c807SRodney W. Grimes 			break;
874b88c807SRodney W. Grimes 		case 's':
884b88c807SRodney W. Grimes 			sflag = 1;
894b88c807SRodney W. Grimes 			break;
904b88c807SRodney W. Grimes 		case 't':
914b88c807SRodney W. Grimes 			tflag = vflag = 1;	/* -t implies -v */
924b88c807SRodney W. Grimes 			break;
934b88c807SRodney W. Grimes 		case 'u':
944b88c807SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
954b88c807SRodney W. Grimes 			break;
964b88c807SRodney W. Grimes 		case 'v':
974b88c807SRodney W. Grimes 			vflag = 1;
984b88c807SRodney W. Grimes 			break;
994b88c807SRodney W. Grimes 		case '?':
1004b88c807SRodney W. Grimes 			(void)fprintf(stderr,
1014b88c807SRodney W. Grimes 			    "usage: cat [-benstuv] [-] [file ...]\n");
1024b88c807SRodney W. Grimes 			exit(1);
1034b88c807SRodney W. Grimes 		}
1044b88c807SRodney W. Grimes 	argv += optind;
1054b88c807SRodney W. Grimes 
1064b88c807SRodney W. Grimes 	if (bflag || eflag || nflag || sflag || tflag || vflag)
1074b88c807SRodney W. Grimes 		cook_args(argv);
1084b88c807SRodney W. Grimes 	else
1094b88c807SRodney W. Grimes 		raw_args(argv);
1104b88c807SRodney W. Grimes 	if (fclose(stdout))
1114b88c807SRodney W. Grimes 		err(1, "stdout");
1124b88c807SRodney W. Grimes 	exit(rval);
1134b88c807SRodney W. Grimes }
1144b88c807SRodney W. Grimes 
1154b88c807SRodney W. Grimes void
1164b88c807SRodney W. Grimes cook_args(argv)
1174b88c807SRodney W. Grimes 	char **argv;
1184b88c807SRodney W. Grimes {
1194b88c807SRodney W. Grimes 	register FILE *fp;
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes 	fp = stdin;
1224b88c807SRodney W. Grimes 	filename = "stdin";
1234b88c807SRodney W. Grimes 	do {
1244b88c807SRodney W. Grimes 		if (*argv) {
1254b88c807SRodney W. Grimes 			if (!strcmp(*argv, "-"))
1264b88c807SRodney W. Grimes 				fp = stdin;
1274b88c807SRodney W. Grimes 			else if ((fp = fopen(*argv, "r")) == NULL) {
1284b88c807SRodney W. Grimes 				warn("%s", *argv);
1294b88c807SRodney W. Grimes 				++argv;
1304b88c807SRodney W. Grimes 				continue;
1314b88c807SRodney W. Grimes 			}
1324b88c807SRodney W. Grimes 			filename = *argv++;
1334b88c807SRodney W. Grimes 		}
1344b88c807SRodney W. Grimes 		cook_buf(fp);
1354b88c807SRodney W. Grimes 		if (fp != stdin)
1364b88c807SRodney W. Grimes 			(void)fclose(fp);
1374b88c807SRodney W. Grimes 	} while (*argv);
1384b88c807SRodney W. Grimes }
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes void
1414b88c807SRodney W. Grimes cook_buf(fp)
1424b88c807SRodney W. Grimes 	register FILE *fp;
1434b88c807SRodney W. Grimes {
1444b88c807SRodney W. Grimes 	register int ch, gobble, line, prev;
1454b88c807SRodney W. Grimes 
1464b88c807SRodney W. Grimes 	line = gobble = 0;
1474b88c807SRodney W. Grimes 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
1484b88c807SRodney W. Grimes 		if (prev == '\n') {
1494b88c807SRodney W. Grimes 			if (ch == '\n') {
1504b88c807SRodney W. Grimes 				if (sflag) {
1514b88c807SRodney W. Grimes 					if (!gobble && putchar(ch) == EOF)
1524b88c807SRodney W. Grimes 						break;
1534b88c807SRodney W. Grimes 					gobble = 1;
1544b88c807SRodney W. Grimes 					continue;
1554b88c807SRodney W. Grimes 				}
1564b88c807SRodney W. Grimes 				if (nflag && !bflag) {
1574b88c807SRodney W. Grimes 					(void)fprintf(stdout, "%6d\t", ++line);
1584b88c807SRodney W. Grimes 					if (ferror(stdout))
1594b88c807SRodney W. Grimes 						break;
1604b88c807SRodney W. Grimes 				}
1614b88c807SRodney W. Grimes 			} else if (nflag) {
1624b88c807SRodney W. Grimes 				(void)fprintf(stdout, "%6d\t", ++line);
1634b88c807SRodney W. Grimes 				if (ferror(stdout))
1644b88c807SRodney W. Grimes 					break;
1654b88c807SRodney W. Grimes 			}
1664b88c807SRodney W. Grimes 		}
1674b88c807SRodney W. Grimes 		gobble = 0;
1684b88c807SRodney W. Grimes 		if (ch == '\n') {
1694b88c807SRodney W. Grimes 			if (eflag)
1704b88c807SRodney W. Grimes 				if (putchar('$') == EOF)
1714b88c807SRodney W. Grimes 					break;
1724b88c807SRodney W. Grimes 		} else if (ch == '\t') {
1734b88c807SRodney W. Grimes 			if (tflag) {
1744b88c807SRodney W. Grimes 				if (putchar('^') == EOF || putchar('I') == EOF)
1754b88c807SRodney W. Grimes 					break;
1764b88c807SRodney W. Grimes 				continue;
1774b88c807SRodney W. Grimes 			}
1784b88c807SRodney W. Grimes 		} else if (vflag) {
1794b88c807SRodney W. Grimes 			if (!isascii(ch)) {
1804b88c807SRodney W. Grimes 				if (putchar('M') == EOF || putchar('-') == EOF)
1814b88c807SRodney W. Grimes 					break;
1824b88c807SRodney W. Grimes 				ch = toascii(ch);
1834b88c807SRodney W. Grimes 			}
1844b88c807SRodney W. Grimes 			if (iscntrl(ch)) {
1854b88c807SRodney W. Grimes 				if (putchar('^') == EOF ||
1864b88c807SRodney W. Grimes 				    putchar(ch == '\177' ? '?' :
1874b88c807SRodney W. Grimes 				    ch | 0100) == EOF)
1884b88c807SRodney W. Grimes 					break;
1894b88c807SRodney W. Grimes 				continue;
1904b88c807SRodney W. Grimes 			}
1914b88c807SRodney W. Grimes 		}
1924b88c807SRodney W. Grimes 		if (putchar(ch) == EOF)
1934b88c807SRodney W. Grimes 			break;
1944b88c807SRodney W. Grimes 	}
1954b88c807SRodney W. Grimes 	if (ferror(fp)) {
1964b88c807SRodney W. Grimes 		warn("%s", filename);
1974b88c807SRodney W. Grimes 		clearerr(fp);
1984b88c807SRodney W. Grimes 	}
1994b88c807SRodney W. Grimes 	if (ferror(stdout))
2004b88c807SRodney W. Grimes 		err(1, "stdout");
2014b88c807SRodney W. Grimes }
2024b88c807SRodney W. Grimes 
2034b88c807SRodney W. Grimes void
2044b88c807SRodney W. Grimes raw_args(argv)
2054b88c807SRodney W. Grimes 	char **argv;
2064b88c807SRodney W. Grimes {
2074b88c807SRodney W. Grimes 	register int fd;
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes 	fd = fileno(stdin);
2104b88c807SRodney W. Grimes 	filename = "stdin";
2114b88c807SRodney W. Grimes 	do {
2124b88c807SRodney W. Grimes 		if (*argv) {
2134b88c807SRodney W. Grimes 			if (!strcmp(*argv, "-"))
2144b88c807SRodney W. Grimes 				fd = fileno(stdin);
2154b88c807SRodney W. Grimes 			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
2164b88c807SRodney W. Grimes 				warn("%s", *argv);
2174b88c807SRodney W. Grimes 				++argv;
2184b88c807SRodney W. Grimes 				continue;
2194b88c807SRodney W. Grimes 			}
2204b88c807SRodney W. Grimes 			filename = *argv++;
2214b88c807SRodney W. Grimes 		}
2224b88c807SRodney W. Grimes 		raw_cat(fd);
2234b88c807SRodney W. Grimes 		if (fd != fileno(stdin))
2244b88c807SRodney W. Grimes 			(void)close(fd);
2254b88c807SRodney W. Grimes 	} while (*argv);
2264b88c807SRodney W. Grimes }
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes void
2294b88c807SRodney W. Grimes raw_cat(rfd)
2304b88c807SRodney W. Grimes 	register int rfd;
2314b88c807SRodney W. Grimes {
2324b88c807SRodney W. Grimes 	register int nr, nw, off, wfd;
2334b88c807SRodney W. Grimes 	static int bsize;
2344b88c807SRodney W. Grimes 	static char *buf;
2354b88c807SRodney W. Grimes 	struct stat sbuf;
2364b88c807SRodney W. Grimes 
2374b88c807SRodney W. Grimes 	wfd = fileno(stdout);
2384b88c807SRodney W. Grimes 	if (buf == NULL) {
2394b88c807SRodney W. Grimes 		if (fstat(wfd, &sbuf))
2404b88c807SRodney W. Grimes 			err(1, "%s", filename);
2414b88c807SRodney W. Grimes 		bsize = MAX(sbuf.st_blksize, 1024);
2424b88c807SRodney W. Grimes 		if ((buf = malloc((u_int)bsize)) == NULL)
2434b88c807SRodney W. Grimes 			err(1, "");
2444b88c807SRodney W. Grimes 	}
2454b88c807SRodney W. Grimes 	while ((nr = read(rfd, buf, bsize)) > 0)
2464b88c807SRodney W. Grimes 		for (off = 0; nr; nr -= nw, off += nw)
2474b88c807SRodney W. Grimes 			if ((nw = write(wfd, buf + off, nr)) < 0)
2484b88c807SRodney W. Grimes 				err(1, "stdout");
2494b88c807SRodney W. Grimes 	if (nr < 0)
2504b88c807SRodney W. Grimes 		warn("%s", filename);
2514b88c807SRodney W. Grimes }
252