commit 68e7cd5a064c2d65d0e73b050050b32c9bf033ee
parent 8ab52e9749e6577dbe372eeddfa4a0ca85d12bb7
Author: hhvn <dev@hhvn.uk>
Date: Wed, 12 Jan 2022 20:59:08 +0000
s/nick.c s/hist.c s/hirc.h: prefix and elements as own memory
Diffstat:
3 files changed, 21 insertions(+), 38 deletions(-)
diff --git a/src/hirc.h b/src/hirc.h
@@ -75,7 +75,6 @@ struct Nick * nick_dup(struct Nick *nick, struct Server *server);
int nick_isself(struct Nick *nick);
int nick_isself_server(struct Nick *nick, struct Server *server);
int nick_remove(struct Nick **head, char *nick);
-char * nick_strprefix(struct Nick *nick);
void nick_sort(struct Nick **head, struct Server *server);
/* hist.c */
diff --git a/src/hist.c b/src/hist.c
@@ -199,7 +199,7 @@ hist_log(char *msg, struct Nick *from, time_t timestamp, struct Server *server)
if (*msg == ':' && strchr(msg, ' '))
msg = strchr(msg, ' ') + 1;
- if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, nick_strprefix(from), msg) < 0) {
+ if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, from->prefix, msg) < 0) {
/* Can't write, try to open the file */
snprintf(filename, sizeof(filename), "%s/%s.log", homepath(logdir), server->name);
ret = open(filename, O_CREAT|O_APPEND|O_WRONLY);
@@ -219,7 +219,7 @@ hist_log(char *msg, struct Nick *from, time_t timestamp, struct Server *server)
} else return 1;
/* retry */
- if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, nick_strprefix(from), msg) < 0) {
+ if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, from->prefix, msg) < 0) {
ui_error("Failed to write to log of server '%s': %s", server->name, strerror(errno));
return -1;
}
diff --git a/src/nick.c b/src/nick.c
@@ -67,34 +67,42 @@ nick_getcolour(struct Nick *nick) {
void
prefix_tokenize(char *prefix, char **nick, char **ident, char **host) {
enum { ISNICK, ISIDENT, ISHOST } segment = ISNICK;
+ char *p = tstrdup(prefix);
- if (*prefix == ':')
- prefix++;
+ if (*p == ':')
+ p++;
- if (nick) *nick = prefix;
+ if (nick) *nick = p;
if (ident) *ident = NULL;
if (host) *host = NULL;
- for (; prefix && *prefix && segment != ISHOST; prefix++) {
- if (segment == ISNICK && *prefix == '!') {
- *prefix = '\0';
+ for (; p && *p && segment != ISHOST; p++) {
+ if (segment == ISNICK && *p == '!') {
+ *p = '\0';
if (ident)
- *ident = prefix + 1;
+ *ident = p + 1;
segment = ISIDENT;
}
- if (segment == ISIDENT && *prefix == '@') {
- *prefix = '\0';
+ if (segment == ISIDENT && *p == '@') {
+ *p = '\0';
if (host)
- *host = prefix + 1;
+ *host = p + 1;
segment = ISHOST;
}
}
+
+ if (nick && *nick) *nick = strdup(*nick);
+ if (ident && *ident) *ident = strdup(*ident);
+ if (host && *host) *host = strdup(*host);
}
void
nick_free(struct Nick *nick) {
if (nick) {
free(nick->prefix);
+ free(nick->nick);
+ free(nick->ident);
+ free(nick->host);
free(nick);
}
}
@@ -172,12 +180,7 @@ nick_add(struct Nick **head, char *prefix, char priv, struct Server *server) {
struct Nick *
nick_dup(struct Nick *nick, struct Server *server) {
- /* Use strprefix to recreate the prefix.
- * This is an example of trying to be clever
- * backfiring - I should've just strdup'd
- * the nick, ident and host instead of
- * splitting prefix and pointing to it. */
- return nick_create(nick_strprefix(nick), nick->priv, server);
+ return nick_create(nick->prefix, nick->priv, server);
}
struct Nick *
@@ -217,25 +220,6 @@ nick_remove(struct Nick **head, char *nick) {
return 1;
}
-char *
-nick_strprefix(struct Nick *nick) {
- static char ret[1024];
-
- if (!nick)
- return NULL;
-
- if (nick->nick && nick->ident && nick->host)
- snprintf(ret, sizeof(ret), "%s!%s@%s", nick->nick, nick->ident, nick->host);
- else if (nick->nick && nick->ident)
- snprintf(ret, sizeof(ret), "%s!%s", nick->nick, nick->ident);
- else if (nick->nick)
- snprintf(ret, sizeof(ret), "%s", nick->nick);
- else
- snprintf(ret, sizeof(ret), "");
-
- return ret;
-}
-
static inline void
nick_swap(struct Nick **head, struct Nick *first, struct Nick *second) {
struct Nick *next[2];