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