diff options
| author | dam <dam@gudinoff> | 2022-09-30 16:51:05 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-09-30 16:51:05 +0000 |
| commit | 4261d2ecc8bea77679ed3cfd67ee45bc302ed753 (patch) | |
| tree | 814a8a9a94f433d696e812c7b1919510a244c0b2 | |
| parent | 4e8e9182d8620a35bc9cae0052bbdeb173922a39 (diff) | |
| download | task-time-tracker-4261d2ecc8bea77679ed3cfd67ee45bc302ed753.tar.zst task-time-tracker-4261d2ecc8bea77679ed3cfd67ee45bc302ed753.zip | |
WIP: Store app data on ~/.task_time_tracker/ folder.
| -rw-r--r-- | main.c | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -13,6 +13,8 @@ #include <stddef.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> +#include <sys/types.h> #include <time.h> #define VERSION "1.0" // Use only 3 chars (to fit layouts). @@ -21,8 +23,9 @@ #define NUM_WEEK_DAYS 7 // Just to avoid magic numbers. #define LOG_FILE_NAME "log.txt" -#define DB_BIN_PATH_NAME "./database.bin" -#define AR_CSV_PATH_NAME "./archive.csv" +#define APP_FOLDER_NAME ".task_time_tracker" +#define DB_BIN_PATH_NAME APP_FOLDER_NAME "/database.bin" +#define AR_CSV_PATH_NAME APP_FOLDER_NAME "/archive.csv" typedef struct { int64_t times[NUM_WEEK_DAYS]; @@ -896,6 +899,7 @@ void draw_tui(database_st *db, layout_st *layout) { mvaddstr(y, x, string_buffer); } +char *app_folder = NULL; void free_memory() { reset_database(&database); @@ -903,12 +907,34 @@ void free_memory() { free(string_buffer); string_buffer = NULL; + + free(app_folder); + app_folder = NULL; } - +bool initialize_app_folder(const char *path) { + size_t path_length = strlen(path); + size_t app_folder_name_length = strlen(APP_FOLDER_NAME); + app_folder = malloc(path_length + app_folder_name_length + 2 + 1); // Add 2 for the '/'s and 1 for the NUL. + memcpy(app_folder, path, path_length); + app_folder[path_length] = '/'; + memcpy(app_folder + path_length + 1, APP_FOLDER_NAME, app_folder_name_length); + app_folder[path_length + 1 + app_folder_name_length] = '/'; + + mkdir(app_folder, 0740); + if (errno != 0 && errno != EEXIST) { + fprintf(stderr, "Failed to create app folder '%s': %s.\n", app_folder, strerror(errno)); + return false; + } + return true; +} int main(int argc, char *argv[]) { + if (initialize_app_folder(getenv("HOME")) == false) { + return EXIT_FAILURE; + } + db = &database; reset_database(&database); reset_database(&archive); |
