diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 33 | ||||
-rw-r--r-- | src/client.h | 8 | ||||
-rw-r--r-- | src/client_main.c | 6 |
3 files changed, 33 insertions, 14 deletions
diff --git a/src/client.c b/src/client.c index 7c47ae6..739c612 100644 --- a/src/client.c +++ b/src/client.c @@ -12,18 +12,39 @@ #include <unistd.h> -int client_create(struct client *client, const struct settings *settings) +struct client { + int fd; +}; + +int client_create(struct client **_client, const struct settings *settings) { - client->fd = net_connect(settings->host, settings->port); - if (client->fd < 0) - return client->fd; + struct client *client; + int ret = 0; + + *_client = malloc(sizeof(struct client)); + if (!*_client) { + log_errno("malloc"); + return -1; + } + client = *_client; + + ret = net_connect(settings->host, settings->port); + if (ret < 0) + goto free; + client->fd = ret; + + return ret; - return 0; +free: + free(client); + + return ret; } -void client_destroy(const struct client *client) +void client_destroy(struct client *client) { log_errno_if(close(client->fd), "close"); + free(client); } int client_main(const struct client *client, int argc, char *argv[]) diff --git a/src/client.h b/src/client.h index 544501e..141c6be 100644 --- a/src/client.h +++ b/src/client.h @@ -13,12 +13,10 @@ struct settings { const char *port; }; -struct client { - int fd; -}; +struct client; -int client_create(struct client *, const struct settings *); -void client_destroy(const struct client *); +int client_create(struct client **, const struct settings *); +void client_destroy(struct client *); int client_main(const struct client *, int argc, char *argv[]); diff --git a/src/client_main.c b/src/client_main.c index b481e44..dd6c8c5 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -71,7 +71,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) int main(int argc, char *argv[]) { struct settings settings; - struct client client; + struct client *client; int ret = 0; ret = parse_settings(&settings, argc, argv); @@ -82,12 +82,12 @@ int main(int argc, char *argv[]) if (ret < 0) return ret; - ret = client_main(&client, argc - optind, argv + optind); + ret = client_main(client, argc - optind, argv + optind); if (ret < 0) goto destroy_client; destroy_client: - client_destroy(&client); + client_destroy(client); return ret; } |