/* * Copyright (c) 2023 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This file is part of YAC coreutils. * * YAC coreutils is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * YAC coreutils is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ #include #include #include #include #include #include void cat(FILE *file, bool u) { int byte = 0; /* variable for storing bytes as they are read */ int p = 0; /* index counter for bytes in buffered reading */ char buf[4096]; /* buffer for buffered reading */ if (u) { while (byte != EOF) { byte = fgetc(file); putchar(byte); } } else { while (byte != EOF) { byte = fgetc(file); if (p > sizeof(buf)) { fputs(buf, stdout); p = 0; } else { buf[p] = byte; p += 1; } } fwrite(buf, 1, p, stdout); fflush(stdout); } } int main(int argc, char *argv[]) { bool u = false; int opt; int i; extern int optind; while ((opt = getopt(argc, argv, "u")) != -1) { switch (opt) { /* * From cat(1p): * * -u Write bytes from the input file to the standard output * without delay as each is read. */ case 'u': u = true; break; default: fprintf(stderr, "Usage: %s (-u) file...\n", argv[0]); return EX_USAGE; } } /* * From cat(1p): * * file A pathname of an input file. If no file operands are * specified, the standard input shall be used. If a file is * '-', the cat utility shall read from the standard input at * that point in the sequence. The cat utility shall not close * and reopen standard input when it is referenced in this way, * but shall accept multiple occurrences of '-' as a file * operand. */ if (optind == argc) { cat(stdin, u); return 0; } FILE *file; struct stat stats; for (i = optind; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case '\0': file = stdin; break; default: continue; } } else if (stat(argv[i], &stats) == 0 && S_ISDIR(stats.st_mode)) { fprintf(stderr, "%s: %s: Is a directory.\n", argv[0], argv[i]); return EX_NOINPUT; } else if ((file = fopen(argv[i], "r")) == NULL) { switch (errno) { case EACCES: fprintf(stderr, "%s: %s: Permission denied.\n", argv[0], argv[i]); return EX_NOINPUT; case EISDIR: fprintf(stderr, "%s: %s: Is a directory.\n", argv[0], argv[i]); return EX_NOINPUT; case ELOOP: fprintf(stderr, "%s: %s: Is a symbolic link loop.\n", argv[0], argv[i]); return EX_UNAVAILABLE; case EMFILE: fprintf(stderr, "%s: Internal error.\n", argv[0]); return EX_SOFTWARE; case ENOENT: case ENOTDIR: case ENXIO: fprintf(stderr, "%s: %s: No such file or directory.\n", argv[0], argv[i]); return EX_NOINPUT; default: fprintf(stderr, "%s: Unknown error.\n", argv[0]); return EX_UNAVAILABLE; } } cat(file, u); if (file != stdin) { fclose(file); } } return EX_OK; }