cat.c revision 98216
164126Skato/* 279755Sdd * Copyright (c) 1989, 1993 364126Skato * The Regents of the University of California. All rights reserved. 464126Skato * 579755Sdd * This code is derived from software contributed to Berkeley by 664126Skato * Kevin Fall. 764126Skato * 864126Skato * Redistribution and use in source and binary forms, with or without 979755Sdd * modification, are permitted provided that the following conditions 1064126Skato * are met: 1164126Skato * 1. Redistributions of source code must retain the above copyright 1264126Skato * notice, this list of conditions and the following disclaimer. 1364126Skato * 2. Redistributions in binary form must reproduce the above copyright 1464126Skato * notice, this list of conditions and the following disclaimer in the 1564126Skato * documentation and/or other materials provided with the distribution. 1664126Skato * 3. All advertising materials mentioning features or use of this software 1764126Skato * must display the following acknowledgement: 1879755Sdd * This product includes software developed by the University of 1964126Skato * California, Berkeley and its contributors. 2064126Skato * 4. Neither the name of the University nor the names of its contributors 2164126Skato * may be used to endorse or promote products derived from this software 2264126Skato * without specific prior written permission. 2364126Skato * 2464126Skato * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2564126Skato * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2664126Skato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2764126Skato * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2864126Skato * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2964126Skato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3064126Skato * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3164126Skato * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3264126Skato * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3364126Skato * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3464126Skato * SUCH DAMAGE. 3564126Skato */ 3664126Skato 3764126Skato#ifndef lint 3864126Skatostatic char const copyright[] = 3990518Snyan"@(#) Copyright (c) 1989, 1993\n\ 4064126Skato The Regents of the University of California. All rights reserved.\n"; 4164126Skato#endif /* not lint */ 4264126Skato 4364126Skato#ifndef lint 4464126Skato#if 0 4564126Skatostatic char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95"; 4664126Skato#endif 4764126Skatostatic const char rcsid[] = 4864126Skato "$FreeBSD: head/bin/cat/cat.c 98216 2002-06-14 09:54:48Z jmallett $"; 4964126Skato#endif /* not lint */ 5064126Skato 5164126Skato#include <sys/param.h> 5264126Skato#include <sys/stat.h> 5364126Skato#ifndef NO_UDOM_SUPPORT 5471898Sru#include <sys/socket.h> 5564126Skato#include <sys/un.h> 56131500Sru#include <errno.h> 57131500Sru#endif 5870403Sru 5964126Skato#include <ctype.h> 6064126Skato#include <err.h> 6199968Scharnier#include <fcntl.h> 6264126Skato#include <locale.h> 6370403Sru#include <stdio.h> 6464126Skato#include <stdlib.h> 6564126Skato#include <string.h> 6664126Skato#include <unistd.h> 6770403Sru#include <stddef.h> 6864126Skato 6964126Skatoint bflag, eflag, nflag, sflag, tflag, vflag; 7064126Skatoint rval; 71131500Sruconst char *filename; 72131500Sru 7364126Skatostatic void usage(void); 7464126Skatostatic void scanfiles(char **argv, int cooked); 75131500Srustatic void cook_cat(FILE *); 76131500Srustatic void raw_cat(int); 7764126Skato 78131500Sru#ifndef NO_UDOM_SUPPORT 79131500Srustatic int udom_open(const char *path, int flags); 8064126Skato#endif 8164126Skato 8279755Sddint 8364126Skatomain(int argc, char *argv[]) 8464126Skato{ 8564126Skato int ch; 8664126Skato 8764126Skato setlocale(LC_CTYPE, ""); 8864126Skato 8964126Skato while ((ch = getopt(argc, argv, "benstuv")) != -1) 9064126Skato switch (ch) { 9164126Skato case 'b': 9264126Skato bflag = nflag = 1; /* -b implies -n */ 93131500Sru break; 94131500Sru case 'e': 9564126Skato eflag = vflag = 1; /* -e implies -v */ 9664126Skato break; 97140442Sru case 'n': 98140442Sru nflag = 1; 9964126Skato break; 10064126Skato case 's': 10168756Sben sflag = 1; 10264126Skato break; 10364126Skato case 't': 10464126Skato tflag = vflag = 1; /* -t implies -v */ 105 break; 106 case 'u': 107 setbuf(stdout, NULL); 108 break; 109 case 'v': 110 vflag = 1; 111 break; 112 default: 113 usage(); 114 } 115 argv += optind; 116 117 if (bflag || eflag || nflag || sflag || tflag || vflag) 118 scanfiles(argv, 1); 119 else 120 scanfiles(argv, 0); 121 if (fclose(stdout)) 122 err(1, "stdout"); 123 exit(rval); 124} 125 126static void 127usage(void) 128{ 129 fprintf(stderr, "usage: cat [-benstuv] [file ...]\n"); 130 exit(1); 131} 132 133void 134scanfiles(char **argv, int cooked) 135{ 136 int i = 0; 137 char *path; 138 FILE *fp; 139 140 while ((path = argv[i]) != NULL || i == 0) { 141 int fd; 142 143 if (path == NULL || strcmp(path, "-") == 0) { 144 filename = "stdin"; 145 fd = STDIN_FILENO; 146 } else { 147 filename = path; 148 fd = open(path, O_RDONLY); 149#ifndef NO_UDOM_SUPPORT 150 if (fd < 0 && errno == EOPNOTSUPP) 151 fd = udom_open(path, O_RDONLY); 152#endif 153 } 154 if (fd < 0) { 155 warn("%s", path); 156 rval = 1; 157 } else if (cooked) { 158 if (fd == STDIN_FILENO) 159 cook_cat(stdin); 160 else { 161 fp = fdopen(fd, "r"); 162 cook_cat(fp); 163 fclose(fp); 164 } 165 } else { 166 raw_cat(fd); 167 if (fd != STDIN_FILENO) 168 close(fd); 169 } 170 if (path == NULL) 171 break; 172 ++i; 173 } 174} 175 176static void 177cook_cat(FILE *fp) 178{ 179 int ch, gobble, line, prev; 180 181 /* Reset EOF condition on stdin. */ 182 if (fp == stdin && feof(stdin)) 183 clearerr(stdin); 184 185 line = gobble = 0; 186 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) { 187 if (prev == '\n') { 188 if (sflag) { 189 if (ch == '\n') { 190 if (gobble) 191 continue; 192 gobble = 1; 193 } else 194 gobble = 0; 195 } 196 if (nflag && (!bflag || ch != '\n')) { 197 (void)fprintf(stdout, "%6d\t", ++line); 198 if (ferror(stdout)) 199 break; 200 } 201 } 202 if (ch == '\n') { 203 if (eflag && putchar('$') == EOF) 204 break; 205 } else if (ch == '\t') { 206 if (tflag) { 207 if (putchar('^') == EOF || putchar('I') == EOF) 208 break; 209 continue; 210 } 211 } else if (vflag) { 212 if (!isascii(ch) && !isprint(ch)) { 213 if (putchar('M') == EOF || putchar('-') == EOF) 214 break; 215 ch = toascii(ch); 216 } 217 if (iscntrl(ch)) { 218 if (putchar('^') == EOF || 219 putchar(ch == '\177' ? '?' : 220 ch | 0100) == EOF) 221 break; 222 continue; 223 } 224 } 225 if (putchar(ch) == EOF) 226 break; 227 } 228 if (ferror(fp)) { 229 warn("%s", filename); 230 rval = 1; 231 clearerr(fp); 232 } 233 if (ferror(stdout)) 234 err(1, "stdout"); 235} 236 237static void 238raw_cat(int rfd) 239{ 240 int off, wfd; 241 ssize_t nr, nw; 242 static size_t bsize; 243 static char *buf = NULL; 244 struct stat sbuf; 245 246 wfd = fileno(stdout); 247 if (buf == NULL) { 248 if (fstat(wfd, &sbuf)) 249 err(1, "%s", filename); 250 bsize = MAX(sbuf.st_blksize, 1024); 251 if ((buf = malloc(bsize)) == NULL) 252 err(1, "buffer"); 253 } 254 while ((nr = read(rfd, buf, bsize)) > 0) 255 for (off = 0; nr; nr -= nw, off += nw) 256 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) 257 err(1, "stdout"); 258 if (nr < 0) { 259 warn("%s", filename); 260 rval = 1; 261 } 262} 263 264#ifndef NO_UDOM_SUPPORT 265 266static int 267udom_open(const char *path, int flags) 268{ 269 struct sockaddr_un sou; 270 int fd; 271 unsigned int len; 272 273 bzero(&sou, sizeof(sou)); 274 275 /* 276 * Construct the unix domain socket address and attempt to connect 277 */ 278 fd = socket(AF_UNIX, SOCK_STREAM, 0); 279 if (fd >= 0) { 280 sou.sun_family = AF_UNIX; 281 snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path); 282 len = strlen(sou.sun_path); 283 len = offsetof(struct sockaddr_un, sun_path[len+1]); 284 285 if (connect(fd, (void *)&sou, len) < 0) { 286 close(fd); 287 fd = -1; 288 } 289 } 290 291 /* 292 * handle the open flags by shutting down appropriate directions 293 */ 294 if (fd >= 0) { 295 switch(flags & O_ACCMODE) { 296 case O_RDONLY: 297 if (shutdown(fd, SHUT_WR) == -1) 298 perror("cat"); 299 break; 300 case O_WRONLY: 301 if (shutdown(fd, SHUT_RD) == -1) 302 perror("cat"); 303 break; 304 default: 305 break; 306 } 307 } 308 return(fd); 309} 310 311#endif 312