← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/cat.c
diff options
context:
space:
mode:
authorDavid Faulkner <[email protected]>2026-08-07 23:40:47 -0500
committerDavid Faulkner <[email protected]>2026-08-07 23:40:47 -0500
commitb3e9e62599532050fc776c5e8f076915b56c2235 (patch)
treeaf252346106a61b18cc6fc6fdbd32e962d096f1c /cat.c
Import official C23 code examples for Modern C (Jens Gustedt, 2024)HEADupstream-importmain
- Add official C source files, Makefile, c23-fallback.h, and LICENSE - Update README.md with study mirror notice
Diffstat (limited to 'cat.c')
-rw-r--r--cat.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/cat.c b/cat.c
new file mode 100644
index 0000000..9baadb5
--- /dev/null
+++ b/cat.c
@@ -0,0 +1,26 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+enum { buf_max = 32, };
+
+int main(int argc, char* argv[argc+1]) {
+ int ret = EXIT_FAILURE;
+ char buffer[buf_max] = { };
+ for (int i = 1; i < argc; ++i) { // Processes args
+ FILE* instream = fopen(argv[i], "r"); // as filenames
+ if (instream) {
+ while (fgets(buffer, buf_max, instream)) {
+ fputs(buffer, stdout);
+ }
+ fclose(instream);
+ ret = EXIT_SUCCESS;
+ } else {
+ /* Provides some error diagnostic. */
+ fprintf(stderr, "Could not open %s: ", argv[i]);
+ perror(0);
+ errno = 0; // Resets the error code
+ }
+ }
+ return ret;
+}