From 08117318a82ac2d57d8cf359d61e57ac38aa0213 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 31 Mar 2024 03:53:08 +0400 Subject: [PATCH] Check for errors. Store an updated addressbook. --- src/main.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 6fce236..42dce4f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ -#include +#include +#include #include #include #include -#include #include "addrbook.h" #include "audio.h" @@ -47,12 +47,12 @@ int main(int argc, const char **argv) { struct options opts = {0}; addrbook_t ab = {0}; + bool is_ab_changed = false; net_t n = {0}; crypt_session_t s = {0}; crypt_key_t ok = {0}, rk = {0}; - result = parse_argv(argc, argv, &opts); - switch (result) { + switch (parse_argv(argc, argv, &opts)) { case -1: usage(); return 1; case -2: version(); return 0; case -3: usage(); return 0; @@ -88,6 +88,7 @@ int main(int argc, const char **argv) { FILE *skey_file = fopen("tat_own_sec_key", "r"); if (pkey_file == NULL || skey_file == NULL) { if ((result = crypt_key_gen(&ok)) == -1) { + fprintf(stderr, "Failed to generate the keys.\n"); result = 1; goto cleanup; } @@ -100,7 +101,15 @@ int main(int argc, const char **argv) { pkey_file = fopen("tat_own_pub_key", "w+"); skey_file = fopen("tat_own_sec_key", "w+"); - crypt_store_key(&ok, pkey_file, skey_file); + if (pkey_file == NULL || skey_file == NULL) { + fprintf(stderr, "Cannot open tat_own_*_key files to write: %s\n", strerror(errno)); + return 1; + } + + if (crypt_store_key(&ok, pkey_file, skey_file) == -1) { + fprintf(stderr, "Failed to save the keys.\n"); + return 1; + } fclose(pkey_file); fclose(skey_file); @@ -108,7 +117,7 @@ int main(int argc, const char **argv) { printf("The new cryptographical keys was written.\n"); } else { if (crypt_load_key(&ok, pkey_file, skey_file) == -1) { - fprintf(stderr, "Cannot load the keys."); + fprintf(stderr, "Cannot load the keys.\n"); result = 1; goto cleanup; } @@ -161,6 +170,15 @@ int main(int argc, const char **argv) { cleanup: + if (is_ab_changed) { + addrbook_file = fopen((opts.addressbook_path == NULL) ? "addressbook" : opts.addressbook_path, "w+"); + if (addrbook_file != NULL) { + if (addrbook_store(&ab, addrbook_file) == -1) + fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno)); + fclose(addrbook_file); + } + } + addrbook_destroy(&ab); audio_destroy(&aud); net_destroy(&n); @@ -168,8 +186,8 @@ cleanup: crypt_key_destroy(&ok); crypt_key_destroy(&rk); - if (audio_terminate_soundsystem() == -1) - return 1; + // if (audio_terminate_soundsystem() == -1) + // return 1; return result; }