| 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 | #include "config.h"
|
|---|
| 54 | #ifndef _MSC_VER
|
|---|
| 55 | # include <sys/param.h>
|
|---|
| 56 | #endif
|
|---|
| 57 | #include <sys/stat.h>
|
|---|
| 58 | #ifndef NO_UDOM_SUPPORT
|
|---|
| 59 | # include <sys/socket.h>
|
|---|
| 60 | # include <sys/un.h>
|
|---|
| 61 | # include <errno.h>
|
|---|
| 62 | #endif
|
|---|
| 63 |
|
|---|
| 64 | #include <ctype.h>
|
|---|
| 65 | #include "err.h"
|
|---|
| 66 | #include <fcntl.h>
|
|---|
| 67 | #include <locale.h>
|
|---|
| 68 | #include <stdio.h>
|
|---|
| 69 | #include <stdlib.h>
|
|---|
| 70 | #include <string.h>
|
|---|
| 71 | #include <unistd.h>
|
|---|
| 72 | #include <stddef.h>
|
|---|
| 73 | #include "getopt.h"
|
|---|
| 74 | #ifdef __sun__
|
|---|
| 75 | # include "solfakes.h"
|
|---|
| 76 | #endif
|
|---|
| 77 | #ifdef _MSC_VER
|
|---|
| 78 | # include "mscfakes.h"
|
|---|
| 79 | #endif
|
|---|
| 80 | #include "kmkbuiltin.h"
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | int bflag, eflag, nflag, sflag, tflag, vflag;
|
|---|
| 84 | /*int rval;*/
|
|---|
| 85 | const char *filename;
|
|---|
| 86 |
|
|---|
| 87 | static struct option long_options[] =
|
|---|
| 88 | {
|
|---|
| 89 | { "help", no_argument, 0, 261 },
|
|---|
| 90 | { "version", no_argument, 0, 262 },
|
|---|
| 91 | { 0, 0, 0, 0 },
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | static int usage(FILE *);
|
|---|
| 96 | static int scanfiles(char *argv[], int cooked);
|
|---|
| 97 | static int cook_cat(FILE *);
|
|---|
| 98 | static int raw_cat(int);
|
|---|
| 99 |
|
|---|
| 100 | #ifndef NO_UDOM_SUPPORT
|
|---|
| 101 | static int udom_open(const char *path, int flags);
|
|---|
| 102 | #endif
|
|---|
| 103 |
|
|---|
| 104 | int
|
|---|
| 105 | kmk_builtin_cat(int argc, char *argv[], char **envp)
|
|---|
| 106 | {
|
|---|
| 107 | int ch, rc;
|
|---|
| 108 |
|
|---|
| 109 | /* kmk: reinitialize globals */
|
|---|
| 110 | bflag = eflag = nflag = sflag = tflag = vflag = 0;
|
|---|
| 111 | filename = NULL;
|
|---|
| 112 |
|
|---|
| 113 | /* kmk: reset getopt and set progname */
|
|---|
| 114 | g_progname = argv[0];
|
|---|
| 115 | opterr = 1;
|
|---|
| 116 | optarg = NULL;
|
|---|
| 117 | optopt = 0;
|
|---|
| 118 | optind = 0; /* init */
|
|---|
| 119 |
|
|---|
| 120 | #ifdef kmk_builtin_cat /* kmk did this already. */
|
|---|
| 121 | setlocale(LC_CTYPE, "");
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | while ((ch = getopt_long(argc, argv, "benstuv", long_options, NULL)) != -1)
|
|---|
| 125 | switch (ch) {
|
|---|
| 126 | case 'b':
|
|---|
| 127 | bflag = nflag = 1; /* -b implies -n */
|
|---|
| 128 | break;
|
|---|
| 129 | case 'e':
|
|---|
| 130 | eflag = vflag = 1; /* -e implies -v */
|
|---|
| 131 | break;
|
|---|
| 132 | case 'n':
|
|---|
| 133 | nflag = 1;
|
|---|
| 134 | break;
|
|---|
| 135 | case 's':
|
|---|
| 136 | sflag = 1;
|
|---|
| 137 | break;
|
|---|
| 138 | case 't':
|
|---|
| 139 | tflag = vflag = 1; /* -t implies -v */
|
|---|
| 140 | break;
|
|---|
| 141 | case 'u':
|
|---|
| 142 | setbuf(stdout, NULL);
|
|---|
| 143 | break;
|
|---|
| 144 | case 'v':
|
|---|
| 145 | vflag = 1;
|
|---|
| 146 | break;
|
|---|
| 147 | case 261:
|
|---|
| 148 | usage(stdout);
|
|---|
| 149 | return 0;
|
|---|
| 150 | case 262:
|
|---|
| 151 | return kbuild_version(argv[0]);
|
|---|
| 152 | default:
|
|---|
| 153 | return usage(stderr);
|
|---|
| 154 | }
|
|---|
| 155 | argv += optind;
|
|---|
| 156 |
|
|---|
| 157 | if (bflag || eflag || nflag || sflag || tflag || vflag)
|
|---|
| 158 | rc = scanfiles(argv, 1);
|
|---|
| 159 | else
|
|---|
| 160 | rc = scanfiles(argv, 0);
|
|---|
| 161 | #ifdef kmk_builtin_cat /* only in the external program. */
|
|---|
| 162 | if (fclose(stdout))
|
|---|
| 163 | return err(1, "stdout");
|
|---|
| 164 | #endif
|
|---|
| 165 | return rc;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | static int
|
|---|
| 169 | usage(FILE *fp)
|
|---|
| 170 | {
|
|---|
| 171 | fprintf(fp, "usage: %s [-benstuv] [file ...]\n"
|
|---|
| 172 | " or: %s --help\n"
|
|---|
| 173 | " or: %s --version\n",
|
|---|
| 174 | g_progname, g_progname, g_progname);
|
|---|
| 175 | return 1;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | static int
|
|---|
| 179 | scanfiles(char *argv[], int cooked)
|
|---|
| 180 | {
|
|---|
| 181 | int i = 0;
|
|---|
| 182 | char *path;
|
|---|
| 183 | FILE *fp;
|
|---|
| 184 | int rc2 = 0;
|
|---|
| 185 | int rc = 0;
|
|---|
| 186 |
|
|---|
| 187 | while ((path = argv[i]) != NULL || i == 0) {
|
|---|
| 188 | int fd;
|
|---|
| 189 |
|
|---|
| 190 | if (path == NULL || strcmp(path, "-") == 0) {
|
|---|
| 191 | filename = "stdin";
|
|---|
| 192 | fd = STDIN_FILENO;
|
|---|
| 193 | } else {
|
|---|
| 194 | filename = path;
|
|---|
| 195 | fd = open(path, O_RDONLY);
|
|---|
| 196 | #ifndef NO_UDOM_SUPPORT
|
|---|
| 197 | if (fd < 0 && errno == EOPNOTSUPP)
|
|---|
| 198 | fd = udom_open(path, O_RDONLY);
|
|---|
| 199 | #endif
|
|---|
| 200 | }
|
|---|
| 201 | if (fd < 0) {
|
|---|
| 202 | warn("%s", path);
|
|---|
| 203 | rc2 = 1; /* non fatal */
|
|---|
| 204 | } else if (cooked) {
|
|---|
| 205 | if (fd == STDIN_FILENO)
|
|---|
| 206 | rc = cook_cat(stdin);
|
|---|
| 207 | else {
|
|---|
| 208 | fp = fdopen(fd, "r");
|
|---|
| 209 | rc = cook_cat(fp);
|
|---|
| 210 | fclose(fp);
|
|---|
| 211 | }
|
|---|
| 212 | } else {
|
|---|
| 213 | rc = raw_cat(fd);
|
|---|
| 214 | if (fd != STDIN_FILENO)
|
|---|
| 215 | close(fd);
|
|---|
| 216 | }
|
|---|
| 217 | if (rc || path == NULL)
|
|---|
| 218 | break;
|
|---|
| 219 | ++i;
|
|---|
| 220 | }
|
|---|
| 221 | return !rc ? rc2 : rc;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | static int
|
|---|
| 225 | cook_cat(FILE *fp)
|
|---|
| 226 | {
|
|---|
| 227 | int ch, gobble, line, prev;
|
|---|
| 228 | int rc = 0;
|
|---|
| 229 |
|
|---|
| 230 | /* Reset EOF condition on stdin. */
|
|---|
| 231 | if (fp == stdin && feof(stdin))
|
|---|
| 232 | clearerr(stdin);
|
|---|
| 233 |
|
|---|
| 234 | line = gobble = 0;
|
|---|
| 235 | for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
|
|---|
| 236 | if (prev == '\n') {
|
|---|
| 237 | if (sflag) {
|
|---|
| 238 | if (ch == '\n') {
|
|---|
| 239 | if (gobble)
|
|---|
| 240 | continue;
|
|---|
| 241 | gobble = 1;
|
|---|
| 242 | } else
|
|---|
| 243 | gobble = 0;
|
|---|
| 244 | }
|
|---|
| 245 | if (nflag && (!bflag || ch != '\n')) {
|
|---|
| 246 | (void)fprintf(stdout, "%6d\t", ++line);
|
|---|
| 247 | if (ferror(stdout))
|
|---|
| 248 | break;
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | if (ch == '\n') {
|
|---|
| 252 | if (eflag && putchar('$') == EOF)
|
|---|
| 253 | break;
|
|---|
| 254 | } else if (ch == '\t') {
|
|---|
| 255 | if (tflag) {
|
|---|
| 256 | if (putchar('^') == EOF || putchar('I') == EOF)
|
|---|
| 257 | break;
|
|---|
| 258 | continue;
|
|---|
| 259 | }
|
|---|
| 260 | } else if (vflag) {
|
|---|
| 261 | if (!isascii(ch) && !isprint(ch)) {
|
|---|
| 262 | if (putchar('M') == EOF || putchar('-') == EOF)
|
|---|
| 263 | break;
|
|---|
| 264 | ch = toascii(ch);
|
|---|
| 265 | }
|
|---|
| 266 | if (iscntrl(ch)) {
|
|---|
| 267 | if (putchar('^') == EOF ||
|
|---|
| 268 | putchar(ch == '\177' ? '?' :
|
|---|
| 269 | ch | 0100) == EOF)
|
|---|
| 270 | break;
|
|---|
| 271 | continue;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 | if (putchar(ch) == EOF)
|
|---|
| 275 | break;
|
|---|
| 276 | }
|
|---|
| 277 | if (ferror(fp)) {
|
|---|
| 278 | warn("%s", filename);
|
|---|
| 279 | rc = 1;
|
|---|
| 280 | clearerr(fp);
|
|---|
| 281 | }
|
|---|
| 282 | if (ferror(stdout))
|
|---|
| 283 | return err(1, "stdout");
|
|---|
| 284 | return rc;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | static int
|
|---|
| 288 | raw_cat(int rfd)
|
|---|
| 289 | {
|
|---|
| 290 | int off, wfd;
|
|---|
| 291 | ssize_t nr, nw;
|
|---|
| 292 | static size_t bsize;
|
|---|
| 293 | static char *buf = NULL;
|
|---|
| 294 | struct stat sbuf;
|
|---|
| 295 |
|
|---|
| 296 | wfd = fileno(stdout);
|
|---|
| 297 | if (buf == NULL) {
|
|---|
| 298 | if (fstat(wfd, &sbuf))
|
|---|
| 299 | return err(1, "%s", filename);
|
|---|
| 300 | #ifdef _MSC_VER
|
|---|
| 301 | bsize = 1024;
|
|---|
| 302 | #else
|
|---|
| 303 | bsize = MAX(sbuf.st_blksize, 1024);
|
|---|
| 304 | #endif
|
|---|
| 305 | if ((buf = malloc(bsize)) == NULL)
|
|---|
| 306 | return err(1, "buffer");
|
|---|
| 307 | }
|
|---|
| 308 | while ((nr = read(rfd, buf, bsize)) > 0)
|
|---|
| 309 | for (off = 0; nr; nr -= nw, off += nw)
|
|---|
| 310 | if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
|
|---|
| 311 | return err(1, "stdout");
|
|---|
| 312 | if (nr < 0) {
|
|---|
| 313 | warn("%s", filename);
|
|---|
| 314 | return 1;
|
|---|
| 315 | }
|
|---|
| 316 | return 0;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | #ifndef NO_UDOM_SUPPORT
|
|---|
| 320 |
|
|---|
| 321 | static int
|
|---|
| 322 | udom_open(const char *path, int flags)
|
|---|
| 323 | {
|
|---|
| 324 | struct sockaddr_un sou;
|
|---|
| 325 | int fd;
|
|---|
| 326 | unsigned int len;
|
|---|
| 327 |
|
|---|
| 328 | bzero(&sou, sizeof(sou));
|
|---|
| 329 |
|
|---|
| 330 | /*
|
|---|
| 331 | * Construct the unix domain socket address and attempt to connect
|
|---|
| 332 | */
|
|---|
| 333 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|---|
| 334 | if (fd >= 0) {
|
|---|
| 335 | sou.sun_family = AF_UNIX;
|
|---|
| 336 | if ((len = strlcpy(sou.sun_path, path,
|
|---|
| 337 | sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
|
|---|
| 338 | errno = ENAMETOOLONG;
|
|---|
| 339 | return (-1);
|
|---|
| 340 | }
|
|---|
| 341 | len = offsetof(struct sockaddr_un, sun_path[len+1]);
|
|---|
| 342 |
|
|---|
| 343 | if (connect(fd, (void *)&sou, len) < 0) {
|
|---|
| 344 | close(fd);
|
|---|
| 345 | fd = -1;
|
|---|
| 346 | }
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /*
|
|---|
| 350 | * handle the open flags by shutting down appropriate directions
|
|---|
| 351 | */
|
|---|
| 352 | if (fd >= 0) {
|
|---|
| 353 | switch(flags & O_ACCMODE) {
|
|---|
| 354 | case O_RDONLY:
|
|---|
| 355 | if (shutdown(fd, SHUT_WR) == -1)
|
|---|
| 356 | warn(NULL);
|
|---|
| 357 | break;
|
|---|
| 358 | case O_WRONLY:
|
|---|
| 359 | if (shutdown(fd, SHUT_RD) == -1)
|
|---|
| 360 | warn(NULL);
|
|---|
| 361 | break;
|
|---|
| 362 | default:
|
|---|
| 363 | break;
|
|---|
| 364 | }
|
|---|
| 365 | }
|
|---|
| 366 | return(fd);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | #endif
|
|---|