From ae48dc3b3caeef7eaa4a079b11cdda988d9c1f0d Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sun, 21 Jun 2020 00:06:29 +0200 Subject: Add streq and strcaseeq and use them throughout --- cfgdir.c | 16 ++++++++-------- config.c | 10 +++++----- main.c | 8 ++++---- server.c | 6 +++--- systemd.c | 2 +- utils.c | 3 +-- utils.h | 11 +++++++++++ 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 +#include #include #include #include @@ -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 -- cgit v1.2.3