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