coreutils/src/cat.c

129 lines
3.3 KiB
C

/*
* 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 <errno.h>
#include <fcntl.h>
#include <sysexits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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 */
while (byte != EOF) {
byte = fgetc(file);
if (u) {
putchar(byte);
} else {
if (p > sizeof(buf)) {
fputs(buf, stdout);
p = 0;
}
buf[p] = byte;
p += 1;
}
}
fwrite(buf, 1, p, stdout);
fflush(stdout);
if (file != stdin) { fclose(file); }
}
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:
printf("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);
}
FILE *file;
for (i = optind; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] != '\0') {
continue;
} if (strcmp(argv[i], "-") == 0) {
file = stdin;
} else if ((file = fopen(argv[i], "r")) == NULL) {
switch (errno) {
case EACCES:
printf("%s: %s: Permission denied.\n", argv[0], argv[i]);
return EX_NOINPUT;
case EISDIR:
printf("%s: %s: Is a directory.\n", argv[0], argv[i]);
return EX_NOINPUT;
case ELOOP:
printf("%s: %s: Is a symbolic link loop.\n", argv[0], argv[i]);
return EX_UNAVAILABLE;
case EMFILE:
printf("%s: Internal error.\n", argv[0]);
return EX_SOFTWARE;
case ENOENT: case ENOTDIR: case ENXIO:
printf("%s: %s: No such file or directory.\n", argv[0], argv[i]);
return EX_NOINPUT;
default:
printf("%s: Unknown error.\n", argv[0]);
return EX_UNAVAILABLE;
}
}
cat(file, u);
}
return EX_OK;
}