xref: /freebsd/bin/cat/cat.c (revision 43e09ab2dbde5eb3737e816a0d13b329b5ad07dd)
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
38890acb95SSteve Price static char const 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
444c95995fSPhilippe Charnier #if 0
4512f93eb9SPhilippe Charnier static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
464c95995fSPhilippe Charnier #endif
474c95995fSPhilippe Charnier static const char rcsid[] =
482a456239SPeter Wemm   "$FreeBSD$";
494b88c807SRodney W. Grimes #endif /* not lint */
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes #include <sys/param.h>
524b88c807SRodney W. Grimes #include <sys/stat.h>
53cbf2d71fSMatthew Dillon #ifndef NO_UDOM_SUPPORT
54cbf2d71fSMatthew Dillon #include <sys/socket.h>
55cbf2d71fSMatthew Dillon #include <sys/un.h>
56cbf2d71fSMatthew Dillon #include <errno.h>
57cbf2d71fSMatthew Dillon #endif
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes #include <ctype.h>
604b88c807SRodney W. Grimes #include <err.h>
614b88c807SRodney W. Grimes #include <fcntl.h>
623043192bSAndrey A. Chernov #include <locale.h>
634b88c807SRodney W. Grimes #include <stdio.h>
644b88c807SRodney W. Grimes #include <stdlib.h>
65cafefe8cSDima Dorfman #include <string.h>
664b88c807SRodney W. Grimes #include <unistd.h>
67cbf2d71fSMatthew Dillon #include <stddef.h>
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes int bflag, eflag, nflag, sflag, tflag, vflag;
704b88c807SRodney W. Grimes int rval;
71a5da0999SWarner Losh const char *filename;
724b88c807SRodney W. Grimes 
73d1762d1fSWarner Losh int main __P((int argc, char *argv[]));
74cbf2d71fSMatthew Dillon 
75cbf2d71fSMatthew Dillon static void scanfiles __P((char **argv, int cooked));
76cbf2d71fSMatthew Dillon static void cook_cat __P((FILE *));
77cbf2d71fSMatthew Dillon static void raw_cat __P((int));
78cbf2d71fSMatthew Dillon 
79cbf2d71fSMatthew Dillon #ifndef NO_UDOM_SUPPORT
8043e09ab2SRuslan Ermilov static int udom_open __P((const char *path, int flags));
81cbf2d71fSMatthew Dillon #endif
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes int
84d1762d1fSWarner Losh main(argc, argv)
85d1762d1fSWarner Losh 	int argc;
86d1762d1fSWarner Losh 	char *argv[];
874b88c807SRodney W. Grimes {
884b88c807SRodney W. Grimes 	int ch;
894b88c807SRodney W. Grimes 
903043192bSAndrey A. Chernov 	setlocale(LC_CTYPE, "");
913043192bSAndrey A. Chernov 
9293ef08afSWarner Losh 	while ((ch = getopt(argc, argv, "benstuv")) != -1)
934b88c807SRodney W. Grimes 		switch (ch) {
944b88c807SRodney W. Grimes 		case 'b':
954b88c807SRodney W. Grimes 			bflag = nflag = 1;	/* -b implies -n */
964b88c807SRodney W. Grimes 			break;
974b88c807SRodney W. Grimes 		case 'e':
984b88c807SRodney W. Grimes 			eflag = vflag = 1;	/* -e implies -v */
994b88c807SRodney W. Grimes 			break;
1004b88c807SRodney W. Grimes 		case 'n':
1014b88c807SRodney W. Grimes 			nflag = 1;
1024b88c807SRodney W. Grimes 			break;
1034b88c807SRodney W. Grimes 		case 's':
1044b88c807SRodney W. Grimes 			sflag = 1;
1054b88c807SRodney W. Grimes 			break;
1064b88c807SRodney W. Grimes 		case 't':
1074b88c807SRodney W. Grimes 			tflag = vflag = 1;	/* -t implies -v */
1084b88c807SRodney W. Grimes 			break;
1094b88c807SRodney W. Grimes 		case 'u':
1102192b407SJeroen Ruigrok van der Werven 			setbuf(stdout, NULL);
1114b88c807SRodney W. Grimes 			break;
1124b88c807SRodney W. Grimes 		case 'v':
1134b88c807SRodney W. Grimes 			vflag = 1;
1144b88c807SRodney W. Grimes 			break;
1158d72a3d7SWarner Losh 		default:
1164b88c807SRodney W. Grimes 			(void)fprintf(stderr,
1174b88c807SRodney W. Grimes 			    "usage: cat [-benstuv] [-] [file ...]\n");
1184b88c807SRodney W. Grimes 			exit(1);
1194b88c807SRodney W. Grimes 		}
1204b88c807SRodney W. Grimes 	argv += optind;
1214b88c807SRodney W. Grimes 
1224b88c807SRodney W. Grimes 	if (bflag || eflag || nflag || sflag || tflag || vflag)
123cbf2d71fSMatthew Dillon 		scanfiles(argv, 1);
1244b88c807SRodney W. Grimes 	else
125cbf2d71fSMatthew Dillon 		scanfiles(argv, 0);
1264b88c807SRodney W. Grimes 	if (fclose(stdout))
1274b88c807SRodney W. Grimes 		err(1, "stdout");
1284b88c807SRodney W. Grimes 	exit(rval);
1294b88c807SRodney W. Grimes }
1304b88c807SRodney W. Grimes 
1314b88c807SRodney W. Grimes void
132cbf2d71fSMatthew Dillon scanfiles(argv, cooked)
1334b88c807SRodney W. Grimes     char **argv;
134cbf2d71fSMatthew Dillon     int cooked;
1354b88c807SRodney W. Grimes {
136cbf2d71fSMatthew Dillon 	int i = 0;
137cbf2d71fSMatthew Dillon 	char *path;
1381b00c916SRuslan Ermilov 	FILE *fp;
1394b88c807SRodney W. Grimes 
140cbf2d71fSMatthew Dillon 	while ((path = argv[i]) != NULL || i == 0) {
141cbf2d71fSMatthew Dillon 		int fd;
142cbf2d71fSMatthew Dillon 
143cbf2d71fSMatthew Dillon 		if (path == NULL || strcmp(path, "-") == 0) {
1444b88c807SRodney W. Grimes 			filename = "stdin";
1451b00c916SRuslan Ermilov 			fd = STDIN_FILENO;
146cbf2d71fSMatthew Dillon 		} else {
147cbf2d71fSMatthew Dillon 			filename = path;
148cbf2d71fSMatthew Dillon 			fd = open(path, O_RDONLY);
149cbf2d71fSMatthew Dillon #ifndef NO_UDOM_SUPPORT
150cbf2d71fSMatthew Dillon 			if (fd < 0 && errno == EOPNOTSUPP)
15143e09ab2SRuslan Ermilov 				fd = udom_open(path, O_RDONLY);
152cbf2d71fSMatthew Dillon #endif
153cbf2d71fSMatthew Dillon 		}
154cbf2d71fSMatthew Dillon 		if (fd < 0) {
155cbf2d71fSMatthew Dillon 			warn("%s", path);
156001aff9fSBruce Evans 			rval = 1;
157cbf2d71fSMatthew Dillon 		} else if (cooked) {
1581b00c916SRuslan Ermilov 			if (fd == STDIN_FILENO)
1591b00c916SRuslan Ermilov 				cook_cat(stdin);
1601b00c916SRuslan Ermilov 			else {
1611b00c916SRuslan Ermilov 				fp = fdopen(fd, "r");
162cbf2d71fSMatthew Dillon 				cook_cat(fp);
163cbf2d71fSMatthew Dillon 				fclose(fp);
1641b00c916SRuslan Ermilov 			}
165cbf2d71fSMatthew Dillon 		} else {
166cbf2d71fSMatthew Dillon 			raw_cat(fd);
1671b00c916SRuslan Ermilov 			if (fd != STDIN_FILENO)
168cbf2d71fSMatthew Dillon 				close(fd);
1694b88c807SRodney W. Grimes 		}
170cbf2d71fSMatthew Dillon 		if (path == NULL)
171cbf2d71fSMatthew Dillon 			break;
172cbf2d71fSMatthew Dillon 		++i;
1734b88c807SRodney W. Grimes 	}
1744b88c807SRodney W. Grimes }
1754b88c807SRodney W. Grimes 
176cbf2d71fSMatthew Dillon static void
177cbf2d71fSMatthew Dillon cook_cat(fp)
1784b88c807SRodney W. Grimes 	register FILE *fp;
1794b88c807SRodney W. Grimes {
1804b88c807SRodney W. Grimes 	register int ch, gobble, line, prev;
1814b88c807SRodney W. Grimes 
1821b00c916SRuslan Ermilov 	/* Reset EOF condition on stdin. */
1831b00c916SRuslan Ermilov 	if (fp == stdin && feof(stdin))
1841b00c916SRuslan Ermilov 		clearerr(stdin);
1851b00c916SRuslan Ermilov 
1864b88c807SRodney W. Grimes 	line = gobble = 0;
1874b88c807SRodney W. Grimes 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
1884b88c807SRodney W. Grimes 		if (prev == '\n') {
1894b88c807SRodney W. Grimes 			if (ch == '\n') {
1904b88c807SRodney W. Grimes 				if (sflag) {
1914b88c807SRodney W. Grimes 					if (!gobble && putchar(ch) == EOF)
1924b88c807SRodney W. Grimes 						break;
1934b88c807SRodney W. Grimes 					gobble = 1;
1944b88c807SRodney W. Grimes 					continue;
1954b88c807SRodney W. Grimes 				}
1964b88c807SRodney W. Grimes 				if (nflag && !bflag) {
1974b88c807SRodney W. Grimes 					(void)fprintf(stdout, "%6d\t", ++line);
1984b88c807SRodney W. Grimes 					if (ferror(stdout))
1994b88c807SRodney W. Grimes 						break;
2004b88c807SRodney W. Grimes 				}
2014b88c807SRodney W. Grimes 			} else if (nflag) {
2024b88c807SRodney W. Grimes 				(void)fprintf(stdout, "%6d\t", ++line);
2034b88c807SRodney W. Grimes 				if (ferror(stdout))
2044b88c807SRodney W. Grimes 					break;
2054b88c807SRodney W. Grimes 			}
2064b88c807SRodney W. Grimes 		}
2074b88c807SRodney W. Grimes 		gobble = 0;
2084b88c807SRodney W. Grimes 		if (ch == '\n') {
2094b88c807SRodney W. Grimes 			if (eflag)
2104b88c807SRodney W. Grimes 				if (putchar('$') == EOF)
2114b88c807SRodney W. Grimes 					break;
2124b88c807SRodney W. Grimes 		} else if (ch == '\t') {
2134b88c807SRodney W. Grimes 			if (tflag) {
2144b88c807SRodney W. Grimes 				if (putchar('^') == EOF || putchar('I') == EOF)
2154b88c807SRodney W. Grimes 					break;
2164b88c807SRodney W. Grimes 				continue;
2174b88c807SRodney W. Grimes 			}
2184b88c807SRodney W. Grimes 		} else if (vflag) {
2193043192bSAndrey A. Chernov 			if (!isascii(ch) && !isprint(ch)) {
2204b88c807SRodney W. Grimes 				if (putchar('M') == EOF || putchar('-') == EOF)
2214b88c807SRodney W. Grimes 					break;
2224b88c807SRodney W. Grimes 				ch = toascii(ch);
2234b88c807SRodney W. Grimes 			}
2244b88c807SRodney W. Grimes 			if (iscntrl(ch)) {
2254b88c807SRodney W. Grimes 				if (putchar('^') == EOF ||
2264b88c807SRodney W. Grimes 				    putchar(ch == '\177' ? '?' :
2274b88c807SRodney W. Grimes 				    ch | 0100) == EOF)
2284b88c807SRodney W. Grimes 					break;
2294b88c807SRodney W. Grimes 				continue;
2304b88c807SRodney W. Grimes 			}
2314b88c807SRodney W. Grimes 		}
2324b88c807SRodney W. Grimes 		if (putchar(ch) == EOF)
2334b88c807SRodney W. Grimes 			break;
2344b88c807SRodney W. Grimes 	}
2354b88c807SRodney W. Grimes 	if (ferror(fp)) {
2364b88c807SRodney W. Grimes 		warn("%s", filename);
237001aff9fSBruce Evans 		rval = 1;
2384b88c807SRodney W. Grimes 		clearerr(fp);
2394b88c807SRodney W. Grimes 	}
2404b88c807SRodney W. Grimes 	if (ferror(stdout))
2414b88c807SRodney W. Grimes 		err(1, "stdout");
2424b88c807SRodney W. Grimes }
2434b88c807SRodney W. Grimes 
244cbf2d71fSMatthew Dillon static void
2454b88c807SRodney W. Grimes raw_cat(rfd)
2464b88c807SRodney W. Grimes 	register int rfd;
2474b88c807SRodney W. Grimes {
248a5da0999SWarner Losh 	register int off, wfd;
249a5da0999SWarner Losh 	ssize_t nr, nw;
250a5da0999SWarner Losh 	static size_t bsize;
2514b88c807SRodney W. Grimes 	static char *buf;
2524b88c807SRodney W. Grimes 	struct stat sbuf;
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 	wfd = fileno(stdout);
2554b88c807SRodney W. Grimes 	if (buf == NULL) {
2564b88c807SRodney W. Grimes 		if (fstat(wfd, &sbuf))
2574b88c807SRodney W. Grimes 			err(1, "%s", filename);
2584b88c807SRodney W. Grimes 		bsize = MAX(sbuf.st_blksize, 1024);
259d1762d1fSWarner Losh 		if ((buf = malloc(bsize)) == NULL)
2602192b407SJeroen Ruigrok van der Werven 			err(1, "buffer");
2614b88c807SRodney W. Grimes 	}
2624b88c807SRodney W. Grimes 	while ((nr = read(rfd, buf, bsize)) > 0)
2634b88c807SRodney W. Grimes 		for (off = 0; nr; nr -= nw, off += nw)
264a5da0999SWarner Losh 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
2654b88c807SRodney W. Grimes 				err(1, "stdout");
266001aff9fSBruce Evans 	if (nr < 0) {
2674b88c807SRodney W. Grimes 		warn("%s", filename);
268001aff9fSBruce Evans 		rval = 1;
269001aff9fSBruce Evans 	}
2704b88c807SRodney W. Grimes }
271cbf2d71fSMatthew Dillon 
272cbf2d71fSMatthew Dillon #ifndef NO_UDOM_SUPPORT
273cbf2d71fSMatthew Dillon 
274cbf2d71fSMatthew Dillon static int
27543e09ab2SRuslan Ermilov udom_open(path, flags)
276cbf2d71fSMatthew Dillon     const char *path;
277cbf2d71fSMatthew Dillon     int flags;
278cbf2d71fSMatthew Dillon {
279cbf2d71fSMatthew Dillon 	struct sockaddr_un sou;
280cbf2d71fSMatthew Dillon 	int fd;
281cbf2d71fSMatthew Dillon 	int len;
282cbf2d71fSMatthew Dillon 
283cbf2d71fSMatthew Dillon 	bzero(&sou, sizeof(sou));
284cbf2d71fSMatthew Dillon 
285cbf2d71fSMatthew Dillon 	/*
286cbf2d71fSMatthew Dillon 	 * Construct the unix domain socket address and attempt to connect
287cbf2d71fSMatthew Dillon 	 */
288cbf2d71fSMatthew Dillon 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
289cbf2d71fSMatthew Dillon 		sou.sun_family = AF_UNIX;
290cbf2d71fSMatthew Dillon 		snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path);
291cbf2d71fSMatthew Dillon 		len = strlen(sou.sun_path);
292cbf2d71fSMatthew Dillon 		len = offsetof(struct sockaddr_un, sun_path[len+1]);
293cbf2d71fSMatthew Dillon 
294cbf2d71fSMatthew Dillon 		if (connect(fd, (void *)&sou, len) < 0) {
295cbf2d71fSMatthew Dillon 			close(fd);
296cbf2d71fSMatthew Dillon 			fd = -1;
297cbf2d71fSMatthew Dillon 		}
298cbf2d71fSMatthew Dillon 	}
299cbf2d71fSMatthew Dillon 
300cbf2d71fSMatthew Dillon 	/*
301cbf2d71fSMatthew Dillon 	 * handle the open flags by shutting down appropriate directions
302cbf2d71fSMatthew Dillon 	 */
303cbf2d71fSMatthew Dillon 	if (fd >= 0) {
304cbf2d71fSMatthew Dillon 		switch(flags & O_ACCMODE) {
305cbf2d71fSMatthew Dillon 		case O_RDONLY:
306cbf2d71fSMatthew Dillon 			shutdown(fd, SHUT_WR);
307cbf2d71fSMatthew Dillon 			break;
308cbf2d71fSMatthew Dillon 		case O_WRONLY:
309cbf2d71fSMatthew Dillon 			shutdown(fd, SHUT_RD);
310cbf2d71fSMatthew Dillon 			break;
311cbf2d71fSMatthew Dillon 		default:
312cbf2d71fSMatthew Dillon 			break;
313cbf2d71fSMatthew Dillon 		}
314cbf2d71fSMatthew Dillon 	}
315cbf2d71fSMatthew Dillon 	return(fd);
316cbf2d71fSMatthew Dillon }
317cbf2d71fSMatthew Dillon 
318cbf2d71fSMatthew Dillon #endif
319cbf2d71fSMatthew Dillon 
320