summaryrefslogtreecommitdiff
path: root/src/cat.c
diff options
context:
space:
mode:
authorradiohotline <radiohotline@disroot.org>2026-03-14 16:20:47 +0300
committerradiohotline <radiohotline@disroot.org>2026-03-14 16:20:47 +0300
commitaad798d9df7417009fa4438aac61ac8e0e4ded36 (patch)
tree7729ac68c07e183215687d585203d89e862666e1 /src/cat.c
first commit
Diffstat (limited to 'src/cat.c')
-rw-r--r--src/cat.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/cat.c b/src/cat.c
new file mode 100644
index 0000000..b89cba6
--- /dev/null
+++ b/src/cat.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "cat"
+
+struct stat statbuf;
+
+int main(int argc, char* argv[]) {
+ FILE *fileptr = NULL;
+ char buf[8192];
+ char ch;
+
+ if (argc < 2) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ printf("%s", buf);
+ }
+
+ return 0;
+ }
+
+ if (access(argv[1], F_OK) != 0) {
+ printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
+ printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ fileptr = fopen(argv[1], "r");
+
+ if (fileptr == NULL){
+ perror("cat");
+ return 1;
+ }
+
+ while ((ch = fgetc(fileptr)) != EOF) {
+ putchar(ch);
+ }
+
+ fclose(fileptr);
+ return 0;
+}