From 42cd075421584e0fb92121fb1872f3d6c71b6534 Mon Sep 17 00:00:00 2001 From: xesc Date: Tue, 8 Oct 2024 14:55:58 +0200 Subject: [PATCH 01/10] makefile: add run target --- makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 24aee5e..18eac52 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,11 @@ -.PHONY: all debug -all: src/wta.c +.PHONY: debug release run + +release: src/wta.c gcc -o build/wta -O2 -Wall -Wextra -pedantic -Werror src/wta.c +run: release + ./build/wta + debug: src/wta.c gcc -o build/wta -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wta.c From 06f1fe37ad95fc6f5f7cc5c1f6085bf7b285d848 Mon Sep 17 00:00:00 2001 From: xesc Date: Tue, 8 Oct 2024 14:57:57 +0200 Subject: [PATCH 02/10] wta.c: add color and debug macros --- src/wta.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/wta.c b/src/wta.c index f8b643a..b053875 100644 --- a/src/wta.c +++ b/src/wta.c @@ -1,3 +1,18 @@ +#include + +#ifdef DEBUG +#define dbg(format, ...) do { \ + printf("\033[1;35m[DEBUG] (func: %s) msg:\033[0;35m " format "\033[0m\n",__func__, __VA_ARGS__); \ +} while(0) +#else +#define dbg(format, ...) +#endif +#define c_red "\033[1;31m" +#define c_green "\033[1;32m" +#define c_yellow "\033[1;33m" +#define c_end "\033[0m" + + int main() { return 0; From c03c9eb9cf9adc1227e3eb7664c28b3d319df75c Mon Sep 17 00:00:00 2001 From: xesc Date: Tue, 8 Oct 2024 14:58:47 +0200 Subject: [PATCH 03/10] wta.c: add basic argument handling --- src/wta.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wta.c b/src/wta.c index b053875..27fb825 100644 --- a/src/wta.c +++ b/src/wta.c @@ -13,7 +13,16 @@ #define c_end "\033[0m" -int main() +int main(const int argc, const char **argv) { + if (2 == argc) { + if ('-' == argv[1][0] && 'h' == argv[1][1] && '\0' == argv[1][2]) { + printf("wte - what to eat\na simple decision making tool for food\n"); + } else { + printf("unrecognized option, try %s -h\n", *argv); + } + goto exit_main; + } +exit_main: return 0; } From 67a044a3c9423d61874f5265ef0104043d1bf82f Mon Sep 17 00:00:00 2001 From: xesc Date: Thu, 10 Oct 2024 15:44:58 +0200 Subject: [PATCH 04/10] wta.c -> wte.c: rename & add error macro. --- makefile | 10 +++++----- src/{wta.c => wte.c} | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) rename src/{wta.c => wte.c} (70%) diff --git a/makefile b/makefile index 18eac52..6ef4d47 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,11 @@ .PHONY: debug release run -release: src/wta.c - gcc -o build/wta -O2 -Wall -Wextra -pedantic -Werror src/wta.c +release: src/wte.c + gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/wte.c run: release - ./build/wta + ./build/wte -debug: src/wta.c - gcc -o build/wta -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wta.c +debug: src/wte.c + gcc -o build/wte -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wte.c diff --git a/src/wta.c b/src/wte.c similarity index 70% rename from src/wta.c rename to src/wte.c index 27fb825..c353686 100644 --- a/src/wta.c +++ b/src/wte.c @@ -7,22 +7,38 @@ #else #define dbg(format, ...) #endif + #define c_red "\033[1;31m" #define c_green "\033[1;32m" #define c_yellow "\033[1;33m" #define c_end "\033[0m" +#define error(format, ...) do {\ + fprintf(stderr, c_red "[ERROR] "format c_end"\n", __VA_ARGS__); \ +} while(0) + + +static int get_proposition() +{ + int ret = 0; + return ret; +} int main(const int argc, const char **argv) { if (2 == argc) { if ('-' == argv[1][0] && 'h' == argv[1][1] && '\0' == argv[1][2]) { - printf("wte - what to eat\na simple decision making tool for food\n"); + printf("wte - what to eat\na simple decision making tool for food\nuse without options\n"); } else { printf("unrecognized option, try %s -h\n", *argv); } goto exit_main; } + if (!get_proposition()) { + error("%s", "error"); + } + + exit_main: return 0; } From 20f4ae05810a6e7c7216cf52cb1935a949eb2ae9 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:04:46 +0200 Subject: [PATCH 05/10] makefile: add clean-target, make build-targets more generic --- makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/makefile b/makefile index 6ef4d47..c494dd2 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,14 @@ .PHONY: debug release run -release: src/wte.c - gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/wte.c +release: src/*.c + gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/*.c run: release ./build/wte -debug: src/wte.c - gcc -o build/wte -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wte.c +debug: src/*.c + gcc -o build/wte -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/*.c + +clean: + rm -f build/* From 64d5fb6f8988114fd70f4e2d6cef331b0543c9b1 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:09:32 +0200 Subject: [PATCH 06/10] wte.c: restructure into seperate files --- src/file.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/manage.c | 14 +++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/file.c create mode 100644 src/manage.c diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..eba640d --- /dev/null +++ b/src/file.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include "wte.h" + +char *get_path() +{ + const char *home = getenv("HOME"); + char *path = NULL; + if (home) { + size_t path_len = strlen(home) + sizeof(DATA_DIR); + path = malloc(sizeof(char) * path_len); + if (path) { + snprintf(path, path_len, "%s" DATA_DIR, home); + dbg("path: %s", path); + } + } + return path; +} + +int create_data_dir() +{ + int ret = 0; + struct stat st; + int dir_exists = stat(DATA_DIR, &st); + if (-1 == dir_exists) { + dbg("%s", "creating data dir"); + char *path = get_path(); + if (path && 0 != (ret = mkdir(path, 0700))) { + error("%s", "failed to create directory"); + } + free(path); + } + return ret; +} + +static int remove_file() +{ + int ret = 0; + return ret; +} + +int remove_data_dir() +{ + int ret = 0; + ret = remove_file(); + char *path = get_path(); + if (path) { + // here: for loop to delete all entries in directory + ret = remove(path); + free(path); + } + dbg("ret: %d", ret); + return ret; +} diff --git a/src/manage.c b/src/manage.c new file mode 100644 index 0000000..67ae9bd --- /dev/null +++ b/src/manage.c @@ -0,0 +1,14 @@ +#include "wte.h" + +int insert_choice() +{ + int ret = 1; + return ret; +} + +int remove_choice() +{ + int ret = 1; + error("%s", "unable to remove choice"); + return ret; +} From 05bc9d24d3d61e051da632405b3c7cf442902207 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:06:18 +0200 Subject: [PATCH 07/10] wte.c: move makros to wte.h, add wte.h --- src/wte.c | 21 +++------------------ src/wte.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 src/wte.h diff --git a/src/wte.c b/src/wte.c index c353686..42a1a3d 100644 --- a/src/wte.c +++ b/src/wte.c @@ -1,21 +1,6 @@ -#include - -#ifdef DEBUG -#define dbg(format, ...) do { \ - printf("\033[1;35m[DEBUG] (func: %s) msg:\033[0;35m " format "\033[0m\n",__func__, __VA_ARGS__); \ -} while(0) -#else -#define dbg(format, ...) -#endif - -#define c_red "\033[1;31m" -#define c_green "\033[1;32m" -#define c_yellow "\033[1;33m" -#define c_end "\033[0m" - -#define error(format, ...) do {\ - fprintf(stderr, c_red "[ERROR] "format c_end"\n", __VA_ARGS__); \ -} while(0) +#include +#include +#include "wte.h" static int get_proposition() diff --git a/src/wte.h b/src/wte.h new file mode 100644 index 0000000..b11abed --- /dev/null +++ b/src/wte.h @@ -0,0 +1,31 @@ +#include +#define error(format, ...) do {\ + fprintf(stderr, c_red "[ERROR] "format c_end"\n", __VA_ARGS__); \ +} while(0) + +#ifdef DEBUG +#define dbg(format, ...) do { \ + printf("\033[1;35m[DEBUG] (func: %s) msg:\033[0;35m " format "\033[0m\n",__func__, __VA_ARGS__); \ +} while(0) +#else +#define dbg(format, ...) +#endif + +#define c_red "\033[1;31m" +#define c_green "\033[1;32m" +#define c_yellow "\033[1;33m" +#define c_end "\033[0m" + +/* path to data storage */ +#define DATA_DIR "/.local/share/wte" + +/* get full path to data storage (with "/home/$USER/"-prefix) */ +char *get_path(); + +/* data storage creation / deletion */ +int create_data_dir(); +int remove_data_dir(); + +/* operating on data storage */ +int insert_choice(); +int remove_choice(); From 94ad845dab8fc1f8084da5ff9318b8eb75591d83 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:07:34 +0200 Subject: [PATCH 08/10] wte.c: add more command line arguments, add variable returnvalue in main --- src/wte.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/wte.c b/src/wte.c index 42a1a3d..3c784ce 100644 --- a/src/wte.c +++ b/src/wte.c @@ -11,9 +11,18 @@ static int get_proposition() int main(const int argc, const char **argv) { - if (2 == argc) { - if ('-' == argv[1][0] && 'h' == argv[1][1] && '\0' == argv[1][2]) { + int ret = 1; + if (2 == argc && '-' == argv[1][0]) { + if ('h' == argv[1][1] && '\0' == argv[1][2]) { printf("wte - what to eat\na simple decision making tool for food\nuse without options\n"); + } else if ('a' == argv[1][1] && '\0' == argv[1][2]) { + ret = insert_choice(); + } else if ('d' == argv[1][1] && '\0' == argv[1][2]) { + ret = remove_choice(); + } else if ('c' == argv[1][1] && '\0' == argv[1][2]) { + ret = create_data_dir(); + } else if ('x' == argv[1][1] && '\0' == argv[1][2]) { + ret = remove_data_dir(); } else { printf("unrecognized option, try %s -h\n", *argv); } @@ -25,5 +34,5 @@ int main(const int argc, const char **argv) exit_main: - return 0; + return ret; } From 49fe65547475de870c5e6bfdf56c234a02b4d774 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:08:47 +0200 Subject: [PATCH 09/10] wte.c: add prototype for random selection --- src/wte.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/wte.c b/src/wte.c index 3c784ce..50e6fa5 100644 --- a/src/wte.c +++ b/src/wte.c @@ -2,10 +2,22 @@ #include #include "wte.h" +static size_t get_entry_count() +{ + size_t count = 0; + count++; + return count; +} -static int get_proposition() +static int make_suggestion() { int ret = 0; + srandom(time(NULL)); // seed random with current time + size_t count = get_entry_count(); + if (count) { + size_t choice = random() % count; + printf("choice: %lu", choice); + } return ret; } @@ -28,10 +40,10 @@ int main(const int argc, const char **argv) } goto exit_main; } - if (!get_proposition()) { - error("%s", "error"); - } + if (make_suggestion()) { + ret = 0; + } exit_main: return ret; From 07dff4f2d2ca7e33bef3a2f2000c397220b52016 Mon Sep 17 00:00:00 2001 From: xesc Date: Sat, 12 Oct 2024 21:31:46 +0200 Subject: [PATCH 10/10] wte.h, file.c: refactor colors, add info-makro --- src/file.c | 8 +++++++- src/wte.h | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/file.c b/src/file.c index eba640d..e67beb3 100644 --- a/src/file.c +++ b/src/file.c @@ -24,10 +24,11 @@ int create_data_dir() struct stat st; int dir_exists = stat(DATA_DIR, &st); if (-1 == dir_exists) { - dbg("%s", "creating data dir"); char *path = get_path(); if (path && 0 != (ret = mkdir(path, 0700))) { error("%s", "failed to create directory"); + } else { + info("%s", "created data directory"); } free(path); } @@ -48,6 +49,11 @@ int remove_data_dir() if (path) { // here: for loop to delete all entries in directory ret = remove(path); + if (0 == ret) { + info("%s", "removed data directory"); + } else { + error("%s", "failed to remove data directory"); + } free(path); } dbg("ret: %d", ret); diff --git a/src/wte.h b/src/wte.h index b11abed..b9d9823 100644 --- a/src/wte.h +++ b/src/wte.h @@ -1,19 +1,27 @@ #include #define error(format, ...) do {\ - fprintf(stderr, c_red "[ERROR] "format c_end"\n", __VA_ARGS__); \ + fprintf(stderr, c_red format c_end"\n", __VA_ARGS__); \ } while(0) +#define info(format, ...) do {\ + fprintf(stdout, c_blue format c_end "\n", __VA_ARGS__); \ +} while(0); + #ifdef DEBUG #define dbg(format, ...) do { \ - printf("\033[1;35m[DEBUG] (func: %s) msg:\033[0;35m " format "\033[0m\n",__func__, __VA_ARGS__); \ + printf(c_bold c_lila"[DEBUG] (func: %s) msg: " c_end c_lila format c_end "\n",__func__, __VA_ARGS__); \ } while(0) #else #define dbg(format, ...) #endif -#define c_red "\033[1;31m" -#define c_green "\033[1;32m" -#define c_yellow "\033[1;33m" +/* easy colors */ +#define c_red "\033[31m" +#define c_green "\033[32m" +#define c_yellow "\033[33m" +#define c_blue "\033[34m" +#define c_lila "\033[35m" +#define c_bold "\033[1m" #define c_end "\033[0m" /* path to data storage */