From 5e9480f817e08dc0eddaee23321effb24012120d Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Sun, 1 Mar 2026 17:47:44 +0100 Subject: Disable static linking for macos cross-compilation Reformat code like a sane person Signed-off-by: Filip Wandzio --- Makefile | 9 +-------- benchmark/resource_usage.c | 29 ++++++++++++++++++----------- include/syntax_essentials.h | 18 ++++++++++++++++-- src/syntax_essentials.c | 34 ++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 4d0bb31..ea7961d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -# ================= COMPILER SETTINGS ================= CC ?= gcc CFLAGS ?= -Wall -Wextra -Werror -std=c18 -Iinclude -Ibenchmark LDFLAGS ?= @@ -13,33 +12,27 @@ TESTS := $(wildcard $(TEST_DIR)/*.c) OUT_HOST := $(BUILD_DIR)/syntax_essentials_tests_host -# ================= TARGETS ================= all: host $(BUILD_DIR): mkdir -p $(BUILD_DIR) -# ---------------- HOST BUILD (static) ---------------- host: $(BUILD_DIR) $(SRC) $(TESTS) - $(CC) $(CFLAGS) -static $(SRC) $(TESTS) -o $(OUT_HOST) $(LDFLAGS) + $(CC) $(CFLAGS) $(SRC) $(TESTS) -o $(OUT_HOST) $(LDFLAGS) @echo "[INFO] Host binary built: $(OUT_HOST)" -# ---------------- ESP32 SIM ---------------- esp32_sim: host @echo "[INFO] Running ESP32-S3 simulation in minimal Docker..." docker build -t esp32-s3-sim . docker run --rm --memory=6m --cpus=0.2 esp32-s3-sim -# ---------------- CLEAN ---------------- clean: rm -rf $(BUILD_DIR) -# ---------------- INFO ---------------- info: @echo "Host compiler: $(CC)" @echo "Binary: $(OUT_HOST)" -# ---------------- HELP ---------------- help: @echo "Available targets:" @echo " all / host - Build host binary (statically linked)" diff --git a/benchmark/resource_usage.c b/benchmark/resource_usage.c index 04f7195..8741215 100644 --- a/benchmark/resource_usage.c +++ b/benchmark/resource_usage.c @@ -3,15 +3,22 @@ #include #include -void print_resource_usage(const char *label) { - struct rusage usage; - if (getrusage(RUSAGE_SELF, &usage) == 0) { - long mem_kb = usage.ru_maxrss; - double user_sec = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6; - double sys_sec = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1e6; - printf("[%s] CPU user: %.3f s, system: %.3f s, peak memory: %ld KB\n", - label, user_sec, sys_sec, mem_kb); - } else { - printf("[%s] Resource usage not available\n", label); - } +void print_resource_usage(const char* label) +{ + struct rusage usage; + if (getrusage(RUSAGE_SELF, &usage) == 0) { + const long mem_kb = usage.ru_maxrss; + const double user_sec = + usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6; + const double sys_sec = + usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1e6; + printf("[%s] CPU user: %.3f s, system: %.3f s, peak memory: " + "%ld KB\n", + label, + user_sec, + sys_sec, + mem_kb); + } else { + printf("[%s] Resource usage not available\n", label); + } } diff --git a/include/syntax_essentials.h b/include/syntax_essentials.h index 181cb99..2327636 100644 --- a/include/syntax_essentials.h +++ b/include/syntax_essentials.h @@ -1,5 +1,19 @@ #pragma once +/** + * + * TASK 0: result codes + * -------------------- + * Idiomatic named return codes instead of raw numbers. + */ +typedef enum { + RESULT_VALID = 0, + RESULT_INVALID_NAME = 1, + RESULT_INVALID_INDEX = 2, + RESULT_ARRAY_FULL = 3, + RESULT_NULL_POINTER = 4 +} Result; + /** * * TASK 1: validate_first_name @@ -70,7 +84,7 @@ typedef struct { * ------------------- * Add a student to an array of Student. Return 0 if added. */ -int add_student(Student *array, int max_size, const char *name, +int add_student(Student *array, int max_length, const char *name, const char *index); /** @@ -78,4 +92,4 @@ int add_student(Student *array, int max_size, const char *name, * ------------------------ * Return error message string for code returned by register_student */ -const char *get_error_message(int code); +const char *get_error_message(int code); \ No newline at end of file diff --git a/src/syntax_essentials.c b/src/syntax_essentials.c index 419996a..eefdab1 100644 --- a/src/syntax_essentials.c +++ b/src/syntax_essentials.c @@ -1,5 +1,6 @@ #include "syntax_essentials.h" #include +#include /** * @@ -18,6 +19,7 @@ int validate_first_name(const char* first_name) return 0; int length = 0; + for (const char* p = first_name; *p; p++) { if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) length++; @@ -69,6 +71,7 @@ int validate_full_name(const char* full_name) return 0; char first_name[64], last_name[64]; + if (sscanf(full_name, "%63s %63s", first_name, last_name) != 2) return 0; @@ -86,6 +89,7 @@ int validate_index(const char* index) return 0; int length = 0; + for (const char* p = index; *p; p++) { if (*p >= '0' && *p <= '9') length++; @@ -106,8 +110,10 @@ int register_student(const char* full_name, const char* index) { if (!validate_full_name(full_name)) return 1; + if (!validate_index(index)) return 2; + return 0; } @@ -118,41 +124,37 @@ int register_student(const char* full_name, const char* index) */ int name_length(const char* name) { - if (!name) - return 0; - - int length = 0; - while (*name++) - length++; - return length; + return name ? (int)strlen(name) : 0; } /** * * @param array - * @param max_size + * @param max_length * @param name * @param index * @return */ int add_student(Student* array, - const int max_size, + const int max_length, const char* name, const char* index) { if (!name || !index || !array) return 2; - int len = 0; - while (len < max_size && array[len].full_name[0] != '\0') - len++; + int length = 0; + while (length < max_length && array[length].full_name[0] != '\0') + length++; - if (len >= max_size) + if (length >= max_length) return 1; - snprintf( - array[len].full_name, sizeof(array[len].full_name), "%s", name); - snprintf(array[len].index, sizeof(array[len].index), "%s", index); + snprintf(array[length].full_name, + sizeof(array[length].full_name), + "%s", + name); + snprintf(array[length].index, sizeof(array[length].index), "%s", index); return 0; } -- cgit v1.2.3