summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-21 00:06:29 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-21 00:06:29 +0200
commitae48dc3b3caeef7eaa4a079b11cdda988d9c1f0d (patch)
tree2b9ec4898b7fd71ae3551ee72f7891640dec5c07
parentab51ac11e68ce0b075688bf17fc89e0ba645b2ed (diff)
Add streq and strcaseeq and use them throughout
-rw-r--r--cfgdir.c16
-rw-r--r--config.c10
-rw-r--r--main.c8
-rw-r--r--server.c6
-rw-r--r--systemd.c2
-rw-r--r--utils.c3
-rw-r--r--utils.h11
7 files changed, 33 insertions, 23 deletions
diff --git a/cfgdir.c b/cfgdir.c
index 494ff81..923926b 100644
--- a/cfgdir.c
+++ b/cfgdir.c
@@ -244,10 +244,10 @@ scfg_parse(struct cfg *cfg, struct server *server)
switch (key) {
case SCFG_KEY_TYPE:
- if (!strcmp(value.str, "proxy")) {
+ if (streq(value.str, "proxy")) {
if (!server_set_type(cfg, server, SERVER_TYPE_PROXY))
return;
- } else if (!strcmp(value.str, "announce")) {
+ } else if (streq(value.str, "announce")) {
if (!server_set_type(cfg, server, SERVER_TYPE_ANNOUNCE))
return;
}
@@ -281,23 +281,23 @@ scfg_parse(struct cfg *cfg, struct server *server)
break;
case SCFG_KEY_STOP_METHOD:
- if (!strcmp(value.str, "exec")) {
+ if (streq(value.str, "exec")) {
if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_EXEC))
break;
- } else if (!strcmp(value.str, "rcon")) {
+ } else if (streq(value.str, "rcon")) {
if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_RCON))
break;
- } else if (!strcmp(value.str, "systemd")) {
+ } else if (streq(value.str, "systemd")) {
if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_SYSTEMD))
break;
}
return;
case SCFG_KEY_START_METHOD:
- if (!strcmp(value.str, "exec")) {
+ if (streq(value.str, "exec")) {
if (server_set_start_method(cfg, server, SERVER_START_METHOD_EXEC))
break;
- } else if (!strcmp(value.str, "systemd")) {
+ } else if (streq(value.str, "systemd")) {
if (server_set_start_method(cfg, server, SERVER_START_METHOD_SYSTEMD))
break;
}
@@ -384,7 +384,7 @@ scfg_valid_filename(const char *name)
return false;
if ((suffix = strrchr(name, '.')) == NULL)
return false;
- if (strcmp(suffix, ".server"))
+ if (!streq(suffix, ".server"))
return false;
return true;
diff --git a/config.c b/config.c
index 30c9cde..f511dbd 100644
--- a/config.c
+++ b/config.c
@@ -190,7 +190,7 @@ strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async)
/* early list_add to make sure saddr is free():d on error */
list_add(&saddr->list, list);
- if (!strcmp(str, "*"))
+ if (streq(str, "*"))
saddr->in6.sin6_addr = in6addr_any;
else if (inet_pton(AF_INET6, str, &saddr->in6.sin6_addr) <= 0)
goto error;
@@ -361,7 +361,7 @@ config_parse_line(struct cfg *cfg, const char *filename, char **buf,
goto error;
for (i = 0; kvmap[i].key_name; i++) {
- if (strcmp(kvmap[i].key_name, key))
+ if (!streq(kvmap[i].key_name, key))
continue;
switch (kvmap[i].value_type) {
@@ -424,10 +424,10 @@ config_parse_line(struct cfg *cfg, const char *filename, char **buf,
break;
case CFG_VAL_TYPE_BOOL:
- if (!strcasecmp(tmp, "yes") || !strcasecmp(tmp, "true")) {
+ if (strcaseeq(tmp, "yes") || strcaseeq(tmp, "true")) {
rvalue->type = CFG_VAL_TYPE_BOOL;
rvalue->boolean = true;
- } else if (!strcasecmp(tmp, "no") || !strcasecmp(tmp, "false")) {
+ } else if (strcaseeq(tmp, "no") || strcaseeq(tmp, "false")) {
rvalue->type = CFG_VAL_TYPE_BOOL;
rvalue->boolean = false;
} else {
@@ -483,7 +483,7 @@ config_parse_header(struct cfg *cfg, const char *filename, const char *title,
char titlehdr[strlen(title) + 3];
sprintf(titlehdr, "[%s]", title);
- if (strcmp(line, titlehdr)) {
+ if (!streq(line, titlehdr)) {
error("%s: incorrect header in configuration file", filename);
return false;
}
diff --git a/main.c b/main.c
index 0bd5bb0..ba9ea37 100644
--- a/main.c
+++ b/main.c
@@ -104,7 +104,7 @@ msg(enum debug_lvl lvl, const char *fmt, va_list ap)
break;
}
- if (!strcmp(e, "dumb")) {
+ if (streq(e, "dumb")) {
sd_daemon = false;
use_colors = false;
break;
@@ -432,10 +432,10 @@ cfg_init(int argc, char **argv)
}
case 'd':
- if (!strcasecmp(optarg, "all")) {
+ if (strcaseeq(optarg, "all")) {
debug_mask = ~0;
break;
- } else if (!strcasecmp(optarg, "list")) {
+ } else if (strcaseeq(optarg, "list")) {
error("Debug categories:");
error(" * all");
for (i = 0; debug_category_str[i].name; i++)
@@ -444,7 +444,7 @@ cfg_init(int argc, char **argv)
}
for (i = 0; debug_category_str[i].name; i++) {
- if (!strcasecmp(optarg, debug_category_str[i].name))
+ if (strcaseeq(optarg, debug_category_str[i].name))
break;
}
diff --git a/server.c b/server.c
index 04275d9..c8f75f7 100644
--- a/server.c
+++ b/server.c
@@ -130,7 +130,7 @@ server_delete_by_name(struct cfg *cfg, const char *name)
assert_return(cfg && !empty_str(name));
list_for_each_entry(server, &cfg->servers, list) {
- if (!strcmp(server->name, name)) {
+ if (streq(server->name, name)) {
server_delete(cfg, server);
return;
}
@@ -678,7 +678,7 @@ server_set_systemd_service(struct cfg *cfg, struct server *server,
assert_return(cfg && server && !empty_str(service) && !server->systemd_service, false);
suffix = strrchr(service, '.');
- if (!suffix || strcmp(suffix, ".service")) {
+ if (!suffix || !streq(suffix, ".service")) {
tmp = zmalloc(strlen(service) + strlen(".service") + 1);
if (tmp)
sprintf(tmp, "%s.service", service);
@@ -785,7 +785,7 @@ server_new(struct cfg *cfg, const char *name)
assert_return(cfg && !empty_str(name), NULL);
list_for_each_entry(server, &cfg->servers, list) {
- if (strcmp(name, server->name))
+ if (!streq(name, server->name))
continue;
error("attempt to add duplicate server: %s", name);
return server;
diff --git a/systemd.c b/systemd.c
index a592d3a..1ddbd18 100644
--- a/systemd.c
+++ b/systemd.c
@@ -131,7 +131,7 @@ systemd_service_running(struct cfg *cfg, struct server *server)
goto out;
}
- if (!strcmp(status, "active")) {
+ if (streq(status, "active")) {
running = true;
debug(DBG_SYSD, "systemd service %s (%s) is active",
server->systemd_service, server->systemd_obj);
diff --git a/utils.c b/utils.c
index 0ddaa28..fc1b81a 100644
--- a/utils.c
+++ b/utils.c
@@ -135,8 +135,7 @@ debug_resource_usage()
debug(DBG_MALLOC, "Open files:");
while ((dent = readdir(dir)) != NULL) {
- if (!strcmp(dent->d_name, ".") ||
- !strcmp(dent->d_name, ".."))
+ if (streq(dent->d_name, ".") || streq(dent->d_name, ".."))
continue;
r = readlinkat(dirfd(dir), dent->d_name, buf, sizeof(buf));
diff --git a/utils.h b/utils.h
index f87159e..a38602c 100644
--- a/utils.h
+++ b/utils.h
@@ -2,6 +2,7 @@
#define fooutilshfoo
#include <stdio.h>
+#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <linux/if_packet.h>
@@ -91,6 +92,16 @@ static inline bool empty_str(const char *str)
return false;
}
+static inline bool streq(const char *a, const char *b)
+{
+ return strcmp(a, b) == 0;
+}
+
+static inline bool strcaseeq(const char *a, const char *b)
+{
+ return strcasecmp(a, b) == 0;
+}
+
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define chtobe32(x) __bswap_constant_32(x)
#else