diff --git a/makefile b/makefile index 24aee5e..c494dd2 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,14 @@ -.PHONY: all debug -all: src/wta.c - gcc -o build/wta -O2 -Wall -Wextra -pedantic -Werror src/wta.c +.PHONY: debug release run -debug: src/wta.c - gcc -o build/wta -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/wta.c +release: src/*.c + gcc -o build/wte -O2 -Wall -Wextra -pedantic -Werror src/*.c + +run: release + ./build/wte + +debug: src/*.c + gcc -o build/wte -ggdb3 -DDEBUG -Wall -Wextra -pedantic -Werror src/*.c + +clean: + rm -f build/* diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..e67beb3 --- /dev/null +++ b/src/file.c @@ -0,0 +1,61 @@ +#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) { + 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); + } + 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); + if (0 == ret) { + info("%s", "removed data directory"); + } else { + error("%s", "failed to remove data directory"); + } + 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; +} diff --git a/src/wta.c b/src/wta.c deleted file mode 100644 index f8b643a..0000000 --- a/src/wta.c +++ /dev/null @@ -1,4 +0,0 @@ -int main() -{ - return 0; -} diff --git a/src/wte.c b/src/wte.c new file mode 100644 index 0000000..50e6fa5 --- /dev/null +++ b/src/wte.c @@ -0,0 +1,50 @@ +#include +#include +#include "wte.h" + +static size_t get_entry_count() +{ + size_t count = 0; + count++; + return count; +} + +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; +} + +int main(const int argc, const char **argv) +{ + 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); + } + goto exit_main; + } + + if (make_suggestion()) { + ret = 0; + } + +exit_main: + return ret; +} diff --git a/src/wte.h b/src/wte.h new file mode 100644 index 0000000..b9d9823 --- /dev/null +++ b/src/wte.h @@ -0,0 +1,39 @@ +#include +#define error(format, ...) do {\ + 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(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 + +/* 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 */ +#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();