hirc

IRC client
Log | Files | Refs

commit ee0bc80c0d7b302d809c5b61f422e901909b8528
parent a23767e60be2eaeea473fe26a9d63961251b0c0d
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 14 Jul 2022 17:38:10 +0100

smprintf function for convenient malloc/snprintf

Diffstat:
Msrc/commands.c | 23++++++++---------------
Msrc/complete.c | 20+++++++-------------
Msrc/hirc.h | 1+
Msrc/hist.c | 3+--
Msrc/serv.c | 6++----
Msrc/str.c | 13+++++++++++++
Msrc/ui.c | 3+--
7 files changed, 33 insertions(+), 36 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -721,8 +721,7 @@ command_format) { } len = strlen(str) + CONSTLEN("format.") + 1; - newstr = emalloc(len); - snprintf(newstr, len, "format.%s", str); + newstr = smprintf(len, "format.%s", str); command_set(server, channel, newstr); pfree(&newstr); } @@ -797,12 +796,10 @@ command_server) { hist_format(selected.history, Activity_none, HIST_UI, "SELF_AUTOCMDS_END %s :End of autocmds for %s", nserver->name, nserver->name); } else { - if (*arg == '/') { + if (*arg == '/') cmd = arg; - } else { - cmd = emalloc(strlen(arg) + 2); - snprintf(cmd, strlen(arg) + 2, "/%s", arg); - } + else + cmd = smprintf(strlen(arg) + 2, "/%s", arg); serv_auto_add(nserver, cmd); } @@ -1651,8 +1648,7 @@ idrange: format = strdup(command_optarg); } else { len = strlen(command_optarg) + 8; - format = emalloc(len); - snprintf(format, len, "format.%s", command_optarg); + format = smprintf(len, "format.%s", command_optarg); } if (!config_gets(format)) { @@ -1920,8 +1916,7 @@ alias_add(char *alias, char *cmd) { return -1; if (*alias != '/') { - tmp = emalloc(strlen(alias) + 2); - snprintf(tmp, strlen(alias) + 2, "/%s", alias); + tmp = smprintf(strlen(alias) + 2, "/%s", alias); } for (p = aliases; p; p = p->next) @@ -1935,8 +1930,7 @@ alias_add(char *alias, char *cmd) { p->alias = estrdup(alias); if (*cmd != '/') { - tmp = emalloc(strlen(cmd) + 2); - snprintf(tmp, strlen(cmd) + 2, "/%s", cmd); + tmp = smprintf(strlen(cmd) + 2, "/%s", cmd); p->cmd = tmp; } else p->cmd = estrdup(cmd); p->prev = NULL; @@ -1956,8 +1950,7 @@ alias_remove(char *alias) { if (!alias) return -1; if (*alias != '/') { - tmp = emalloc(strlen(alias) + 2); - snprintf(tmp, strlen(alias) + 2, "/%s", alias); + tmp = smprintf(strlen(alias) + 2, "/%s", alias); alias = tmp; /* tmp is guaranteed NULL or freeable */ }; diff --git a/src/complete.c b/src/complete.c @@ -78,8 +78,7 @@ complete_cmds(char *str, size_t len, char **ret, int *fullcomplete) { for (i = 0; commands[i].name; i++) if (strncmp(commands[i].name, str, len) == 0) complete_add(ret, commands[i].name, fullcomplete); - tmp = emalloc(len + 2); - snprintf(tmp, len + 2, "/%s", str); + tmp = smprintf(len + 2, "/%s", str); for (p = aliases; p; p = p->next) if (strncmp(p->alias, tmp, len + 1) == 0) complete_add(ret, p->alias + 1, fullcomplete); @@ -236,9 +235,7 @@ getcmd: pfree(&stem); if (found) { - len = strlen(found) + 2; - p = emalloc(len); - snprintf(p, len, "/%s", found); + p = smprintf(strlen(found) + 2, "/%s", found); pfree(&found); found = p; @@ -268,11 +265,10 @@ getcmd: wstem = toks[1]; p = wctos(wstem); - if (type == 3) { - len = strlen(p) + CONSTLEN("format.") + 1; - stem = emalloc(len); - snprintf(stem, len, "format.%s", p); - } else stem = p; + if (type == 3) + stem = smprintf(strlen(p) + CONSTLEN("format.") + 1, "format.%s", p); + else + stem = p; len = strlen(stem); if (type == 1) @@ -311,9 +307,7 @@ getcmd: if (found) { if (ctok == 0 && fullcomplete) { hchar = config_gets("completion.hchar"); - len = strlen(found) + strlen(hchar) + 1; - p = emalloc(len); - snprintf(p, len, "%s%s", found, hchar); + p = smprintf(strlen(found) + strlen(hchar) + 1, "%s%s", found, hchar); pfree(&found); found = p; } diff --git a/src/hirc.h b/src/hirc.h @@ -64,6 +64,7 @@ char * struntil(char *str, char until); int strisnum(char *str, int allowneg); char * strntok(char *str, char *sep, int n); char * strrdate(time_t secs); +char * smprintf(size_t len, char *fmt, ...); /* mem.c */ void pfree_(void **ptr); diff --git a/src/hist.c b/src/hist.c @@ -421,8 +421,7 @@ hist_loadlog(struct HistInfo *hist, char *server, char *channel) { len += strlen(tok[6]) + 1; if (*tok[7] != ' ') len += strlen(tok[7]) + 1; - prefix = emalloc(len); - snprintf(prefix, len, "%s%s%s%s%s", + prefix = smprintf(len, "%s%s%s%s%s", tok[5] ? tok[5] : "", tok[6] ? "!" : "", tok[6] ? tok[6] : "", tok[7] ? "@" : "", tok[7] ? tok[7] : ""); diff --git a/src/serv.c b/src/serv.c @@ -375,8 +375,7 @@ serv_read(struct Server *sp) { case -1: err = (char *)tls_error(sp->tls_ctx); len = CONSTLEN("tls_read(): ") + strlen(err) + 1; - reason = emalloc(len); - snprintf(reason, len, "tls_read(): %s", err); + reason = smprintf(len, "tls_read(): %s", err); /* fallthrough */ case 0: serv_disconnect(sp, 1, "EOF"); @@ -398,8 +397,7 @@ serv_read(struct Server *sp) { case -1: err = estrdup(strerror(errno)); len = CONSTLEN("read(): ") + strlen(err) + 1; - reason = emalloc(len); - snprintf(reason, len, "read(): %s", err); + reason = smprintf(len, "read(): %s", err); pfree(&err); /* fallthrough */ case 0: diff --git a/src/str.c b/src/str.c @@ -182,3 +182,16 @@ strrdate(time_t secs) { return ret; } + +char * +smprintf(size_t len, char *fmt, ...) { + char *ret; + va_list ap; + + ret = emalloc(len); + va_start(ap, fmt); + vsnprintf(ret, len, fmt, ap); + va_end(ap); + + return ret; +} diff --git a/src/ui.c b/src/ui.c @@ -974,8 +974,7 @@ ui_bind(char *binding, char *cmd) { p->binding = estrdup(b); p->wbinding = stowc(p->binding); if (*cmd != '/') { - tmp = emalloc(strlen(cmd) + 2); - snprintf(tmp, strlen(cmd) + 2, "/%s", cmd); + tmp = smprintf(strlen(cmd) + 2, "/%s", cmd); p->cmd = tmp; } else { p->cmd = estrdup(cmd);