commit 9392dc9bac45d517228ff8d806a36608aa7c1e7f
parent 49a79f1aa9abedc216e96b9147bca1ab44314595
Author: hhvn <dev@hhvn.uk>
Date: Wed, 13 Apr 2022 12:18:37 +0100
Use CONSTLEN() macro for literals
Diffstat:
6 files changed, 35 insertions(+), 33 deletions(-)
diff --git a/src/commands.c b/src/commands.c
@@ -1014,7 +1014,7 @@ command_format) {
return;
}
- len = strlen(str) + strlen("format.") + 1;
+ len = strlen(str) + CONSTLEN("format.") + 1;
newstr = emalloc(len);
snprintf(newstr, len, "format.%s", str);
command_set(server, channel, newstr);
@@ -1792,9 +1792,9 @@ command_dump) {
fprintf(file, "Configuration variables\n");
for (i = 0; config[i].name; i++) {
if (!config[i].isdef || def) {
- if (selected & opt_formats && strncmp(config[i].name, "format.", strlen("format.")) == 0) {
- fprintf(file, "/format %s %s\n", config[i].name + strlen("format."), config[i].str);
- } else if (selected & opt_config && strncmp(config[i].name, "format.", strlen("format.")) != 0) {
+ if (selected & opt_formats && strncmp(config[i].name, "format.", CONSTLEN("format.")) == 0) {
+ fprintf(file, "/format %s %s\n", config[i].name + CONSTLEN("format."), config[i].str);
+ } else if (selected & opt_config && strncmp(config[i].name, "format.", CONSTLEN("format.")) != 0) {
if (config[i].valtype == Val_string)
fprintf(file, "/set %s %s\n", config[i].name, config[i].str);
else if (config[i].valtype == Val_pair || config[i].valtype == Val_colourpair)
@@ -1935,7 +1935,7 @@ idrange:
ui_error("id out of range: %s", str);
return;
case opt_format:
- if (strncmp(command_optarg, "format.", 7) == 0) {
+ if (strncmp(command_optarg, "format.", CONSTLEN("format.")) == 0) {
format = strdup(command_optarg);
} else {
len = strlen(command_optarg) + 8;
@@ -1944,7 +1944,7 @@ idrange:
}
if (!config_gets(format)) {
- ui_error("no such format: %s", format + 7);
+ ui_error("no such format: %s", format + CONSTLEN("format"));
free(format);
return;
}
diff --git a/src/complete.c b/src/complete.c
@@ -243,7 +243,7 @@ getcmd:
p = wctos(wstem);
if (type == 3) {
- len = strlen(p) + 8; /* format.\0 */
+ len = strlen(p) + CONSTLEN("format.") + 1;
stem = emalloc(len);
snprintf(stem, len, "format.%s", p);
} else stem = p;
@@ -256,7 +256,7 @@ getcmd:
if (found) {
if (type == 3)
- p = found + 7; /* format. */
+ p = found + CONSTLEN("format.");
else
p = found;
wp = stowc(p);
diff --git a/src/format.c b/src/format.c
@@ -270,7 +270,7 @@ format_get(struct History *hist) {
cmd = "MODE-NICK";
} else if (strcmp_n(cmd, "PRIVMSG") == 0) {
/* ascii 1 is ^A */
- if (*p2 == 1 && strncmp(p2 + 1, "ACTION", strlen("ACTION")) == 0)
+ if (*p2 == 1 && strncmp(p2 + 1, "ACTION", CONSTLEN("ACTION")) == 0)
cmd = "PRIVMSG-ACTION";
else if (*p2 == 1)
cmd = "PRIVMSG-CTCP";
@@ -433,7 +433,7 @@ outcont:
if (!*p && hist) {
pn = strtol(content, NULL, 10) - 1;
if (pn >= 0 && param_len(params) > pn) {
- if (**(params+pn) == 1 && strncmp((*(params+pn))+1, "ACTION", strlen("ACTION")) == 0 && strchr(*(params+pn), ' '))
+ if (**(params+pn) == 1 && strncmp((*(params+pn))+1, "ACTION", CONSTLEN("ACTION")) == 0 && strchr(*(params+pn), ' '))
rc += snprintf(&ret[rc], rs - rc, "%s", struntil(strchr(*(params+pn), ' ') + 1, 1));
else if (**(params+pn) == 1)
rc += snprintf(&ret[rc], rs - rc, "%s", struntil((*(params+pn)) + 1, 1));
@@ -448,7 +448,7 @@ outcont:
pn = strtol(content, NULL, 10) - 1;
if (pn >= 0 && param_len(params) > pn) {
for (; *(params+pn) != NULL; pn++) {
- if (**(params+pn) == 1 && strncmp((*(params+pn))+1, "ACTION", strlen("ACTION")) == 0 && strchr(*(params+pn), ' ')) {
+ if (**(params+pn) == 1 && strncmp((*(params+pn))+1, "ACTION", CONSTLEN("ACTION")) == 0 && strchr(*(params+pn), ' ')) {
rc += snprintf(&ret[rc], rs - rc, "%s%s",
struntil(strchr(*(params+pn), ' ') + 1, 1),
*(params+pn+1) ? " " : "");
@@ -571,29 +571,29 @@ outcont:
*
* These styling formatters are quite ugly and repetitive.
* %{nick:...} was implemented first, and has the most (all of them :)) comments */
- if (strncmp(content, "pad:", strlen("pad:")) == 0 && strchr(content, ',')) {
- pn = strtol(content + strlen("pad:"), NULL, 10);
- content = estrdup(format_get_content(strchr(format+2+strlen("pad:"), ',') + 1, 1));
+ if (strncmp(content, "pad:", CONSTLEN("pad:")) == 0 && strchr(content, ',')) {
+ pn = strtol(content + CONSTLEN("pad:"), NULL, 10);
+ content = estrdup(format_get_content(strchr(format+2+CONSTLEN("pad:"), ',') + 1, 1));
save = ret;
ret = NULL;
format_(NULL, content, hist, 1);
rc += snprintf(&save[rc], rs - rc, "%1$*2$s", ret, pn);
pfree(&ret);
ret = save;
- format = strchr(format+2+strlen("pad:"), ',') + strlen(content) + 2;
+ format = strchr(format+2+CONSTLEN("pad:"), ',') + strlen(content) + 2;
pfree(&content);
continue;
}
- if (strncmp(content, "rdate:", strlen("rdate:")) == 0) {
- content = estrdup(format_get_content(format+2+strlen("rdate:"), 1));
+ if (strncmp(content, "rdate:", CONSTLEN("rdate:")) == 0) {
+ content = estrdup(format_get_content(format+2+CONSTLEN("rdate:"), 1));
save = ret;
ret = NULL;
format_(NULL, content, hist, 1);
pn = strtoll(ret, NULL, 10);
rc += snprintf(&save[rc], rs - rc, "%s", strrdate((time_t)pn));
- format += 3 + strlen("rdate:") + strlen(content);
+ format += 3 + CONSTLEN("rdate:") + strlen(content);
pfree(&ret);
ret = save;
@@ -601,16 +601,16 @@ outcont:
continue;
}
- if (strncmp(content, "time:", strlen("time:")) == 0 && strchr(content, ',')) {
- content = estrdup(format_get_content(strchr(format+2+strlen("time:"), ',') + 1, 1));
+ if (strncmp(content, "time:", CONSTLEN("time:")) == 0 && strchr(content, ',')) {
+ content = estrdup(format_get_content(strchr(format+2+CONSTLEN("time:"), ',') + 1, 1));
save = ret;
ret = NULL;
format_(NULL, content, hist, 1);
pn = strtoll(ret, NULL, 10);
- p = struntil(format+2+strlen("time:"), ',');
+ p = struntil(format+2+CONSTLEN("time:"), ',');
rc += strftime(&save[rc], rs - rc, p, localtime((time_t *)&pn));
- format = strchr(format+2+strlen("time:"), ',') + strlen(content) + 2;
+ format = strchr(format+2+CONSTLEN("time:"), ',') + strlen(content) + 2;
pfree(&ret);
ret = save;
@@ -623,13 +623,13 @@ outcont:
p = strchr(content, ',');
if (p)
p2 = strchr(p + 1, ',');
- if (strncmp(content, "split:", strlen("split:")) == 0 && p2 - p == 2) {
- pn = strtol(content + strlen("split:"), NULL, 10);
+ if (strncmp(content, "split:", CONSTLEN("split:")) == 0 && p2 - p == 2) {
+ pn = strtol(content + CONSTLEN("split:"), NULL, 10);
chs[0] = *(strchr(content, ',') + 1);
chs[1] = '\0';
content = estrdup(format_get_content(
strchr(
- strchr(format+2+strlen("split:"), ',') + 1,
+ strchr(format+2+CONSTLEN("split:"), ',') + 1,
',') + 1,
1));
save = ret;
@@ -637,7 +637,7 @@ outcont:
format_(NULL, content, hist, 1);
rc += snprintf(&save[rc], rs - rc, "%s", strntok(ret, chs, pn));
format = strchr(
- strchr(format+2+strlen("split:"), ',') + 1,
+ strchr(format+2+CONSTLEN("split:"), ',') + 1,
',') + strlen(content) + 2;
pfree(&ret);
@@ -646,14 +646,14 @@ outcont:
continue;
}
- if (hist && !recursive && strncmp(content, "nick:", strlen("nick:")) == 0) {
- content = estrdup(format_get_content(format+2+strlen("nick:"), 1));
+ if (hist && !recursive && strncmp(content, "nick:", CONSTLEN("nick:")) == 0) {
+ content = estrdup(format_get_content(format+2+CONSTLEN("nick:"), 1));
save = ret;
ret = NULL;
format_(NULL, content, hist, 1);
nick = nick_create(ret, ' ', hist->origin ? hist->origin->server : NULL);
rc += snprintf(&save[rc], rs - rc, "%c%02d", 3 /* ^C */, nick_getcolour(nick));
- format += 3 + strlen("nick:") + strlen(content);
+ format += 3 + CONSTLEN("nick:") + strlen(content);
pfree(&ret);
ret = save;
diff --git a/src/hirc.h b/src/hirc.h
@@ -28,6 +28,8 @@
#define HIST_MAX 8192
/* real maximum = HIST_MAX * (channels + servers + queries) */
#define strcmp_n(s1, s2) (s1 == s2 ? 0 : (s1 ? s2 ? strcmp(s1, s2) : -1 : -1))
+#define CONSTLEN(str) ((size_t)((sizeof(str) - sizeof(str[0])) / sizeof(str[0])))
+ /* compile-time char/wchar_t literals */
/* strlcpy/wcslcpy.c */
#ifdef HIRC_STRLCPY
diff --git a/src/main.c b/src/main.c
@@ -132,7 +132,7 @@ ircread(struct Server *sp) {
switch (ret = tls_read(sp->tls_ctx, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) {
case -1:
err = (char *)tls_error(sp->tls_ctx);
- len = strlen("tls_read(): ") + strlen(err) + 1;
+ len = CONSTLEN("tls_read(): ") + strlen(err) + 1;
reason = emalloc(len);
snprintf(reason, len, "tls_read(): %s", err);
/* fallthrough */
@@ -155,7 +155,7 @@ ircread(struct Server *sp) {
switch (ret = read(sp->rfd, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) {
case -1:
err = estrdup(strerror(errno));
- len = strlen("read(): ") + strlen(err) + 1;
+ len = CONSTLEN("read(): ") + strlen(err) + 1;
reason = emalloc(len);
snprintf(reason, len, "read(): %s", err);
pfree(&err);
diff --git a/src/serv.c b/src/serv.c
@@ -486,8 +486,8 @@ serv_auto_haschannel(struct Server *server, char *chan) {
return 0;
for (p = server->autocmds; *p; p++)
- if (strncmp(*p, "/join ", strlen("/join ")) == 0 &&
- strcmp((*p) + strlen("/join "), chan) == 0)
+ if (strncmp(*p, "/join ", CONSTLEN("/join ")) == 0 &&
+ strcmp((*p) + CONSTLEN("/join "), chan) == 0)
return 1;
return 0;
}