Author | Jamozed <[email protected]> |
Date | 2020-09-16 07:26:59 |
Commit | 5fb1515bb308a27e17fdf4cb473ce43d2e569ace |
Parent | 30cf081fc83e87553e77b61ad0a885a0a36f0ad5 |
cat: Add comments
Diffstat
M | src/cat.c | | | 13 | ++++++++++--- |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/cat.c b/src/cat.c index 5808bc9..7f9a949 100644 --- a/src/cat.c +++ b/src/cat.c @@ -58,6 +58,7 @@ int main(int ac, char *av[]) { A0 = av[0]; default: { return 1; } } + // Disable buffering on stdout setvbuf(stdout, NULL, _IONBF, 0); bool warned = false; @@ -69,19 +70,24 @@ int main(int ac, char *av[]) { A0 = av[0]; return warned; } +/* + Write a file to stdout using a fixed size buffer. + If the file path given is "-", then use stdin. +*/ static inline int cat(const char *file) { - char stdbuf[BUFSIZ * 16]; FILE *fi; + char buf[BUFSIZ * 16]; FILE *fi; if (file[0] == '-' && file[1] == 0) { fi = stdin; } else if (!(fi = fopen(file, "r"))) { return 1; } - for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), fi)) != 0;) { - fwrite(stdbuf, 1, c, stdout); + for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)) != 0;) { + fwrite(buf, 1, c, stdout); } if (fi != stdin) { fclose(fi); } return 0; } +/* Print help information */ static void hlp(void) { puts("cat - concatenate and print files\n"); puts("usage: cat [-u] [file...]\n"); @@ -92,6 +98,7 @@ static void hlp(void) { return; } +/* Print version information */ static void ver(void) { puts("OMKOV coreutils cat, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling");