summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@jacobmcdonnell.com>2025-05-29 20:10:01 -0400
committerJacob McDonnell <jacob@jacobmcdonnell.com>2025-05-29 20:10:01 -0400
commit5b05099d44ae6ac962be108f4c0da7493a9305c7 (patch)
tree1b796b3ff1491e83f26c9ba09fd6c48feac7c994 /src
Initial CommitHEADmain
Diffstat (limited to 'src')
-rw-r--r--src/hello.c20
-rw-r--r--src/io.c27
-rw-r--r--src/io.h10
-rw-r--r--src/makefile29
4 files changed, 86 insertions, 0 deletions
diff --git a/src/hello.c b/src/hello.c
new file mode 100644
index 0000000..b69ccdf
--- /dev/null
+++ b/src/hello.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include "io.h"
+
+int main(void) {
+ char buffer[BUFSIZ] = {0};
+ if (!ReadFile("version.txt", BUFSIZ, buffer)) {
+ return -1;
+ }
+
+ char *b = strrchr(buffer, '\n');
+ if (b != NULL) {
+ *b = '\0';
+ }
+
+ printf("Hello %s\n", buffer);
+ return 0;
+}
+
diff --git a/src/io.c b/src/io.c
new file mode 100644
index 0000000..78f679a
--- /dev/null
+++ b/src/io.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+bool ReadFile(const char * const path, const size_t n, char * const buffer) {
+ FILE *fp = fopen(path, "r");
+ if (fp == NULL) {
+ perror("ReadFile: fopen");
+ return false;
+ }
+
+ int j = 0;
+ size_t i = 0;
+ for (; i < n - 1; i++) {
+ j = fgetc(fp);
+ if (j == EOF) {
+ break;
+ }
+ buffer[i] = (char)j;
+ }
+
+ buffer[i] = '\0';
+
+ bool ret = (!ferror(fp) && feof(fp));
+ fclose(fp);
+ return ret;
+}
+
diff --git a/src/io.h b/src/io.h
new file mode 100644
index 0000000..7555ee5
--- /dev/null
+++ b/src/io.h
@@ -0,0 +1,10 @@
+#ifndef _IO_H
+#define _IO_H
+
+#include <stddef.h>
+#include <stdbool.h>
+
+bool ReadFile(const char * const path, const size_t n, char * const buffer);
+
+#endif
+
diff --git a/src/makefile b/src/makefile
new file mode 100644
index 0000000..ce91b59
--- /dev/null
+++ b/src/makefile
@@ -0,0 +1,29 @@
+CC = gcc-14
+CFLAGS = -Wall \
+ -Werror \
+ -Wextra \
+ -Wpedantic \
+ -std=c17
+SOURCE_DIR = .
+SOURCES = hello.c io.c
+OBJECT_DIR = object
+OBJECTS = $(SOURCES:%.c=$(OBJECT_DIR)/%.o)
+TARGET = hello
+
+all: $(TARGET)
+
+debug: CFLAGS += -g -O0
+debug: $(TARGET)
+
+clean:
+ rm -rf $(TARGET) $(OBJECT_DIR)
+
+$(TARGET): $(OBJECTS) | $(BIN_DIR)
+ $(CC) $(CFLAGS) -o $@ $^
+
+$(OBJECT_DIR)/%.o: %.c | $(OBJECT_DIR)
+ $(CC) $(CFLAGS) -c $^ -o $@
+
+$(OBJECT_DIR) $(BIN_DIR):
+ mkdir $@
+