diff --git a/C/irc.c b/C/irc.c index 9e8f604..8068476 100644 --- a/C/irc.c +++ b/C/irc.c @@ -1,6 +1,6 @@ #include "irc.h" -unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){ +int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){ struct hostent *hp = gethostbyname(ip); if (!hp) return IRCC_ERROR; @@ -27,13 +27,11 @@ unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int p return IRCC_SUCCESS; } -unsigned int IRCC_register(IRCC_client *irc, const char *nickname){ +int IRCC_register(IRCC_client *irc, const char *nickname){ size_t size = strlen("NICK %s\r\nUSER %s 0 localhost :%s \r\n") + (strlen(nickname) * 3); char *tmp = (char *)malloc(size); - if (tmp == NULL){ - fprintf(stderr, "malloc returned NULL (IRCC_register)\n"); - exit(1); - } + if (tmp == NULL) + return IRCC_ALLOC; sleep(2); snprintf(tmp, size, "NICK %s\r\nUSER %s 0 localhost :%s\r\n", nickname, nickname, nickname); @@ -62,7 +60,7 @@ void IRCC_parse(char *tmp, IRCC_client *irc){ } } -unsigned int IRCC_recv(IRCC_client *irc){ +int IRCC_recv(IRCC_client *irc){ memset(irc->raw, '\0', IRCC_MSG_MAX); irc->channel = NULL; irc->msg = NULL; @@ -121,28 +119,26 @@ unsigned int IRCC_recv(IRCC_client *irc){ return IRCC_SUCCESS; } -void IRCC_send(IRCC_client *irc, const char *msg, const char *channel){ +int IRCC_send(IRCC_client *irc, const char *msg, const char *channel){ size_t size = strlen("PRIVMSG :\r\n") + strlen(channel) + strlen(msg); char *tmp = (char *)malloc(size + 1); - if (tmp == NULL){ - fprintf(stderr, "malloc returned NULL (IRCC_send)\n"); - exit(1); - } + if (tmp == NULL) + return IRCC_ALLOC; snprintf(tmp, size, "PRIVMSG %s :%s\r\n", channel, msg); - send(irc->socket, tmp, strlen(tmp), 0); + int ret = send(irc->socket, tmp, strlen(tmp), 0); memset(tmp, 0, size); free(tmp); + + return ret; } -unsigned int IRCC_init(IRCC_client *irc) { +int IRCC_init(IRCC_client *irc) { irc->raw = (char *)malloc(IRCC_MSG_MAX + 1); - if (irc->raw == NULL){ - fprintf(stderr, "malloc returned NULL (IRCC_init)\n"); - exit(1); - } + if (irc->raw == NULL) + return IRCC_ALLOC; irc->msg = irc->nick = irc->channel = NULL; return IRCC_SUCCESS; diff --git a/C/irc.h b/C/irc.h index bf4cb4c..451e45a 100644 --- a/C/irc.h +++ b/C/irc.h @@ -32,6 +32,7 @@ enum { IRCC_CONNECTED, IRCC_DISCONNECTED, IRCC_ERROR, + IRCC_ALLOC, IRCC_SUCCESS }; @@ -44,9 +45,9 @@ typedef struct { char *nick; } IRCC_client; -unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port); -unsigned int IRCC_register(IRCC_client *irc, const char *nickname); -unsigned int IRCC_recv(IRCC_client *irc); -void IRCC_send(IRCC_client *irc, const char *msg, const char *channel); -unsigned int IRCC_init(IRCC_client *irc); +int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port); +int IRCC_register(IRCC_client *irc, const char *nickname); +int IRCC_recv(IRCC_client *irc); +int IRCC_send(IRCC_client *irc, const char *msg, const char *channel); +int IRCC_init(IRCC_client *irc); void IRCC_close(IRCC_client *irc);