1
0
Fork 0

Check for errors. Store an updated addressbook.

This commit is contained in:
Alexander Andreev 2024-03-31 03:53:08 +04:00
parent 4a03096436
commit 08117318a8
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
1 changed files with 26 additions and 8 deletions

View File

@ -1,8 +1,8 @@
#include <bits/endian.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#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;
}