forked from bonsai/harakit
cat(1p): refactor to get no args working
This commit is contained in:
parent
da2ebdb1e1
commit
b57a7179a5
87
src/cat.c
87
src/cat.c
@ -27,9 +27,37 @@
|
||||
#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) {
|
||||
@ -49,22 +77,27 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
FILE *file;
|
||||
/*
|
||||
* 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++) {
|
||||
/*
|
||||
* 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 (argv[i] == "-" || argc == 1) {
|
||||
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) {
|
||||
@ -88,31 +121,7 @@ int main(int argc, char *argv[]) {
|
||||
return EX_UNAVAILABLE;
|
||||
}
|
||||
}
|
||||
|
||||
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); }
|
||||
cat(file, u);
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
|
Loading…
Reference in New Issue
Block a user