/* $shbox: cat.c,v 20220701 2022/07/02 06:02: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. */ #include #include #include #include #include #include #include #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) int status; void cat_input(const char *path); void cat_spew(int rfd, const char *path); int main(int argc, char *argv[]) { int ch; setvbuf(stdout, NULL, _IOLBF, 0); while ((ch = getopt(argc, argv, "u")) != -1) { if (ch != 'u') { fprintf(stderr, "usage: cat [-u] [file ...]\n"); exit(1); } setvbuf(stdout, NULL, _IONBF, 0); } argc -= optind; argv += optind; if (argc == 0) cat_input(NULL); else while (*argv != NULL) cat_input(*argv++); if (fclose(stdout)) err(1, "stdout"); return status; } void cat_input(const char *path) { FILE *fp; int fd; if (path == NULL || strcmp(path, "-") == 0) { cat_spew(STDIN_FILENO, "stdin"); } else if ((fd = open(path, O_RDONLY)) == -1) { warn("%s", path); status = 1; } else { cat_spew(fd, path); close(fd); } } void cat_spew(int rfd, const char *path) { 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(1, "stdout"); bsize = MAXIMUM(sbuf.st_blksize, BUFSIZ); if ((buf = malloc(bsize)) == NULL) err(1, NULL); } while ((nr = read(rfd, buf, bsize)) != -1 && nr != 0) for (off = 0; nr; nr -= nw, off += nw) if ((nw = write(wfd, buf + off, nr)) == -1 || nw == 0) err(1, "stdout"); if (nr == -1) { warn("%s", path); status = 1; } }