/* $shbox: cat.c,v 20220721 2022/07/21 07:42:69 leah Exp $ */ /* $OpenBSD: cat.c,v 1.34 2022/02/09 01:58:57 cheloha Exp $ */ /* $NetBSD: cat.c,v 1.11 1995/09/07 06:12:54 jtc Exp $ */ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kevin Fall. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #ifdef linux #include #include #include #endif #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) int status = EXIT_SUCCESS; void cat_input(const char *path); int cat_rw_fail(int rfd); #ifdef linux int cat_splice_fail(int rfd); long block_size; #endif int main(int argc, char *argv[]) { int ch; setvbuf(stdout, NULL, _IONBF, 0); while ((ch = getopt(argc, argv, "u")) != -1) { if (ch != 'u') { fprintf(stderr, "usage: cat [-u] [file ...]\n"); return EXIT_FAILURE; } } argc -= optind; argv += optind; #ifdef linux block_size = (sysconf(_SC_PAGESIZE) << 4) - 1; #endif if (argc == 0) cat_input(NULL); else while (*argv != NULL) cat_input(*argv++); if (fclose(stdout)) err(EXIT_FAILURE, "stdout"); return status; } void cat_input(const char *path) { int fd; if (path == NULL || strcmp(path, "-") == 0) { if (cat_rw_fail(STDIN_FILENO)) goto fail; } else if ((fd = open(path, O_RDONLY)) == -1) { goto fail; } else { #ifdef linux if (cat_splice_fail(fd)) #endif if (cat_rw_fail(fd)) goto close_and_fail; close(fd); } return; close_and_fail: close(fd); fail: warn("%s", path); status = EXIT_FAILURE; } int cat_rw_fail(int rfd) { int wfd; ssize_t nr, nw, off; static size_t bsize; static char *buf = NULL; struct stat sbuf; wfd = fileno(stdout); if (buf == NULL) { if (fstat(wfd, &sbuf) == -1) err(EXIT_FAILURE, "stdout"); bsize = MAXIMUM(sbuf.st_blksize, BUFSIZ); if ((buf = malloc(bsize)) == NULL) err(EXIT_FAILURE, NULL); } while ((nr = read(rfd, buf, bsize)) > 0) for (off = 0; nr > 0; nr -= nw, off += nw) if ((nw = write(wfd, buf + off, nr)) < 1) err(EXIT_FAILURE, "stdout"); return nr == -1 ? 1 : 0; } #ifdef linux int cat_splice_fail(int rfd) { int pipefd[2]; ssize_t nr, nw; off_t off = 0; int rval = 0; if (pipe(pipefd) < 0) err(EXIT_FAILURE, "pipe"); do { nr = splice(rfd, &off, pipefd[1], NULL, block_size, SPLICE_F_MOVE | SPLICE_F_MORE); if (nr < 1) { if (nr < 0) rval = 1; break; } nw = splice(pipefd[0], NULL, STDOUT_FILENO, NULL, block_size, SPLICE_F_MOVE | SPLICE_F_MORE); if (nw < 0) { rval = 1; break; } } while (nw > 0); close(pipefd[0]); close(pipefd[1]); return rval; } #endif