From f153e712c52cf6d1922915fc735ba290dcf9bae1 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 17 Jul 2013 18:47:23 -0300 Subject: [PATCH] Really set the friend alias. Set the friend alias, make it more robust to fail to create a friend and allow it to be destroyed. --- include/sgp/friend.h | 2 ++ src/friend.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/sgp/friend.h b/include/sgp/friend.h index 7087f48..91965f9 100644 --- a/include/sgp/friend.h +++ b/include/sgp/friend.h @@ -24,6 +24,8 @@ struct sgp_friend; struct sgp_friend * sgp_friend_new(char *); +void sgp_friend_destroy(struct sgp_friend *); +char * sgp_friend_get_alias(struct sgp_friend *); struct sgp_channel * sgp_friend_get_channel(struct sgp_friend *); diff --git a/src/friend.c b/src/friend.c index db5c457..a8c1fc6 100644 --- a/src/friend.c +++ b/src/friend.c @@ -21,6 +21,7 @@ #include struct sgp_friend { + char *alias; struct sgp_channel *channel; }; @@ -28,8 +29,27 @@ struct sgp_friend * sgp_friend_new(char *alias) { struct sgp_friend *friend; friend = malloc(sizeof(*friend)); + if (!friend) + return NULL; + friend->alias = strdup(alias); + if (!friend->alias) + goto out; friend->channel = NULL; return friend; +out: + free(friend); + return NULL; +} + +void sgp_friend_destroy(struct sgp_friend *friend) +{ + free(friend->alias); + free(friend); +} + +char * sgp_friend_get_alias(struct sgp_friend *friend) +{ + return friend->alias; } /* -- 2.20.1