Home | History | Annotate | Line # | Download | only in cat
      1 /* $NetBSD: cat.c,v 1.54 2013/12/08 08:32:13 spz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kevin Fall.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if !defined(lint)
     41 __COPYRIGHT(
     42 "@(#) Copyright (c) 1989, 1993\
     43  The Regents of the University of California.  All rights reserved.");
     44 #if 0
     45 static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
     46 #else
     47 __RCSID("$NetBSD: cat.c,v 1.54 2013/12/08 08:32:13 spz Exp $");
     48 #endif
     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 <errno.h>
     57 #include <fcntl.h>
     58 #include <locale.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 
     64 static int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
     65 static size_t bsize;
     66 static int rval;
     67 static const char *filename;
     68 
     69 void cook_args(char *argv[]);
     70 void cook_buf(FILE *);
     71 void raw_args(char *argv[]);
     72 void raw_cat(int);
     73 
     74 int
     75 main(int argc, char *argv[])
     76 {
     77 	int ch;
     78 	struct flock stdout_lock;
     79 
     80 	setprogname(argv[0]);
     81 	(void)setlocale(LC_ALL, "");
     82 
     83 	while ((ch = getopt(argc, argv, "B:beflnstuv")) != -1)
     84 		switch (ch) {
     85 		case 'B':
     86 			bsize = (size_t)strtol(optarg, NULL, 0);
     87 			break;
     88 		case 'b':
     89 			bflag = nflag = 1;	/* -b implies -n */
     90 			break;
     91 		case 'e':
     92 			eflag = vflag = 1;	/* -e implies -v */
     93 			break;
     94 		case 'f':
     95 			fflag = 1;
     96 			break;
     97 		case 'l':
     98 			lflag = 1;
     99 			break;
    100 		case 'n':
    101 			nflag = 1;
    102 			break;
    103 		case 's':
    104 			sflag = 1;
    105 			break;
    106 		case 't':
    107 			tflag = vflag = 1;	/* -t implies -v */
    108 			break;
    109 		case 'u':
    110 			setbuf(stdout, NULL);
    111 			break;
    112 		case 'v':
    113 			vflag = 1;
    114 			break;
    115 		default:
    116 			(void)fprintf(stderr,
    117 			    "Usage: %s [-beflnstuv] [-B bsize] [-] "
    118 			    "[file ...]\n", getprogname());
    119 			return EXIT_FAILURE;
    120 		}
    121 	argv += optind;
    122 
    123 	if (lflag) {
    124 		stdout_lock.l_len = 0;
    125 		stdout_lock.l_start = 0;
    126 		stdout_lock.l_type = F_WRLCK;
    127 		stdout_lock.l_whence = SEEK_SET;
    128 		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
    129 			err(EXIT_FAILURE, "stdout");
    130 	}
    131 
    132 	if (bflag || eflag || nflag || sflag || tflag || vflag)
    133 		cook_args(argv);
    134 	else
    135 		raw_args(argv);
    136 	if (fclose(stdout))
    137 		err(EXIT_FAILURE, "stdout");
    138 	return rval;
    139 }
    140 
    141 void
    142 cook_args(char **argv)
    143 {
    144 	FILE *fp;
    145 
    146 	fp = stdin;
    147 	filename = "stdin";
    148 	do {
    149 		if (*argv) {
    150 			if (!strcmp(*argv, "-"))
    151 				fp = stdin;
    152 			else if ((fp = fopen(*argv,
    153 			    fflag ? "rf" : "r")) == NULL) {
    154 				warn("%s", *argv);
    155 				rval = EXIT_FAILURE;
    156 				++argv;
    157 				continue;
    158 			}
    159 			filename = *argv++;
    160 		}
    161 		cook_buf(fp);
    162 		if (fp != stdin)
    163 			(void)fclose(fp);
    164 		else
    165 			clearerr(fp);
    166 	} while (*argv);
    167 }
    168 
    169 void
    170 cook_buf(FILE *fp)
    171 {
    172 	int ch, gobble, line, prev;
    173 
    174 	line = gobble = 0;
    175 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
    176 		if (prev == '\n') {
    177 			if (ch == '\n') {
    178 				if (sflag) {
    179 					if (!gobble && nflag && !bflag)
    180 						(void)fprintf(stdout,
    181 							"%6d\t\n", ++line);
    182 					else if (!gobble && putchar(ch) == EOF)
    183 						break;
    184 					gobble = 1;
    185 					continue;
    186 				}
    187 				if (nflag) {
    188 					if (!bflag) {
    189 						(void)fprintf(stdout,
    190 						    "%6d\t", ++line);
    191 						if (ferror(stdout))
    192 							break;
    193 					} else if (eflag) {
    194 						(void)fprintf(stdout,
    195 						    "%6s\t", "");
    196 						if (ferror(stdout))
    197 							break;
    198 					}
    199 				}
    200 			} else if (nflag) {
    201 				(void)fprintf(stdout, "%6d\t", ++line);
    202 				if (ferror(stdout))
    203 					break;
    204 			}
    205 		}
    206 		gobble = 0;
    207 		if (ch == '\n') {
    208 			if (eflag)
    209 				if (putchar('$') == EOF)
    210 					break;
    211 		} else if (ch == '\t') {
    212 			if (tflag) {
    213 				if (putchar('^') == EOF || putchar('I') == EOF)
    214 					break;
    215 				continue;
    216 			}
    217 		} else if (vflag) {
    218 			if (!isascii(ch)) {
    219 				if (putchar('M') == EOF || putchar('-') == EOF)
    220 					break;
    221 				ch = toascii(ch);
    222 			}
    223 			if (iscntrl(ch)) {
    224 				if (putchar('^') == EOF ||
    225 				    putchar(ch == '\177' ? '?' :
    226 				    ch | 0100) == EOF)
    227 					break;
    228 				continue;
    229 			}
    230 		}
    231 		if (putchar(ch) == EOF)
    232 			break;
    233 	}
    234 	if (ferror(fp)) {
    235 		warn("%s", filename);
    236 		rval = EXIT_FAILURE;
    237 		clearerr(fp);
    238 	}
    239 	if (ferror(stdout))
    240 		err(EXIT_FAILURE, "stdout");
    241 }
    242 
    243 void
    244 raw_args(char **argv)
    245 {
    246 	int fd;
    247 
    248 	fd = fileno(stdin);
    249 	filename = "stdin";
    250 	do {
    251 		if (*argv) {
    252 			if (!strcmp(*argv, "-")) {
    253 				fd = fileno(stdin);
    254 				if (fd < 0)
    255 					goto skip;
    256 			} else if (fflag) {
    257 				struct stat st;
    258 				fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
    259 				if (fd < 0)
    260 					goto skip;
    261 
    262 				if (fstat(fd, &st) == -1) {
    263 					close(fd);
    264 					goto skip;
    265 				}
    266 				if (!S_ISREG(st.st_mode)) {
    267 					close(fd);
    268 					warnx("%s: not a regular file", *argv);
    269 					goto skipnomsg;
    270 				}
    271 			}
    272 			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
    273 skip:
    274 				warn("%s", *argv);
    275 skipnomsg:
    276 				rval = EXIT_FAILURE;
    277 				++argv;
    278 				continue;
    279 			}
    280 			filename = *argv++;
    281 		} else if (fd < 0) {
    282 			err(EXIT_FAILURE, "stdin");
    283 		}
    284 		raw_cat(fd);
    285 		if (fd != fileno(stdin))
    286 			(void)close(fd);
    287 	} while (*argv);
    288 }
    289 
    290 void
    291 raw_cat(int rfd)
    292 {
    293 	static char *buf;
    294 	static char fb_buf[BUFSIZ];
    295 
    296 	ssize_t nr, nw, off;
    297 	int wfd;
    298 
    299 	wfd = fileno(stdout);
    300 	if (wfd < 0)
    301 		err(EXIT_FAILURE, "stdout");
    302 	if (buf == NULL) {
    303 		struct stat sbuf;
    304 
    305 		if (bsize == 0) {
    306 			if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > 0 &&
    307 			    (size_t)sbuf.st_blksize > sizeof(fb_buf))
    308 				bsize = sbuf.st_blksize;
    309 		}
    310 		if (bsize > sizeof(fb_buf)) {
    311 			buf = malloc(bsize);
    312 			if (buf == NULL)
    313 				warnx("malloc, using %zu buffer", bsize);
    314 		}
    315 		if (buf == NULL) {
    316 			bsize = sizeof(fb_buf);
    317 			buf = fb_buf;
    318 		}
    319 	}
    320 	while ((nr = read(rfd, buf, bsize)) > 0)
    321 		for (off = 0; nr; nr -= nw, off += nw)
    322 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
    323 				err(EXIT_FAILURE, "stdout");
    324 	if (nr < 0) {
    325 		warn("%s", filename);
    326 		rval = EXIT_FAILURE;
    327 	}
    328 }
    329