cat.c revision 98169
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 37#ifndef lint 38static char const copyright[] = 39"@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95"; 46#endif 47static const char rcsid[] = 48 "$FreeBSD: head/bin/cat/cat.c 98169 2002-06-13 14:05:26Z tjr $"; 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/stat.h> 53#ifndef NO_UDOM_SUPPORT 54#include <sys/socket.h> 55#include <sys/un.h> 56#include <errno.h> 57#endif 58 59#include <ctype.h> 60#include <err.h> 61#include <fcntl.h> 62#include <locale.h> 63#include <stdio.h> 64#include <stdlib.h> 65#include <string.h> 66#include <unistd.h> 67#include <stddef.h> 68 69int bflag, eflag, nflag, sflag, tflag, vflag; 70int rval; 71const char *filename; 72 73static void scanfiles(char **argv, int cooked); 74static void cook_cat(FILE *); 75static void raw_cat(int); 76 77#ifndef NO_UDOM_SUPPORT 78static int udom_open(const char *path, int flags); 79#endif 80 81int 82main(int argc, char *argv[]) 83{ 84 int ch; 85 86 setlocale(LC_CTYPE, ""); 87 88 while ((ch = getopt(argc, argv, "benstuv")) != -1) 89 switch (ch) { 90 case 'b': 91 bflag = nflag = 1; /* -b implies -n */ 92 break; 93 case 'e': 94 eflag = vflag = 1; /* -e implies -v */ 95 break; 96 case 'n': 97 nflag = 1; 98 break; 99 case 's': 100 sflag = 1; 101 break; 102 case 't': 103 tflag = vflag = 1; /* -t implies -v */ 104 break; 105 case 'u': 106 setbuf(stdout, NULL); 107 break; 108 case 'v': 109 vflag = 1; 110 break; 111 default: 112 fprintf(stderr, 113 "usage: cat [-benstuv] [file ...]\n"); 114 exit(1); 115 } 116 argv += optind; 117 118 if (bflag || eflag || nflag || sflag || tflag || vflag) 119 scanfiles(argv, 1); 120 else 121 scanfiles(argv, 0); 122 if (fclose(stdout)) 123 err(1, "stdout"); 124 exit(rval); 125} 126 127void 128scanfiles(char **argv, int cooked) 129{ 130 int i = 0; 131 char *path; 132 FILE *fp; 133 134 while ((path = argv[i]) != NULL || i == 0) { 135 int fd; 136 137 if (path == NULL || strcmp(path, "-") == 0) { 138 filename = "stdin"; 139 fd = STDIN_FILENO; 140 } else { 141 filename = path; 142 fd = open(path, O_RDONLY); 143#ifndef NO_UDOM_SUPPORT 144 if (fd < 0 && errno == EOPNOTSUPP) 145 fd = udom_open(path, O_RDONLY); 146#endif 147 } 148 if (fd < 0) { 149 warn("%s", path); 150 rval = 1; 151 } else if (cooked) { 152 if (fd == STDIN_FILENO) 153 cook_cat(stdin); 154 else { 155 fp = fdopen(fd, "r"); 156 cook_cat(fp); 157 fclose(fp); 158 } 159 } else { 160 raw_cat(fd); 161 if (fd != STDIN_FILENO) 162 close(fd); 163 } 164 if (path == NULL) 165 break; 166 ++i; 167 } 168} 169 170static void 171cook_cat(FILE *fp) 172{ 173 int ch, gobble, line, prev; 174 175 /* Reset EOF condition on stdin. */ 176 if (fp == stdin && feof(stdin)) 177 clearerr(stdin); 178 179 line = gobble = 0; 180 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) { 181 if (prev == '\n') { 182 if (sflag) { 183 if (ch == '\n') { 184 if (gobble) 185 continue; 186 gobble = 1; 187 } else 188 gobble = 0; 189 } 190 if (nflag && (!bflag || ch != '\n')) { 191 (void)fprintf(stdout, "%6d\t", ++line); 192 if (ferror(stdout)) 193 break; 194 } 195 } 196 if (ch == '\n') { 197 if (eflag && putchar('$') == EOF) 198 break; 199 } else if (ch == '\t') { 200 if (tflag) { 201 if (putchar('^') == EOF || putchar('I') == EOF) 202 break; 203 continue; 204 } 205 } else if (vflag) { 206 if (!isascii(ch) && !isprint(ch)) { 207 if (putchar('M') == EOF || putchar('-') == EOF) 208 break; 209 ch = toascii(ch); 210 } 211 if (iscntrl(ch)) { 212 if (putchar('^') == EOF || 213 putchar(ch == '\177' ? '?' : 214 ch | 0100) == EOF) 215 break; 216 continue; 217 } 218 } 219 if (putchar(ch) == EOF) 220 break; 221 } 222 if (ferror(fp)) { 223 warn("%s", filename); 224 rval = 1; 225 clearerr(fp); 226 } 227 if (ferror(stdout)) 228 err(1, "stdout"); 229} 230 231static void 232raw_cat(int rfd) 233{ 234 int off, wfd; 235 ssize_t nr, nw; 236 static size_t bsize; 237 static char *buf = NULL; 238 struct stat sbuf; 239 240 wfd = fileno(stdout); 241 if (buf == NULL) { 242 if (fstat(wfd, &sbuf)) 243 err(1, "%s", filename); 244 bsize = MAX(sbuf.st_blksize, 1024); 245 if ((buf = malloc(bsize)) == NULL) 246 err(1, "buffer"); 247 } 248 while ((nr = read(rfd, buf, bsize)) > 0) 249 for (off = 0; nr; nr -= nw, off += nw) 250 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) 251 err(1, "stdout"); 252 if (nr < 0) { 253 warn("%s", filename); 254 rval = 1; 255 } 256} 257 258#ifndef NO_UDOM_SUPPORT 259 260static int 261udom_open(const char *path, int flags) 262{ 263 struct sockaddr_un sou; 264 int fd; 265 unsigned int len; 266 267 bzero(&sou, sizeof(sou)); 268 269 /* 270 * Construct the unix domain socket address and attempt to connect 271 */ 272 fd = socket(AF_UNIX, SOCK_STREAM, 0); 273 if (fd >= 0) { 274 sou.sun_family = AF_UNIX; 275 snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path); 276 len = strlen(sou.sun_path); 277 len = offsetof(struct sockaddr_un, sun_path[len+1]); 278 279 if (connect(fd, (void *)&sou, len) < 0) { 280 close(fd); 281 fd = -1; 282 } 283 } 284 285 /* 286 * handle the open flags by shutting down appropriate directions 287 */ 288 if (fd >= 0) { 289 switch(flags & O_ACCMODE) { 290 case O_RDONLY: 291 if (shutdown(fd, SHUT_WR) == -1) 292 perror("cat"); 293 break; 294 case O_WRONLY: 295 if (shutdown(fd, SHUT_RD) == -1) 296 perror("cat"); 297 break; 298 default: 299 break; 300 } 301 } 302 return(fd); 303} 304 305#endif 306