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