aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c73
1 files changed, 29 insertions, 44 deletions
diff --git a/main.c b/main.c
index b88120b..c128475 100644
--- a/main.c
+++ b/main.c
@@ -41,7 +41,7 @@
#define FIRST_DAY_OF_WEEK 1 // (0-6, Sunday = 0).
#define NUM_WEEK_DAYS 7 // Just to avoid magic numbers.
-#define LOG_FILE_NAME "log.txt"
+#define HOME_PATH_ENV "HOME" // #if defined(_WIN64) #define HOME_PATH_ENV "USERPROFILE" #endif
#define APP_FOLDER_NAME ".task_time_tracker"
#define DB_FILE_NAME "database.bin"
#define AR_FILE_NAME "archive.csv"
@@ -220,7 +220,6 @@ char *sprint_time5_utf8(char *string, intmax_t time, int space) {
}
int64_t add_int64(int64_t x, int64_t y) {
-
if (y > 0 && x > INT64_MAX - y)
return INT64_MAX;
@@ -231,7 +230,6 @@ int64_t add_int64(int64_t x, int64_t y) {
}
int64_t sub_int64(int64_t x, int64_t y) {
-
if (y < 0 && x > INT64_MAX + y)
return INT64_MAX;
@@ -1021,6 +1019,15 @@ void draw_tui(database_st *db, layout_st *layout) {
mvaddstr(y, x, string_buffer);
}
+static inline void *mem_alloc(size_t mem_size, const char *error_tag) {
+ void *mem_pointer = malloc(mem_size);
+ if(mem_pointer == NULL && mem_size > 0) {
+ fprintf(stderr, "Failed to allocate memory (%s): %s.\n", (error_tag == NULL ? "undefined" : error_tag), strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ return mem_pointer;
+}
+
void free_memory() {
reset_database(&database);
reset_database(&archive);
@@ -1033,55 +1040,30 @@ void free_memory() {
bool initialize_app_folder() {
size_t temp_size;
- char* home_path = getenv("HOME");
-
-#if defined(_WIN64)
- home_path = getenv("USERPROFILE");
-#endif
-
- if (home_path != NULL)
- {
- temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'.
- app_folder = malloc(temp_size);
- if (app_folder == NULL && temp_size > 0) {
- fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno));
- return false;
- }
- snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME);
-
- // Create app folder.
- 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;
- }
+
+ char *home_path = getenv(HOME_PATH_ENV);
+ if (home_path == NULL) {
+ home_path = ".";
}
- else {
- temp_size = 3;
- app_folder = malloc(temp_size);
- if (app_folder == NULL && temp_size > 0) {
- fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno));
- return false;
- }
- snprintf(app_folder, temp_size, "./");
+ temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'.
+ app_folder = mem_alloc(temp_size, "app folder");
+ snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME);
+
+ // Create app folder.
+ 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;
}
// Set database file path.
temp_size = strlen(app_folder) + 1 + strlen(DB_FILE_NAME) + 1; // Add space for folder separator and '\0'.
- db_file_path = malloc(temp_size);
- if (db_file_path == NULL && temp_size > 0) {
- fprintf(stderr, "Failed to allocate memory for database file path: %s.\n", strerror(errno));
- return false;
- }
+ db_file_path = mem_alloc(temp_size, "database file path");
snprintf(db_file_path, temp_size, "%s/%s", app_folder, DB_FILE_NAME);
// Set archive file path.
temp_size = strlen(app_folder) + 1 + strlen(AR_FILE_NAME) + 1; // Add space for folder separator and '\0'.
- ar_file_path = malloc(temp_size);
- if (ar_file_path == NULL && temp_size > 0) {
- fprintf(stderr, "Failed to allocate memory for archive file path: %s.\n", strerror(errno));
- return false;
- }
+ ar_file_path = mem_alloc(temp_size, "archive file path");
snprintf(ar_file_path, temp_size, "%s/%s", app_folder, AR_FILE_NAME);
return true;
@@ -1092,7 +1074,10 @@ void exit_gracefully(int signal) {
ungetch('q');
}
-void static read_input_to_string_buffer_with_space(int row, int column, int length, int space) {
+void read_input_to_string_buffer_with_space(int row, int column, int length, int space) {
+ assert(length < string_buffer_size);
+ assert(space < string_buffer_size);
+
snprintf(string_buffer, string_buffer_size, "%*s", space, "");
attron(A_UNDERLINE);
mvaddstr(row, column, string_buffer);