commit f29201a64705b70e5320811c21db089ce988b726
parent 6acfc828bee2e43cb1500d03171aed9a2121d03b
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Mon, 31 May 2021 23:03:15 +0200
reuse expand_path and free mem
Diffstat:
3 files changed, 17 insertions(+), 18 deletions(-)
diff --git a/caldav.c b/caldav.c
@@ -84,6 +84,7 @@ char* read_tokenfile() {
char* tokenfile_path = expand_path(CONFIG.google_tokenfile);
token_file = fopen(tokenfile_path, "r");
+ free(tokenfile_path);
if (token_file == NULL) {
perror("Failed to open tokenfile");
@@ -127,6 +128,8 @@ char* read_tokenfile() {
void write_tokenfile() {
char* tokenfile_path = expand_path(CONFIG.google_tokenfile);
FILE* tokenfile = fopen(tokenfile_path, "wb");
+ free(tokenfile_path);
+
if (tokenfile == NULL) {
perror("Failed to open tokenfile");
} else {
@@ -187,6 +190,8 @@ void get_access_token(char* code, char* verifier, bool refresh) {
tokenfile_path = expand_path(CONFIG.google_tokenfile);
tokenfile = fopen(tokenfile_path, "wb");
+ free(tokenfile_path);
+
if (tokenfile == NULL) {
perror("Failed to open tokenfile");
} else {
@@ -692,4 +697,5 @@ void caldav_sync(struct tm* date, WINDOW* header, WINDOW* cal, int pad_pos) {
wchgat(cal, 2, atrs | A_BOLD, 0, NULL);
prefresh(cal, pad_pos, 0, 1, ASIDE_WIDTH, LINES - 1, ASIDE_WIDTH + CAL_WIDTH);
}
+ free(rmt_desc);
}
diff --git a/diary.c b/diary.c
@@ -257,6 +257,7 @@ bool read_config(const char* file_path)
char key_buf[80];
char value_buf[80];
char line[256];
+ char* expaned_value;
FILE * pfile;
// read config file line by line
@@ -267,13 +268,9 @@ bool read_config(const char* file_path)
if (sscanf(line, "%s = %s", key_buf, value_buf) == 2) {
if (strcmp("dir", key_buf) == 0) {
- wordexp_t diary_dir_wordexp;
- if ( wordexp( value_buf, &diary_dir_wordexp, 0 ) == 0) {
- // set expanded diary directory path from config file
- CONFIG.dir = (char *) calloc(strlen(diary_dir_wordexp.we_wordv[0]) + 1, sizeof(char));
- strcpy(CONFIG.dir, diary_dir_wordexp.we_wordv[0]);
- }
- wordfree(&diary_dir_wordexp);
+ expaned_value = expand_path(value_buf);
+ strcpy(CONFIG.dir, expaned_value);
+ free(expaned_value);
} else if (strcmp("range", key_buf) == 0) {
CONFIG.range = atoi(value_buf);
} else if (strcmp("weekday", key_buf) == 0) {
@@ -285,13 +282,9 @@ bool read_config(const char* file_path)
CONFIG.editor = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.editor, value_buf);
} else if (strcmp("google_tokenfile", key_buf) == 0) {
- wordexp_t tokenfile_wordexp;
- if ( wordexp( value_buf, &tokenfile_wordexp, 0 ) == 0) {
- // set expanded tokenfile path from config file
- CONFIG.google_tokenfile = (char *) calloc(strlen(tokenfile_wordexp.we_wordv[0]) + 1, sizeof(char));
- strcpy(CONFIG.google_tokenfile, tokenfile_wordexp.we_wordv[0]);
- }
- wordfree(&tokenfile_wordexp);
+ expaned_value = expand_path(value_buf);
+ strcpy(CONFIG.google_tokenfile, expaned_value);
+ free(expaned_value);
} else if (strcmp("google_clientid", key_buf) == 0) {
CONFIG.google_clientid = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.google_clientid, value_buf);
diff --git a/utils.c b/utils.c
@@ -26,10 +26,10 @@ char* extract_json_value(char* json, char* key, bool quoted) {
char* unfold(const char* str) {
// work on a copy of the str
- char* res = (char *) malloc(strlen(str) * sizeof(char));
- strcpy(res, str);
+ char* strcp = (char *) malloc(strlen(str) * sizeof(char));
+ strcpy(strcp, str);
- res = strtok(res, "\n");
+ char* res = strtok(strcp, "\n");
char* buf = malloc(strlen(res));
strcpy(buf, res);
@@ -61,7 +61,7 @@ char* unfold(const char* str) {
}
regfree(&re);
- //free(buf);
+ free(strcp);
return buf;
}