| 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 | * 4. Neither the name of the University nor the names of its contributors | 
|---|
| 17 | *    may be used to endorse or promote products derived from this software | 
|---|
| 18 | *    without specific prior written permission. | 
|---|
| 19 | * | 
|---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 23 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 30 | * SUCH DAMAGE. | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | #if 0 | 
|---|
| 34 | #ifndef lint | 
|---|
| 35 | static char const copyright[] = | 
|---|
| 36 | "@(#) Copyright (c) 1989, 1993\n\ | 
|---|
| 37 | The Regents of the University of California.  All rights reserved.\n"; | 
|---|
| 38 | #endif /* not lint */ | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef lint | 
|---|
| 42 | #if 0 | 
|---|
| 43 | static char sccsid[] = "@(#)cat.c       8.2 (Berkeley) 4/27/95"; | 
|---|
| 44 | #endif | 
|---|
| 45 | #endif /* not lint */ | 
|---|
| 46 | #if 0 | 
|---|
| 47 | #include <sys/cdefs.h> | 
|---|
| 48 | __FBSDID("$FreeBSD: src/bin/cat/cat.c,v 1.32 2005/01/10 08:39:20 imp Exp $"); | 
|---|
| 49 | #else | 
|---|
| 50 | #define NO_UDOM_SUPPORT /* kmk */ | 
|---|
| 51 | #endif | 
|---|
| 52 |  | 
|---|
| 53 | #ifndef _MSC_VER | 
|---|
| 54 | # include <sys/param.h> | 
|---|
| 55 | #endif | 
|---|
| 56 | #include <sys/stat.h> | 
|---|
| 57 | #ifndef NO_UDOM_SUPPORT | 
|---|
| 58 | # include <sys/socket.h> | 
|---|
| 59 | # include <sys/un.h> | 
|---|
| 60 | # include <errno.h> | 
|---|
| 61 | #endif | 
|---|
| 62 |  | 
|---|
| 63 | #include <ctype.h> | 
|---|
| 64 | #include "err.h" | 
|---|
| 65 | #include <fcntl.h> | 
|---|
| 66 | #include <locale.h> | 
|---|
| 67 | #include <stdio.h> | 
|---|
| 68 | #include <stdlib.h> | 
|---|
| 69 | #include <string.h> | 
|---|
| 70 | #include <unistd.h> | 
|---|
| 71 | #include <stddef.h> | 
|---|
| 72 | #include "getopt.h" | 
|---|
| 73 | #ifdef __sun__ | 
|---|
| 74 | # include "solfakes.h" | 
|---|
| 75 | #endif | 
|---|
| 76 | #ifdef _MSC_VER | 
|---|
| 77 | # include "mscfakes.h" | 
|---|
| 78 | #endif | 
|---|
| 79 | #include "kmkbuiltin.h" | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | int bflag, eflag, nflag, sflag, tflag, vflag; | 
|---|
| 83 | /*int rval;*/ | 
|---|
| 84 | const char *filename; | 
|---|
| 85 |  | 
|---|
| 86 | static struct option long_options[] = | 
|---|
| 87 | { | 
|---|
| 88 | { "help",                                           no_argument, 0, 261 }, | 
|---|
| 89 | { "version",                                        no_argument, 0, 262 }, | 
|---|
| 90 | { 0, 0,     0, 0 }, | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | static int usage(FILE *); | 
|---|
| 95 | static int scanfiles(char *argv[], int cooked); | 
|---|
| 96 | static int cook_cat(FILE *); | 
|---|
| 97 | static int raw_cat(int); | 
|---|
| 98 |  | 
|---|
| 99 | #ifndef NO_UDOM_SUPPORT | 
|---|
| 100 | static int udom_open(const char *path, int flags); | 
|---|
| 101 | #endif | 
|---|
| 102 |  | 
|---|
| 103 | int | 
|---|
| 104 | kmk_builtin_cat(int argc, char *argv[], char **envp) | 
|---|
| 105 | { | 
|---|
| 106 | int ch, rc; | 
|---|
| 107 |  | 
|---|
| 108 | /* kmk: reinitialize globals */ | 
|---|
| 109 | bflag = eflag = nflag = sflag = tflag = vflag = 0; | 
|---|
| 110 | filename = NULL; | 
|---|
| 111 |  | 
|---|
| 112 | /* kmk: reset getopt and set progname */ | 
|---|
| 113 | g_progname = argv[0]; | 
|---|
| 114 | opterr = 1; | 
|---|
| 115 | optarg = NULL; | 
|---|
| 116 | optopt = 0; | 
|---|
| 117 | optind = 0; /* init */ | 
|---|
| 118 |  | 
|---|
| 119 | #ifdef kmk_builtin_cat /* kmk did this already. */ | 
|---|
| 120 | setlocale(LC_CTYPE, ""); | 
|---|
| 121 | #endif | 
|---|
| 122 |  | 
|---|
| 123 | while ((ch = getopt_long(argc, argv, "benstuv", long_options, NULL)) != -1) | 
|---|
| 124 | switch (ch) { | 
|---|
| 125 | case 'b': | 
|---|
| 126 | bflag = nflag = 1;      /* -b implies -n */ | 
|---|
| 127 | break; | 
|---|
| 128 | case 'e': | 
|---|
| 129 | eflag = vflag = 1;      /* -e implies -v */ | 
|---|
| 130 | break; | 
|---|
| 131 | case 'n': | 
|---|
| 132 | nflag = 1; | 
|---|
| 133 | break; | 
|---|
| 134 | case 's': | 
|---|
| 135 | sflag = 1; | 
|---|
| 136 | break; | 
|---|
| 137 | case 't': | 
|---|
| 138 | tflag = vflag = 1;      /* -t implies -v */ | 
|---|
| 139 | break; | 
|---|
| 140 | case 'u': | 
|---|
| 141 | setbuf(stdout, NULL); | 
|---|
| 142 | break; | 
|---|
| 143 | case 'v': | 
|---|
| 144 | vflag = 1; | 
|---|
| 145 | break; | 
|---|
| 146 | case 261: | 
|---|
| 147 | usage(stdout); | 
|---|
| 148 | return 0; | 
|---|
| 149 | case 262: | 
|---|
| 150 | return kbuild_version(argv[0]); | 
|---|
| 151 | default: | 
|---|
| 152 | return usage(stderr); | 
|---|
| 153 | } | 
|---|
| 154 | argv += optind; | 
|---|
| 155 |  | 
|---|
| 156 | if (bflag || eflag || nflag || sflag || tflag || vflag) | 
|---|
| 157 | rc = scanfiles(argv, 1); | 
|---|
| 158 | else | 
|---|
| 159 | rc = scanfiles(argv, 0); | 
|---|
| 160 | #ifdef kmk_builtin_cat /* only in the external program. */ | 
|---|
| 161 | if (fclose(stdout)) | 
|---|
| 162 | return err(1, "stdout"); | 
|---|
| 163 | #endif | 
|---|
| 164 | return rc; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | static int | 
|---|
| 168 | usage(FILE *fp) | 
|---|
| 169 | { | 
|---|
| 170 | fprintf(fp, "usage: %s [-benstuv] [file ...]\n" | 
|---|
| 171 | "   or: %s --help\n" | 
|---|
| 172 | "   or: %s --version\n", | 
|---|
| 173 | g_progname, g_progname, g_progname); | 
|---|
| 174 | return 1; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | static int | 
|---|
| 178 | scanfiles(char *argv[], int cooked) | 
|---|
| 179 | { | 
|---|
| 180 | int i = 0; | 
|---|
| 181 | char *path; | 
|---|
| 182 | FILE *fp; | 
|---|
| 183 | int rc2 = 0; | 
|---|
| 184 | int rc = 0; | 
|---|
| 185 |  | 
|---|
| 186 | while ((path = argv[i]) != NULL || i == 0) { | 
|---|
| 187 | int fd; | 
|---|
| 188 |  | 
|---|
| 189 | if (path == NULL || strcmp(path, "-") == 0) { | 
|---|
| 190 | filename = "stdin"; | 
|---|
| 191 | fd = STDIN_FILENO; | 
|---|
| 192 | } else { | 
|---|
| 193 | filename = path; | 
|---|
| 194 | fd = open(path, O_RDONLY); | 
|---|
| 195 | #ifndef NO_UDOM_SUPPORT | 
|---|
| 196 | if (fd < 0 && errno == EOPNOTSUPP) | 
|---|
| 197 | fd = udom_open(path, O_RDONLY); | 
|---|
| 198 | #endif | 
|---|
| 199 | } | 
|---|
| 200 | if (fd < 0) { | 
|---|
| 201 | warn("%s", path); | 
|---|
| 202 | rc2 = 1; /* non fatal */ | 
|---|
| 203 | } else if (cooked) { | 
|---|
| 204 | if (fd == STDIN_FILENO) | 
|---|
| 205 | rc = cook_cat(stdin); | 
|---|
| 206 | else { | 
|---|
| 207 | fp = fdopen(fd, "r"); | 
|---|
| 208 | rc = cook_cat(fp); | 
|---|
| 209 | fclose(fp); | 
|---|
| 210 | } | 
|---|
| 211 | } else { | 
|---|
| 212 | rc = raw_cat(fd); | 
|---|
| 213 | if (fd != STDIN_FILENO) | 
|---|
| 214 | close(fd); | 
|---|
| 215 | } | 
|---|
| 216 | if (rc || path == NULL) | 
|---|
| 217 | break; | 
|---|
| 218 | ++i; | 
|---|
| 219 | } | 
|---|
| 220 | return !rc ? rc2 : rc; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | static int | 
|---|
| 224 | cook_cat(FILE *fp) | 
|---|
| 225 | { | 
|---|
| 226 | int ch, gobble, line, prev; | 
|---|
| 227 | int rc = 0; | 
|---|
| 228 |  | 
|---|
| 229 | /* Reset EOF condition on stdin. */ | 
|---|
| 230 | if (fp == stdin && feof(stdin)) | 
|---|
| 231 | clearerr(stdin); | 
|---|
| 232 |  | 
|---|
| 233 | line = gobble = 0; | 
|---|
| 234 | for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) { | 
|---|
| 235 | if (prev == '\n') { | 
|---|
| 236 | if (sflag) { | 
|---|
| 237 | if (ch == '\n') { | 
|---|
| 238 | if (gobble) | 
|---|
| 239 | continue; | 
|---|
| 240 | gobble = 1; | 
|---|
| 241 | } else | 
|---|
| 242 | gobble = 0; | 
|---|
| 243 | } | 
|---|
| 244 | if (nflag && (!bflag || ch != '\n')) { | 
|---|
| 245 | (void)fprintf(stdout, "%6d\t", ++line); | 
|---|
| 246 | if (ferror(stdout)) | 
|---|
| 247 | break; | 
|---|
| 248 | } | 
|---|
| 249 | } | 
|---|
| 250 | if (ch == '\n') { | 
|---|
| 251 | if (eflag && putchar('$') == EOF) | 
|---|
| 252 | break; | 
|---|
| 253 | } else if (ch == '\t') { | 
|---|
| 254 | if (tflag) { | 
|---|
| 255 | if (putchar('^') == EOF || putchar('I') == EOF) | 
|---|
| 256 | break; | 
|---|
| 257 | continue; | 
|---|
| 258 | } | 
|---|
| 259 | } else if (vflag) { | 
|---|
| 260 | if (!isascii(ch) && !isprint(ch)) { | 
|---|
| 261 | if (putchar('M') == EOF || putchar('-') == EOF) | 
|---|
| 262 | break; | 
|---|
| 263 | ch = toascii(ch); | 
|---|
| 264 | } | 
|---|
| 265 | if (iscntrl(ch)) { | 
|---|
| 266 | if (putchar('^') == EOF || | 
|---|
| 267 | putchar(ch == '\177' ? '?' : | 
|---|
| 268 | ch | 0100) == EOF) | 
|---|
| 269 | break; | 
|---|
| 270 | continue; | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 | if (putchar(ch) == EOF) | 
|---|
| 274 | break; | 
|---|
| 275 | } | 
|---|
| 276 | if (ferror(fp)) { | 
|---|
| 277 | warn("%s", filename); | 
|---|
| 278 | rc = 1; | 
|---|
| 279 | clearerr(fp); | 
|---|
| 280 | } | 
|---|
| 281 | if (ferror(stdout)) | 
|---|
| 282 | return err(1, "stdout"); | 
|---|
| 283 | return rc; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | static int | 
|---|
| 287 | raw_cat(int rfd) | 
|---|
| 288 | { | 
|---|
| 289 | int off, wfd; | 
|---|
| 290 | ssize_t nr, nw; | 
|---|
| 291 | static size_t bsize; | 
|---|
| 292 | static char *buf = NULL; | 
|---|
| 293 | struct stat sbuf; | 
|---|
| 294 |  | 
|---|
| 295 | wfd = fileno(stdout); | 
|---|
| 296 | if (buf == NULL) { | 
|---|
| 297 | if (fstat(wfd, &sbuf)) | 
|---|
| 298 | return err(1, "%s", filename); | 
|---|
| 299 | #ifdef _MSC_VER | 
|---|
| 300 | bsize = 1024; | 
|---|
| 301 | #else | 
|---|
| 302 | bsize = MAX(sbuf.st_blksize, 1024); | 
|---|
| 303 | #endif | 
|---|
| 304 | if ((buf = malloc(bsize)) == NULL) | 
|---|
| 305 | return err(1, "buffer"); | 
|---|
| 306 | } | 
|---|
| 307 | while ((nr = read(rfd, buf, bsize)) > 0) | 
|---|
| 308 | for (off = 0; nr; nr -= nw, off += nw) | 
|---|
| 309 | if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) | 
|---|
| 310 | return err(1, "stdout"); | 
|---|
| 311 | if (nr < 0) { | 
|---|
| 312 | warn("%s", filename); | 
|---|
| 313 | return 1; | 
|---|
| 314 | } | 
|---|
| 315 | return 0; | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | #ifndef NO_UDOM_SUPPORT | 
|---|
| 319 |  | 
|---|
| 320 | static int | 
|---|
| 321 | udom_open(const char *path, int flags) | 
|---|
| 322 | { | 
|---|
| 323 | struct sockaddr_un sou; | 
|---|
| 324 | int fd; | 
|---|
| 325 | unsigned int len; | 
|---|
| 326 |  | 
|---|
| 327 | bzero(&sou, sizeof(sou)); | 
|---|
| 328 |  | 
|---|
| 329 | /* | 
|---|
| 330 | * Construct the unix domain socket address and attempt to connect | 
|---|
| 331 | */ | 
|---|
| 332 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | 
|---|
| 333 | if (fd >= 0) { | 
|---|
| 334 | sou.sun_family = AF_UNIX; | 
|---|
| 335 | if ((len = strlcpy(sou.sun_path, path, | 
|---|
| 336 | sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) { | 
|---|
| 337 | errno = ENAMETOOLONG; | 
|---|
| 338 | return (-1); | 
|---|
| 339 | } | 
|---|
| 340 | len = offsetof(struct sockaddr_un, sun_path[len+1]); | 
|---|
| 341 |  | 
|---|
| 342 | if (connect(fd, (void *)&sou, len) < 0) { | 
|---|
| 343 | close(fd); | 
|---|
| 344 | fd = -1; | 
|---|
| 345 | } | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | /* | 
|---|
| 349 | * handle the open flags by shutting down appropriate directions | 
|---|
| 350 | */ | 
|---|
| 351 | if (fd >= 0) { | 
|---|
| 352 | switch(flags & O_ACCMODE) { | 
|---|
| 353 | case O_RDONLY: | 
|---|
| 354 | if (shutdown(fd, SHUT_WR) == -1) | 
|---|
| 355 | warn(NULL); | 
|---|
| 356 | break; | 
|---|
| 357 | case O_WRONLY: | 
|---|
| 358 | if (shutdown(fd, SHUT_RD) == -1) | 
|---|
| 359 | warn(NULL); | 
|---|
| 360 | break; | 
|---|
| 361 | default: | 
|---|
| 362 | break; | 
|---|
| 363 | } | 
|---|
| 364 | } | 
|---|
| 365 | return(fd); | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | #endif | 
|---|