diff options
| author | Filip Wandzio <contact@philw.dev> | 2026-03-01 17:47:44 +0100 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2026-03-01 17:47:44 +0100 |
| commit | 5e9480f817e08dc0eddaee23321effb24012120d (patch) | |
| tree | 196d13fbcd19786108a51d75d5f99078ceba574a | |
| parent | bf0d77d7d448e964e9716d5af67c48f3d014f090 (diff) | |
| download | embedded_guardian-master.tar.gz embedded_guardian-master.zip | |
Reformat code like a sane person
Signed-off-by: Filip Wandzio <contact@philw.dev>
Diffstat (limited to '')
| -rw-r--r-- | Makefile | 9 | ||||
| -rw-r--r-- | benchmark/resource_usage.c | 29 | ||||
| -rw-r--r-- | include/syntax_essentials.h | 18 | ||||
| -rw-r--r-- | src/syntax_essentials.c | 34 |
4 files changed, 53 insertions, 37 deletions
| @@ -1,4 +1,3 @@ | |||
| 1 | # ================= COMPILER SETTINGS ================= | ||
| 2 | CC ?= gcc | 1 | CC ?= gcc |
| 3 | CFLAGS ?= -Wall -Wextra -Werror -std=c18 -Iinclude -Ibenchmark | 2 | CFLAGS ?= -Wall -Wextra -Werror -std=c18 -Iinclude -Ibenchmark |
| 4 | LDFLAGS ?= | 3 | LDFLAGS ?= |
| @@ -13,33 +12,27 @@ TESTS := $(wildcard $(TEST_DIR)/*.c) | |||
| 13 | 12 | ||
| 14 | OUT_HOST := $(BUILD_DIR)/syntax_essentials_tests_host | 13 | OUT_HOST := $(BUILD_DIR)/syntax_essentials_tests_host |
| 15 | 14 | ||
| 16 | # ================= TARGETS ================= | ||
| 17 | all: host | 15 | all: host |
| 18 | 16 | ||
| 19 | $(BUILD_DIR): | 17 | $(BUILD_DIR): |
| 20 | mkdir -p $(BUILD_DIR) | 18 | mkdir -p $(BUILD_DIR) |
| 21 | 19 | ||
| 22 | # ---------------- HOST BUILD (static) ---------------- | ||
| 23 | host: $(BUILD_DIR) $(SRC) $(TESTS) | 20 | host: $(BUILD_DIR) $(SRC) $(TESTS) |
| 24 | $(CC) $(CFLAGS) -static $(SRC) $(TESTS) -o $(OUT_HOST) $(LDFLAGS) | 21 | $(CC) $(CFLAGS) $(SRC) $(TESTS) -o $(OUT_HOST) $(LDFLAGS) |
| 25 | @echo "[INFO] Host binary built: $(OUT_HOST)" | 22 | @echo "[INFO] Host binary built: $(OUT_HOST)" |
| 26 | 23 | ||
| 27 | # ---------------- ESP32 SIM ---------------- | ||
| 28 | esp32_sim: host | 24 | esp32_sim: host |
| 29 | @echo "[INFO] Running ESP32-S3 simulation in minimal Docker..." | 25 | @echo "[INFO] Running ESP32-S3 simulation in minimal Docker..." |
| 30 | docker build -t esp32-s3-sim . | 26 | docker build -t esp32-s3-sim . |
| 31 | docker run --rm --memory=6m --cpus=0.2 esp32-s3-sim | 27 | docker run --rm --memory=6m --cpus=0.2 esp32-s3-sim |
| 32 | 28 | ||
| 33 | # ---------------- CLEAN ---------------- | ||
| 34 | clean: | 29 | clean: |
| 35 | rm -rf $(BUILD_DIR) | 30 | rm -rf $(BUILD_DIR) |
| 36 | 31 | ||
| 37 | # ---------------- INFO ---------------- | ||
| 38 | info: | 32 | info: |
| 39 | @echo "Host compiler: $(CC)" | 33 | @echo "Host compiler: $(CC)" |
| 40 | @echo "Binary: $(OUT_HOST)" | 34 | @echo "Binary: $(OUT_HOST)" |
| 41 | 35 | ||
| 42 | # ---------------- HELP ---------------- | ||
| 43 | help: | 36 | help: |
| 44 | @echo "Available targets:" | 37 | @echo "Available targets:" |
| 45 | @echo " all / host - Build host binary (statically linked)" | 38 | @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 @@ | |||
| 3 | #include <sys/resource.h> | 3 | #include <sys/resource.h> |
| 4 | #include <sys/time.h> | 4 | #include <sys/time.h> |
| 5 | 5 | ||
| 6 | void print_resource_usage(const char *label) { | 6 | void print_resource_usage(const char* label) |
| 7 | struct rusage usage; | 7 | { |
| 8 | if (getrusage(RUSAGE_SELF, &usage) == 0) { | 8 | struct rusage usage; |
| 9 | long mem_kb = usage.ru_maxrss; | 9 | if (getrusage(RUSAGE_SELF, &usage) == 0) { |
| 10 | double user_sec = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6; | 10 | const long mem_kb = usage.ru_maxrss; |
| 11 | double sys_sec = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1e6; | 11 | const double user_sec = |
| 12 | printf("[%s] CPU user: %.3f s, system: %.3f s, peak memory: %ld KB\n", | 12 | usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6; |
| 13 | label, user_sec, sys_sec, mem_kb); | 13 | const double sys_sec = |
| 14 | } else { | 14 | usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1e6; |
| 15 | printf("[%s] Resource usage not available\n", label); | 15 | printf("[%s] CPU user: %.3f s, system: %.3f s, peak memory: " |
| 16 | } | 16 | "%ld KB\n", |
| 17 | label, | ||
| 18 | user_sec, | ||
| 19 | sys_sec, | ||
| 20 | mem_kb); | ||
| 21 | } else { | ||
| 22 | printf("[%s] Resource usage not available\n", label); | ||
| 23 | } | ||
| 17 | } | 24 | } |
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 | |||
| @@ -2,6 +2,20 @@ | |||
| 2 | 2 | ||
| 3 | /** | 3 | /** |
| 4 | * | 4 | * |
| 5 | * TASK 0: result codes | ||
| 6 | * -------------------- | ||
| 7 | * Idiomatic named return codes instead of raw numbers. | ||
| 8 | */ | ||
| 9 | typedef enum { | ||
| 10 | RESULT_VALID = 0, | ||
| 11 | RESULT_INVALID_NAME = 1, | ||
| 12 | RESULT_INVALID_INDEX = 2, | ||
| 13 | RESULT_ARRAY_FULL = 3, | ||
| 14 | RESULT_NULL_POINTER = 4 | ||
| 15 | } Result; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * | ||
| 5 | * TASK 1: validate_first_name | 19 | * TASK 1: validate_first_name |
| 6 | * --------------------------- | 20 | * --------------------------- |
| 7 | * Check if the first name is valid. | 21 | * Check if the first name is valid. |
| @@ -70,7 +84,7 @@ typedef struct { | |||
| 70 | * ------------------- | 84 | * ------------------- |
| 71 | * Add a student to an array of Student. Return 0 if added. | 85 | * Add a student to an array of Student. Return 0 if added. |
| 72 | */ | 86 | */ |
| 73 | int add_student(Student *array, int max_size, const char *name, | 87 | int add_student(Student *array, int max_length, const char *name, |
| 74 | const char *index); | 88 | const char *index); |
| 75 | 89 | ||
| 76 | /** | 90 | /** |
| @@ -78,4 +92,4 @@ int add_student(Student *array, int max_size, const char *name, | |||
| 78 | * ------------------------ | 92 | * ------------------------ |
| 79 | * Return error message string for code returned by register_student | 93 | * Return error message string for code returned by register_student |
| 80 | */ | 94 | */ |
| 81 | const char *get_error_message(int code); | 95 | 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 @@ | |||
| 1 | #include "syntax_essentials.h" | 1 | #include "syntax_essentials.h" |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <string.h> | ||
| 3 | 4 | ||
| 4 | /** | 5 | /** |
| 5 | * | 6 | * |
| @@ -18,6 +19,7 @@ int validate_first_name(const char* first_name) | |||
| 18 | return 0; | 19 | return 0; |
| 19 | 20 | ||
| 20 | int length = 0; | 21 | int length = 0; |
| 22 | |||
| 21 | for (const char* p = first_name; *p; p++) { | 23 | for (const char* p = first_name; *p; p++) { |
| 22 | if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) | 24 | if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) |
| 23 | length++; | 25 | length++; |
| @@ -69,6 +71,7 @@ int validate_full_name(const char* full_name) | |||
| 69 | return 0; | 71 | return 0; |
| 70 | 72 | ||
| 71 | char first_name[64], last_name[64]; | 73 | char first_name[64], last_name[64]; |
| 74 | |||
| 72 | if (sscanf(full_name, "%63s %63s", first_name, last_name) != 2) | 75 | if (sscanf(full_name, "%63s %63s", first_name, last_name) != 2) |
| 73 | return 0; | 76 | return 0; |
| 74 | 77 | ||
| @@ -86,6 +89,7 @@ int validate_index(const char* index) | |||
| 86 | return 0; | 89 | return 0; |
| 87 | 90 | ||
| 88 | int length = 0; | 91 | int length = 0; |
| 92 | |||
| 89 | for (const char* p = index; *p; p++) { | 93 | for (const char* p = index; *p; p++) { |
| 90 | if (*p >= '0' && *p <= '9') | 94 | if (*p >= '0' && *p <= '9') |
| 91 | length++; | 95 | length++; |
| @@ -106,8 +110,10 @@ int register_student(const char* full_name, const char* index) | |||
| 106 | { | 110 | { |
| 107 | if (!validate_full_name(full_name)) | 111 | if (!validate_full_name(full_name)) |
| 108 | return 1; | 112 | return 1; |
| 113 | |||
| 109 | if (!validate_index(index)) | 114 | if (!validate_index(index)) |
| 110 | return 2; | 115 | return 2; |
| 116 | |||
| 111 | return 0; | 117 | return 0; |
| 112 | } | 118 | } |
| 113 | 119 | ||
| @@ -118,41 +124,37 @@ int register_student(const char* full_name, const char* index) | |||
| 118 | */ | 124 | */ |
| 119 | int name_length(const char* name) | 125 | int name_length(const char* name) |
| 120 | { | 126 | { |
| 121 | if (!name) | 127 | return name ? (int)strlen(name) : 0; |
| 122 | return 0; | ||
| 123 | |||
| 124 | int length = 0; | ||
| 125 | while (*name++) | ||
| 126 | length++; | ||
| 127 | return length; | ||
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | /** | 130 | /** |
| 131 | * | 131 | * |
| 132 | * @param array | 132 | * @param array |
| 133 | * @param max_size | 133 | * @param max_length |
| 134 | * @param name | 134 | * @param name |
| 135 | * @param index | 135 | * @param index |
| 136 | * @return | 136 | * @return |
| 137 | */ | 137 | */ |
| 138 | int add_student(Student* array, | 138 | int add_student(Student* array, |
| 139 | const int max_size, | 139 | const int max_length, |
| 140 | const char* name, | 140 | const char* name, |
| 141 | const char* index) | 141 | const char* index) |
| 142 | { | 142 | { |
| 143 | if (!name || !index || !array) | 143 | if (!name || !index || !array) |
| 144 | return 2; | 144 | return 2; |
| 145 | 145 | ||
| 146 | int len = 0; | 146 | int length = 0; |
| 147 | while (len < max_size && array[len].full_name[0] != '\0') | 147 | while (length < max_length && array[length].full_name[0] != '\0') |
| 148 | len++; | 148 | length++; |
| 149 | 149 | ||
| 150 | if (len >= max_size) | 150 | if (length >= max_length) |
| 151 | return 1; | 151 | return 1; |
| 152 | 152 | ||
| 153 | snprintf( | 153 | snprintf(array[length].full_name, |
| 154 | array[len].full_name, sizeof(array[len].full_name), "%s", name); | 154 | sizeof(array[length].full_name), |
| 155 | snprintf(array[len].index, sizeof(array[len].index), "%s", index); | 155 | "%s", |
| 156 | name); | ||
| 157 | snprintf(array[length].index, sizeof(array[length].index), "%s", index); | ||
| 156 | return 0; | 158 | return 0; |
| 157 | } | 159 | } |
| 158 | 160 | ||
