forked from bonsai/harakit
105 lines
2.6 KiB
C
105 lines
2.6 KiB
C
/*
|
|
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
|
* 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 <sysexits.h>
|
|
#include <sys/stat.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
#include "yac.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 */
|
|
|
|
if (u) {
|
|
while ((byte = fgetc(file)) != EOF) { putchar(byte); }
|
|
} else {
|
|
while ((byte = fgetc(file)) != EOF) {
|
|
if (p > sizeof(buf) - 1) {
|
|
fputs(buf, stdout);
|
|
p = 0;
|
|
} else {
|
|
buf[p] = byte;
|
|
p++;
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
file = rpath(argv[0], argv[i]);
|
|
if (file != NULL) {
|
|
cat(file, u);
|
|
if (file != stdin) { fclose(file); }
|
|
} else { continue; }
|
|
}
|
|
|
|
return EX_OK;
|
|
}
|