1
0
forked from bonsai/harakit

cat(1p): refactor to get no args working

This commit is contained in:
Emma Tebibyte 2023-08-13 23:30:03 -06:00
parent da2ebdb1e1
commit b57a7179a5
Signed by untrusted user: emma
GPG Key ID: 6D661C738815E7DD

View File

@ -27,9 +27,37 @@
#include <string.h> #include <string.h>
#include <unistd.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[]) { int main(int argc, char *argv[]) {
bool u = false; bool u = false;
int opt; int opt;
int i;
extern int optind; extern int optind;
while ((opt = getopt(argc, argv, "u")) != -1) { while ((opt = getopt(argc, argv, "u")) != -1) {