From: Sebastian Harl Date: Fri, 30 Jan 2015 11:01:48 +0000 (+0100) Subject: sysdb: Add and use sdb_input_reset(). X-Git-Tag: sysdb-0.7.0~33 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=8efdf07be8a00287bc3d929cde8aa7562b4910be;p=sysdb.git sysdb: Add and use sdb_input_reset(). This simplifies cleanup. --- diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index a1e7b71..0b0dbf7 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -237,6 +237,24 @@ sdb_input_init(sdb_input_t *input) return 0; } /* sdb_input_init */ +void +sdb_input_reset(sdb_input_t *input) +{ + sdb_input_t reset = SDB_INPUT_INIT; + + if (! input) + return; + + if (input->client) + sdb_client_destroy(input->client); + if (input->user) + free(input->user); + if (input->input) + sdb_strbuf_destroy(input->input); + + *input = reset; +} /* sdb_input_reset */ + int sdb_input_mainloop(void) { diff --git a/src/tools/sysdb/input.h b/src/tools/sysdb/input.h index ba2b32d..6002c1f 100644 --- a/src/tools/sysdb/input.h +++ b/src/tools/sysdb/input.h @@ -61,6 +61,14 @@ extern sdb_input_t *sysdb_input; int sdb_input_init(sdb_input_t *input); +/* + * sdb_input_reset: + * Reset the input handler and free all dynamically allocated memory. The + * input handler object itself will not be freed. + */ +void +sdb_input_reset(sdb_input_t *input); + /* * sdb_input_mainloop: * Wait for and handle all user and server input until end-of-file is read diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c index b22e11f..91a6723 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -255,21 +255,19 @@ main(int argc, char **argv) input.client = sdb_client_create(host); if (! input.client) { sdb_log(SDB_LOG_ERR, "Failed to create client object"); - free(input.user); + sdb_input_reset(&input); exit(1); } if (sdb_client_connect(input.client, input.user)) { sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd"); - sdb_client_destroy(input.client); - free(input.user); + sdb_input_reset(&input); exit(1); } if (commands) { int status = execute_commands(input.client, commands); sdb_llist_destroy(commands); - sdb_client_destroy(input.client); - free(input.user); + sdb_input_reset(&input); if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA)) exit(1); exit(0); @@ -295,7 +293,6 @@ main(int argc, char **argv) hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf))); } } - free(input.user); input.input = sdb_strbuf_create(2048); sdb_input_init(&input); @@ -316,8 +313,7 @@ main(int argc, char **argv) } } - sdb_client_destroy(input.client); - sdb_strbuf_destroy(input.input); + sdb_input_reset(&input); ERR_free_strings(); return 0;