aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/main.c b/main.c
index 1ebe7ca..e0a2279 100644
--- a/main.c
+++ b/main.c
@@ -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);