From: Phil Carmody Date: Thu, 11 Jan 2018 13:03:36 +0000 (+0200) Subject: global - migrate strncmp literals to str_begins X-Git-Tag: 2.3.2.rc1~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9be765076fd7ac8593b7f759d1ec7dcf96654a2a;p=thirdparty%2Fdovecot%2Fcore.git global - migrate strncmp literals to str_begins Simplify a bunch of strncmp(,,number) calls. git ls-files \*.[ch] | xargs perl -p -i -e 's/strncmp\((.*?), ?(\".*?\"), ?(\d+)\) == 0/str_begins($1, $2)/g' git ls-files \*.[ch] | xargs perl -p -i -e 's/strncmp\((.*?), ?(\".*?\"), ?(\d+)\) != 0/!str_begins($1, $2)/g' I ran a longer script to verify that all of the string literals and the length matched. They didn't: $ git grep strncmp | perl -ne 'print if(m/strncmp\([^,]*,\s*"(.*?)",\s*(\d+)/ and ($s=$1,$t=$2,$s=~s/\\[tn]/#/g,length($s)) != $t)' src/auth/db-oauth2.c: if (strncmp(field, "oauth2:", 8) == 0 && With the new functions, that kind of typo is impossible. Signed-off-by: Phil Carmody --- diff --git a/src/auth/auth-client-connection.c b/src/auth/auth-client-connection.c index 78293fd6b2..0bcc8bc2eb 100644 --- a/src/auth/auth-client-connection.c +++ b/src/auth/auth-client-connection.c @@ -215,7 +215,7 @@ auth_client_cancel(struct auth_client_connection *conn, const char *line) static bool auth_client_handle_line(struct auth_client_connection *conn, const char *line) { - if (strncmp(line, "AUTH\t", 5) == 0) { + if (str_begins(line, "AUTH\t")) { if (conn->auth->set->debug) { i_debug("client in: %s", auth_line_hide_pass(conn, line)); @@ -223,7 +223,7 @@ auth_client_handle_line(struct auth_client_connection *conn, const char *line) return auth_request_handler_auth_begin(conn->request_handler, line + 5); } - if (strncmp(line, "CONT\t", 5) == 0) { + if (str_begins(line, "CONT\t")) { if (conn->auth->set->debug) { i_debug("client in: %s", cont_line_hide_pass(conn, line)); @@ -231,7 +231,7 @@ auth_client_handle_line(struct auth_client_connection *conn, const char *line) return auth_request_handler_auth_continue(conn->request_handler, line + 5); } - if (strncmp(line, "CANCEL\t", 7) == 0) { + if (str_begins(line, "CANCEL\t")) { if (conn->auth->set->debug) i_debug("client in: %s", line); return auth_client_cancel(conn, line + 7); @@ -273,7 +273,7 @@ static void auth_client_input(struct auth_client_connection *conn) const char *p; /* split the version line */ - if (strncmp(line, "VERSION\t", 8) != 0 || + if (!str_begins(line, "VERSION\t") || str_parse_uint(line + 8, &vmajor, &p) < 0 || *(p++) != '\t' || str_to_uint(p, &vminor) < 0) { i_error("Authentication client " @@ -294,7 +294,7 @@ static void auth_client_input(struct auth_client_connection *conn) continue; } - if (strncmp(line, "CPID\t", 5) == 0) { + if (str_begins(line, "CPID\t")) { if (!auth_client_input_cpid(conn, line + 5)) { auth_client_connection_destroy(&conn); return; diff --git a/src/auth/auth-master-connection.c b/src/auth/auth-master-connection.c index a694b9dde4..368c78bb61 100644 --- a/src/auth/auth-master-connection.c +++ b/src/auth/auth-master-connection.c @@ -610,20 +610,20 @@ auth_master_input_line(struct auth_master_connection *conn, const char *line) if (conn->auth->set->debug) i_debug("master in: %s", line); - if (strncmp(line, "USER\t", 5) == 0) + if (str_begins(line, "USER\t")) return master_input_user(conn, line + 5); - if (strncmp(line, "LIST\t", 5) == 0) + if (str_begins(line, "LIST\t")) return master_input_list(conn, line + 5); - if (strncmp(line, "PASS\t", 5) == 0) + if (str_begins(line, "PASS\t")) return master_input_pass(conn, line + 5); if (!conn->userdb_only) { i_assert(conn->userdb_restricted_uid == 0); - if (strncmp(line, "REQUEST\t", 8) == 0) + if (str_begins(line, "REQUEST\t")) return master_input_request(conn, line + 8); - if (strncmp(line, "CACHE-FLUSH\t", 12) == 0) + if (str_begins(line, "CACHE-FLUSH\t")) return master_input_cache_flush(conn, line + 12); - if (strncmp(line, "CPID\t", 5) == 0) { + if (str_begins(line, "CPID\t")) { i_error("Authentication client trying to connect to " "master socket"); return FALSE; @@ -662,7 +662,7 @@ static void master_input(struct auth_master_connection *conn) return; /* make sure the major version matches */ - if (strncmp(line, "VERSION\t", 8) != 0 || + if (!str_begins(line, "VERSION\t") || !str_uint_equals(t_strcut(line + 8, '\t'), AUTH_MASTER_PROTOCOL_MAJOR_VERSION)) { i_error("Master not compatible with this server " diff --git a/src/auth/auth-request.c b/src/auth/auth-request.c index 8aaa4f99bf..5bfe122d78 100644 --- a/src/auth/auth-request.c +++ b/src/auth/auth-request.c @@ -513,9 +513,9 @@ bool auth_request_import(struct auth_request *request, request->delayed_credentials_size = 1; } else if (strcmp(key, "mech") == 0) request->mech_name = p_strdup(request->pool, value); - else if (strncmp(key, "passdb_", 7) == 0) + else if (str_begins(key, "passdb_")) auth_fields_add(request->extra_fields, key+7, value, 0); - else if (strncmp(key, "userdb_", 7) == 0) { + else if (str_begins(key, "userdb_")) { if (request->userdb_reply == NULL) request->userdb_reply = auth_fields_init(request->pool); auth_fields_add(request->userdb_reply, key+7, value, 0); @@ -1962,7 +1962,7 @@ void auth_request_set_field(struct auth_request *request, } } else if (strcmp(name, "allow_real_nets") == 0) { auth_request_validate_networks(request, name, value, &request->real_remote_ip); - } else if (strncmp(name, "userdb_", 7) == 0) { + } else if (str_begins(name, "userdb_")) { /* for prefetch userdb */ request->userdb_prefetch_set = TRUE; if (request->userdb_reply == NULL) @@ -2017,7 +2017,7 @@ void auth_request_set_field(struct auth_request *request, void auth_request_set_null_field(struct auth_request *request, const char *name) { - if (strncmp(name, "userdb_", 7) == 0) { + if (str_begins(name, "userdb_")) { /* make sure userdb prefetch is used even if all the fields were returned as NULL. */ request->userdb_prefetch_set = TRUE; diff --git a/src/auth/auth-worker-server.c b/src/auth/auth-worker-server.c index 65bf5f05c0..a0b16d7657 100644 --- a/src/auth/auth-worker-server.c +++ b/src/auth/auth-worker-server.c @@ -277,7 +277,7 @@ static bool auth_worker_request_handle(struct auth_worker_connection *conn, struct auth_worker_request *request, const char *line) { - if (strncmp(line, "*\t", 2) == 0) { + if (str_begins(line, "*\t")) { /* multi-line reply, not finished yet */ if (conn->resuming) timeout_reset(conn->to); diff --git a/src/auth/db-ldap.c b/src/auth/db-ldap.c index b9c59cf48c..f62512d871 100644 --- a/src/auth/db-ldap.c +++ b/src/auth/db-ldap.c @@ -1246,7 +1246,7 @@ int db_ldap_connect(struct ldap_connection *conn) if (ret != LDAP_SUCCESS) { if (ret == LDAP_OPERATIONS_ERROR && conn->set.uris != NULL && - strncmp(conn->set.uris, "ldaps:", 6) == 0) { + str_begins(conn->set.uris, "ldaps:")) { i_fatal("LDAP %s: Don't use both tls=yes " "and ldaps URI", conn->config_path); } diff --git a/src/auth/db-oauth2.c b/src/auth/db-oauth2.c index eb95a8b756..69f76177e3 100644 --- a/src/auth/db-oauth2.c +++ b/src/auth/db-oauth2.c @@ -291,7 +291,7 @@ db_oauth2_have_all_fields(struct db_oauth2_request *req) var_get_key_range(ptr, &idx, &size); ptr = ptr+idx; field = t_strndup(ptr,size); - if (strncmp(field, "oauth2:", 8) == 0 && + if (str_begins(field, "oauth2:") && !auth_fields_exists(req->fields, ptr+8)) return FALSE; ptr = ptr+size; diff --git a/src/auth/main.c b/src/auth/main.c index 7221e5e1e4..b38c963668 100644 --- a/src/auth/main.c +++ b/src/auth/main.c @@ -160,8 +160,8 @@ static void listeners_init(void) static bool auth_module_filter(const char *name, void *context ATTR_UNUSED) { - if (strncmp(name, "authdb_", 7) == 0 || - strncmp(name, "mech_", 5) == 0) { + if (str_begins(name, "authdb_") || + str_begins(name, "mech_")) { /* this is lazily loaded */ return FALSE; } diff --git a/src/auth/mech-oauth2.c b/src/auth/mech-oauth2.c index 0f1740a89c..b5e9be52ca 100644 --- a/src/auth/mech-oauth2.c +++ b/src/auth/mech-oauth2.c @@ -104,7 +104,7 @@ mech_xoauth2_auth_continue(struct auth_request *request, const char *const *fields = t_strsplit(t_strndup(data, data_size), "\x01"); for(ptr = fields; *ptr != NULL; ptr++) { - if (strncmp(*ptr,"user=", 5) == 0) { + if (str_begins(*ptr, "user=")) { /* xoauth2 does not require unescaping because the data format does not contain anything to escape */ const char *username = (*ptr)+5; @@ -115,7 +115,7 @@ mech_xoauth2_auth_continue(struct auth_request *request, return; } user_given = TRUE; - } else if (strncmp(*ptr,"auth=", 5) == 0) { + } else if (str_begins(*ptr, "auth=")) { const char *value = (*ptr)+5; if (strncasecmp(value, "bearer ", 7) == 0 && oauth2_valid_token(value+7)) { @@ -212,7 +212,7 @@ mech_oauthbearer_auth_continue(struct auth_request *request, } for(ptr = fields; *ptr != NULL; ptr++) { - if (strncmp(*ptr,"auth=", 5) == 0) { + if (str_begins(*ptr, "auth=")) { const char *value = (*ptr)+5; if (strncasecmp(value, "bearer ", 7) == 0 && oauth2_valid_token(value+7)) { diff --git a/src/auth/mech-otp.c b/src/auth/mech-otp.c index 14952de00b..5666e99f36 100644 --- a/src/auth/mech-otp.c +++ b/src/auth/mech-otp.c @@ -203,13 +203,13 @@ mech_otp_auth_phase2(struct auth_request *auth_request, { const char *str = t_strndup(data, data_size); - if (strncmp(str, "hex:", 4) == 0) { + if (str_begins(str, "hex:")) { mech_otp_verify(auth_request, str + 4, TRUE); - } else if (strncmp(str, "word:", 5) == 0) { + } else if (str_begins(str, "word:")) { mech_otp_verify(auth_request, str + 5, FALSE); - } else if (strncmp(str, "init-hex:", 9) == 0) { + } else if (str_begins(str, "init-hex:")) { mech_otp_verify_init(auth_request, str + 9, TRUE); - } else if (strncmp(str, "init-word:", 10) == 0) { + } else if (str_begins(str, "init-word:")) { mech_otp_verify_init(auth_request, str + 10, FALSE); } else { auth_request_log_error(auth_request, AUTH_SUBSYS_MECH, diff --git a/src/auth/passdb-blocking.c b/src/auth/passdb-blocking.c index 7f72934e2a..76f7e821e7 100644 --- a/src/auth/passdb-blocking.c +++ b/src/auth/passdb-blocking.c @@ -146,7 +146,7 @@ set_credentials_callback(const char *reply, void *context) struct auth_request *request = context; bool success; - success = strcmp(reply, "OK") == 0 || strncmp(reply, "OK\t", 3) == 0; + success = strcmp(reply, "OK") == 0 || str_begins(reply, "OK\t"); request->private_callback.set_credentials(success, request); auth_request_unref(&request); return TRUE; diff --git a/src/auth/passdb-bsdauth.c b/src/auth/passdb-bsdauth.c index d2a237c200..c67a7d0bf8 100644 --- a/src/auth/passdb-bsdauth.c +++ b/src/auth/passdb-bsdauth.c @@ -66,7 +66,7 @@ bsdauth_preinit(pool_t pool, const char *args) if (strcmp(args, "blocking=no") == 0) module->blocking = FALSE; - else if (strncmp(args, "cache_key=", 10) == 0) + else if (str_begins(args, "cache_key=")) module->default_cache_key = auth_cache_parse_key(pool, args + 10); else if (*args != '\0') i_fatal("passdb bsdauth: Unknown setting: %s", args); diff --git a/src/auth/passdb-checkpassword.c b/src/auth/passdb-checkpassword.c index d5302deed3..dd6cc3c38f 100644 --- a/src/auth/passdb-checkpassword.c +++ b/src/auth/passdb-checkpassword.c @@ -33,7 +33,7 @@ auth_checkpassword_callback(struct auth_request *request, break; } for (i = 0; extra_fields[i] != NULL; i++) { - if (strncmp(extra_fields[i], "password=", 9) == 0) + if (str_begins(extra_fields[i], "password=")) crypted_pass = extra_fields[i]+9; else if (extra_fields[i][0] != '\0') { auth_request_set_field_keyvalue(request, @@ -86,7 +86,7 @@ credentials_checkpassword_callback(struct auth_request *request, break; } for (i = 0; extra_fields[i] != NULL; i++) { - if (strncmp(extra_fields[i], "password=", 9) == 0) + if (str_begins(extra_fields[i], "password=")) crypted_pass = extra_fields[i]+9; else if (extra_fields[i][0] != '\0') { auth_request_set_field_keyvalue(request, diff --git a/src/auth/passdb-lua.c b/src/auth/passdb-lua.c index 55f252bd31..02dd15f029 100644 --- a/src/auth/passdb-lua.c +++ b/src/auth/passdb-lua.c @@ -111,9 +111,9 @@ passdb_lua_preinit(pool_t pool, const char *args) module = p_new(pool, struct dlua_passdb_module, 1); const char *const *fields = t_strsplit_spaces(args, " "); while(*fields != NULL) { - if (strncmp(*fields, "file=", 5) == 0) { + if (str_begins(*fields, "file=")) { module->file = p_strdup(pool, (*fields)+5); - } else if (strncmp(*fields, "blocking=", 9) == 0) { + } else if (str_begins(*fields, "blocking=")) { const char *value = (*fields)+9; if (strcmp(value, "yes") == 0) { blocking = TRUE; @@ -124,12 +124,12 @@ passdb_lua_preinit(pool_t pool, const char *args) "Field blocking must be yes or no", value); } - } else if (strncmp(*fields, "cache_key=", 10) == 0) { + } else if (str_begins(*fields, "cache_key=")) { if (*((*fields)+10) != '\0') cache_key = (*fields)+10; else /* explicitly disable auth caching for lua */ cache_key = NULL; - } else if (strncmp(*fields, "scheme=", 7) == 0) { + } else if (str_begins(*fields, "scheme=")) { scheme = p_strdup(pool, (*fields)+7); } else { i_fatal("Unsupported parameter %s", *fields); diff --git a/src/auth/passdb-pam.c b/src/auth/passdb-pam.c index fe402082f2..6828f194e2 100644 --- a/src/auth/passdb-pam.c +++ b/src/auth/passdb-pam.c @@ -358,7 +358,7 @@ pam_preinit(pool_t pool, const char *args) module->pam_session = TRUE; else if (strcmp(t_args[i], "setcred=yes") == 0) module->pam_setcred = TRUE; - else if (strncmp(t_args[i], "cache_key=", 10) == 0) { + else if (str_begins(t_args[i], "cache_key=")) { module->module.default_cache_key = auth_cache_parse_key(pool, t_args[i] + 10); } else if (strcmp(t_args[i], "blocking=yes") == 0) { @@ -368,7 +368,7 @@ pam_preinit(pool_t pool, const char *args) } else if (strcmp(t_args[i], "*") == 0) { /* for backwards compatibility */ module->service_name = "%Ls"; - } else if (strncmp(t_args[i], "max_requests=", 13) == 0) { + } else if (str_begins(t_args[i], "max_requests=")) { if (str_to_uint(t_args[i] + 13, &module->requests_left) < 0) { i_error("pam: Invalid requests_left value: %s", diff --git a/src/auth/passdb-vpopmail.c b/src/auth/passdb-vpopmail.c index 34b351cab8..733a12655e 100644 --- a/src/auth/passdb-vpopmail.c +++ b/src/auth/passdb-vpopmail.c @@ -186,10 +186,10 @@ vpopmail_preinit(pool_t pool, const char *args) tmp = t_strsplit_spaces(args, " "); for (; *tmp != NULL; tmp++) { - if (strncmp(*tmp, "cache_key=", 10) == 0) { + if (str_begins(*tmp, "cache_key=")) { module->module.default_cache_key = auth_cache_parse_key(pool, *tmp + 10); - } else if (strncmp(*tmp, "webmail=", 8) == 0) { + } else if (str_begins(*tmp, "webmail=")) { if (net_addr2ip(*tmp + 8, &module->webmail_ip) < 0) i_fatal("vpopmail: Invalid webmail IP address"); } else if (strcmp(*tmp, "blocking=no") == 0) { diff --git a/src/auth/password-scheme-crypt.c b/src/auth/password-scheme-crypt.c index db9b50c90c..34febad884 100644 --- a/src/auth/password-scheme-crypt.c +++ b/src/auth/password-scheme-crypt.c @@ -78,7 +78,7 @@ crypt_verify_blowfish(const char *plaintext, const struct password_generate_para password = t_strndup(raw_password, size); if (size < CRYPT_BLF_PREFIX_LEN || - strncmp(password, "$2", 2) != 0 || + !str_begins(password, "$2") || password[2] < 'a' || password[2] > 'z' || password[3] != '$') { *error_r = "Password is not blowfish password"; diff --git a/src/auth/password-scheme.c b/src/auth/password-scheme.c index dc684e5939..0b84c125b1 100644 --- a/src/auth/password-scheme.c +++ b/src/auth/password-scheme.c @@ -103,7 +103,7 @@ const char *password_get_scheme(const char **password) if (*password == NULL) return NULL; - if (strncmp(*password, "$1$", 3) == 0) { + if (str_begins(*password, "$1$")) { /* $1$$[$] */ p = strchr(*password + 3, '$'); if (p != NULL) { @@ -342,7 +342,7 @@ md5_verify(const char *plaintext, const struct password_generate_params *params, size_t md5_size; password = t_strndup(raw_password, size); - if (strncmp(password, "$1$", 3) == 0) { + if (str_begins(password, "$1$")) { /* MD5-CRYPT */ str = password_generate_md5_crypt(plaintext, password); return strcmp(str, password) == 0 ? 1 : 0; diff --git a/src/auth/userdb-blocking.c b/src/auth/userdb-blocking.c index 7b134c876d..fd337434b1 100644 --- a/src/auth/userdb-blocking.c +++ b/src/auth/userdb-blocking.c @@ -20,13 +20,13 @@ static bool user_callback(const char *reply, void *context) enum userdb_result result; const char *username, *args; - if (strncmp(reply, "FAIL\t", 5) == 0) { + if (str_begins(reply, "FAIL\t")) { result = USERDB_RESULT_INTERNAL_FAILURE; args = reply + 5; - } else if (strncmp(reply, "NOTFOUND\t", 9) == 0) { + } else if (str_begins(reply, "NOTFOUND\t")) { result = USERDB_RESULT_USER_UNKNOWN; args = reply + 9; - } else if (strncmp(reply, "OK\t", 3) == 0) { + } else if (str_begins(reply, "OK\t")) { result = USERDB_RESULT_OK; username = reply + 3; args = strchr(username, '\t'); @@ -70,7 +70,7 @@ static bool iter_callback(const char *reply, void *context) { struct blocking_userdb_iterate_context *ctx = context; - if (strncmp(reply, "*\t", 2) == 0) { + if (str_begins(reply, "*\t")) { if (ctx->destroyed) return TRUE; ctx->next = FALSE; diff --git a/src/auth/userdb-checkpassword.c b/src/auth/userdb-checkpassword.c index 55d029b4d9..aa6455e9a9 100644 --- a/src/auth/userdb-checkpassword.c +++ b/src/auth/userdb-checkpassword.c @@ -29,7 +29,7 @@ userdb_checkpassword_callback(struct auth_request *request, break; case DB_CHECKPASSWORD_STATUS_OK: for (i = 0; extra_fields[i] != NULL; i++) { - if (strncmp(extra_fields[i], "userdb_", 7) != 0) + if (!str_begins(extra_fields[i], "userdb_")) continue; auth_request_set_field_keyvalue(request, extra_fields[i], NULL); diff --git a/src/auth/userdb-lua.c b/src/auth/userdb-lua.c index c460dc435c..d15451e3fc 100644 --- a/src/auth/userdb-lua.c +++ b/src/auth/userdb-lua.c @@ -39,9 +39,9 @@ userdb_lua_preinit(pool_t pool, const char *args) module = p_new(pool, struct dlua_userdb_module, 1); const char *const *fields = t_strsplit_spaces(args, " "); while(*fields != NULL) { - if (strncmp(*fields, "file=", 5) == 0) { + if (str_begins(*fields, "file=")) { module->file = p_strdup(pool, (*fields)+5); - } else if (strncmp(*fields, "blocking=", 9) == 0) { + } else if (str_begins(*fields, "blocking=")) { const char *value = (*fields)+9; if (strcmp(value, "yes") == 0) { blocking = TRUE; @@ -52,7 +52,7 @@ userdb_lua_preinit(pool_t pool, const char *args) "Field blocking must be yes or no", value); } - } else if (strncmp(*fields, "cache_key=", 10) == 0) { + } else if (str_begins(*fields, "cache_key=")) { if (*((*fields)+10) != '\0') cache_key = (*fields)+10; else /* explicitly disable auth caching for lua */ diff --git a/src/auth/userdb-passwd-file.c b/src/auth/userdb-passwd-file.c index 190ed045d5..d90bcab928 100644 --- a/src/auth/userdb-passwd-file.c +++ b/src/auth/userdb-passwd-file.c @@ -38,7 +38,7 @@ passwd_file_add_extra_fields(struct auth_request *request, char *const *fields) table = auth_request_get_var_expand_table(request, NULL); for (i = 0; fields[i] != NULL; i++) { - if (strncmp(fields[i], "userdb_", 7) != 0) + if (!str_begins(fields[i], "userdb_")) continue; key = fields[i] + 7; @@ -186,7 +186,7 @@ passwd_file_preinit(pool_t pool, const char *args) const char *format = PASSWD_FILE_DEFAULT_USERNAME_FORMAT; const char *p; - if (strncmp(args, "username_format=", 16) == 0) { + if (str_begins(args, "username_format=")) { args += 16; p = strchr(args, ' '); if (p == NULL) { diff --git a/src/auth/userdb-vpopmail.c b/src/auth/userdb-vpopmail.c index c6e1857753..4291a1049c 100644 --- a/src/auth/userdb-vpopmail.c +++ b/src/auth/userdb-vpopmail.c @@ -162,10 +162,10 @@ vpopmail_preinit(pool_t pool, const char *args) module->module.blocking = TRUE; for (tmp = t_strsplit(args, " "); *tmp != NULL; tmp++) { - if (strncmp(*tmp, "cache_key=", 10) == 0) + if (str_begins(*tmp, "cache_key=")) module->module.default_cache_key = p_strdup(pool, *tmp + 10); - else if (strncmp(*tmp, "quota_template=", 15) == 0) { + else if (str_begins(*tmp, "quota_template=")) { p = strchr(*tmp + 15, '='); if (p == NULL) { i_fatal("vpopmail userdb: " diff --git a/src/config/config-connection.c b/src/config/config-connection.c index f3803e6fc9..f3522f1060 100644 --- a/src/config/config-connection.c +++ b/src/config/config-connection.c @@ -78,22 +78,22 @@ static int config_connection_request(struct config_connection *conn, t_array_init(&modules, 4); i_zero(&filter); for (; *args != NULL; args++) { - if (strncmp(*args, "service=", 8) == 0) + if (str_begins(*args, "service=")) filter.service = *args + 8; - else if (strncmp(*args, "module=", 7) == 0) { + else if (str_begins(*args, "module=")) { module = *args + 7; if (strcmp(module, "master") == 0) is_master = TRUE; array_append(&modules, &module, 1); - } else if (strncmp(*args, "lname=", 6) == 0) + } else if (str_begins(*args, "lname=")) filter.local_name = *args + 6; - else if (strncmp(*args, "lip=", 4) == 0) { + else if (str_begins(*args, "lip=")) { if (net_addr2ip(*args + 4, &filter.local_net) == 0) { filter.local_bits = IPADDR_IS_V4(&filter.local_net) ? 32 : 128; } - } else if (strncmp(*args, "rip=", 4) == 0) { + } else if (str_begins(*args, "rip=")) { if (net_addr2ip(*args + 4, &filter.remote_net) == 0) { filter.remote_bits = IPADDR_IS_V4(&filter.remote_net) ? diff --git a/src/config/old-set-parser.c b/src/config/old-set-parser.c index 5a7ba4cb14..93d78c8d3b 100644 --- a/src/config/old-set-parser.c +++ b/src/config/old-set-parser.c @@ -350,7 +350,7 @@ old_settings_handle_root(struct config_parser_context *ctx, return TRUE; } if (ctx->old->auth_section == 1) { - if (strncmp(key, "auth_", 5) != 0) + if (!str_begins(key, "auth_")) key = t_strconcat("auth_", key, NULL); config_parser_apply_line(ctx, CONFIG_LINE_TYPE_KEYVALUE, key, value); @@ -561,7 +561,7 @@ old_settings_handle_proto(struct config_parser_context *ctx, } if (ctx->old->auth_section == 1) { - if (strncmp(key, "auth_", 5) != 0) + if (!str_begins(key, "auth_")) key = t_strconcat("auth_", key, NULL); } diff --git a/src/config/sysinfo-get.c b/src/config/sysinfo-get.c index a2397b538a..dffebe6e2c 100644 --- a/src/config/sysinfo-get.c +++ b/src/config/sysinfo-get.c @@ -37,7 +37,7 @@ static bool lsb_distro_get(const char *path, const char **name_r) return FALSE; for (p = t_strsplit(data, "\n"); *p != NULL; p++) { - if (strncmp(*p, "DISTRIB_DESCRIPTION=", 20) == 0) + if (str_begins(*p, "DISTRIB_DESCRIPTION=")) break; } if (*p == NULL) diff --git a/src/director/login-connection.c b/src/director/login-connection.c index 02052a441b..0cc5b1b546 100644 --- a/src/director/login-connection.c +++ b/src/director/login-connection.c @@ -143,9 +143,9 @@ login_host_callback(const struct mail_host *host, const char *hostname, unsigned int secs; if (host == NULL) { - if (strncmp(request->line, "OK\t", 3) == 0) + if (str_begins(request->line, "OK\t")) line_params = request->line + 3; - else if (strncmp(request->line, "PASS\t", 5) == 0) + else if (str_begins(request->line, "PASS\t")) line_params = request->line + 5; else i_panic("BUG: Unexpected line: %s", request->line); @@ -196,10 +196,10 @@ static void auth_input_line(const char *line, void *context) return; } if (conn->type != LOGIN_CONNECTION_TYPE_USERDB && - strncmp(line, "OK\t", 3) == 0) + str_begins(line, "OK\t")) line_params = line + 3; else if (conn->type == LOGIN_CONNECTION_TYPE_USERDB && - strncmp(line, "PASS\t", 5) == 0) + str_begins(line, "PASS\t")) line_params = line + 5; else { login_connection_send_line(conn, line); @@ -216,28 +216,28 @@ static void auth_input_line(const char *line, void *context) i_zero(&temp_request); for (; *args != NULL; args++) { - if (strncmp(*args, "proxy", 5) == 0 && + if (str_begins(*args, "proxy") && ((*args)[5] == '=' || (*args)[5] == '\0')) proxy = TRUE; - else if (strncmp(*args, "host=", 5) == 0) + else if (str_begins(*args, "host=")) host = TRUE; - else if (strncmp(*args, "lip=", 4) == 0) { + else if (str_begins(*args, "lip=")) { if (net_addr2ip((*args) + 4, &temp_request.local_ip) < 0) i_error("auth sent invalid lip field: %s", (*args) + 6); - } else if (strncmp(*args, "lport=", 6) == 0) { + } else if (str_begins(*args, "lport=")) { if (net_str2port((*args) + 6, &temp_request.local_port) < 0) i_error("auth sent invalid lport field: %s", (*args) + 6); - } else if (strncmp(*args, "port=", 5) == 0) { + } else if (str_begins(*args, "port=")) { if (net_str2port((*args) + 5, &temp_request.dest_port) < 0) i_error("auth sent invalid port field: %s", (*args) + 6); - } else if (strncmp(*args, "destuser=", 9) == 0) + } else if (str_begins(*args, "destuser=")) username = *args + 9; - else if (strncmp(*args, "director_tag=", 13) == 0) + else if (str_begins(*args, "director_tag=")) tag = *args + 13; - else if (strncmp(*args, "director_proxy_maybe", 20) == 0 && + else if (str_begins(*args, "director_proxy_maybe") && ((*args)[20] == '=' || (*args)[20] == '\0')) temp_request.director_proxy_maybe = TRUE; - else if (strncmp(*args, "user=", 5) == 0) { + else if (str_begins(*args, "user=")) { if (username == NULL) username = *args + 5; } diff --git a/src/dns/dns-client.c b/src/dns/dns-client.c index 6d685676db..53a41be7ff 100644 --- a/src/dns/dns-client.c +++ b/src/dns/dns-client.c @@ -31,7 +31,7 @@ static int dns_client_input_line(struct dns_client *client, const char *line) unsigned int i, ips_count; int ret; - if (strncmp(line, "IP\t", 3) == 0) { + if (str_begins(line, "IP\t")) { ret = net_gethostbyname(line + 3, &ips, &ips_count); if (ret == 0 && ips_count == 0) { /* shouldn't happen, but fix it anyway.. */ @@ -48,7 +48,7 @@ static int dns_client_input_line(struct dns_client *client, const char *line) net_ip2addr(&ips[i]), "\n", NULL)); } } - } else if (strncmp(line, "NAME\t", 5) == 0) { + } else if (str_begins(line, "NAME\t")) { if (net_addr2ip(line+5, &ip) < 0) o_stream_nsend_str(client->output, "-1\n"); else if ((ret = net_gethostbyaddr(&ip, &name)) != 0) { diff --git a/src/doveadm/client-connection-tcp.c b/src/doveadm/client-connection-tcp.c index 0f84525abe..40e582a19a 100644 --- a/src/doveadm/client-connection-tcp.c +++ b/src/doveadm/client-connection-tcp.c @@ -446,7 +446,7 @@ client_connection_tcp_authenticate(struct client_connection_tcp *conn) /* FIXME: some day we should probably let auth process do this and support all kinds of authentication */ - if (strncmp(line, "PLAIN\t", 6) != 0) { + if (!str_begins(line, "PLAIN\t")) { i_error("doveadm client attempted non-PLAIN authentication: %s", line); return -1; } diff --git a/src/doveadm/doveadm-auth-server.c b/src/doveadm/doveadm-auth-server.c index e8da5d0a33..44278e467e 100644 --- a/src/doveadm/doveadm-auth-server.c +++ b/src/doveadm/doveadm-auth-server.c @@ -142,18 +142,18 @@ cmd_user_input(struct auth_master_connection *conn, static void auth_user_info_parse(struct auth_user_info *info, const char *arg) { - if (strncmp(arg, "service=", 8) == 0) + if (str_begins(arg, "service=")) info->service = arg + 8; - else if (strncmp(arg, "lip=", 4) == 0) { + else if (str_begins(arg, "lip=")) { if (net_addr2ip(arg + 4, &info->local_ip) < 0) i_fatal("lip: Invalid ip"); - } else if (strncmp(arg, "rip=", 4) == 0) { + } else if (str_begins(arg, "rip=")) { if (net_addr2ip(arg + 4, &info->remote_ip) < 0) i_fatal("rip: Invalid ip"); - } else if (strncmp(arg, "lport=", 6) == 0) { + } else if (str_begins(arg, "lport=")) { if (net_str2port(arg + 6, &info->local_port) < 0) i_fatal("lport: Invalid port number"); - } else if (strncmp(arg, "rport=", 6) == 0) { + } else if (str_begins(arg, "rport=")) { if (net_str2port(arg + 6, &info->remote_port) < 0) i_fatal("rport: Invalid port number"); } else { diff --git a/src/doveadm/doveadm-auth.c b/src/doveadm/doveadm-auth.c index d35b3487a5..d5a5edafa7 100644 --- a/src/doveadm/doveadm-auth.c +++ b/src/doveadm/doveadm-auth.c @@ -216,18 +216,18 @@ cmd_auth_input(const char *auth_socket_path, struct authtest_input *input) static void auth_user_info_parse(struct auth_user_info *info, const char *arg) { - if (strncmp(arg, "service=", 8) == 0) + if (str_begins(arg, "service=")) info->service = arg + 8; - else if (strncmp(arg, "lip=", 4) == 0) { + else if (str_begins(arg, "lip=")) { if (net_addr2ip(arg + 4, &info->local_ip) < 0) i_fatal("lip: Invalid ip"); - } else if (strncmp(arg, "rip=", 4) == 0) { + } else if (str_begins(arg, "rip=")) { if (net_addr2ip(arg + 4, &info->remote_ip) < 0) i_fatal("rip: Invalid ip"); - } else if (strncmp(arg, "lport=", 6) == 0) { + } else if (str_begins(arg, "lport=")) { if (net_str2port(arg + 6, &info->local_port) < 0) i_fatal("lport: Invalid port number"); - } else if (strncmp(arg, "rport=", 6) == 0) { + } else if (str_begins(arg, "rport=")) { if (net_str2port(arg + 6, &info->remote_port) < 0) i_fatal("rport: Invalid port number"); } else { diff --git a/src/doveadm/doveadm-dsync.c b/src/doveadm/doveadm-dsync.c index 25dce789bf..d60b130564 100644 --- a/src/doveadm/doveadm-dsync.c +++ b/src/doveadm/doveadm-dsync.c @@ -891,23 +891,23 @@ parse_location(struct dsync_cmd_context *ctx, { struct doveadm_cmd_context *cctx = ctx->ctx.cctx; - if (strncmp(location, "tcp:", 4) == 0) { + if (str_begins(location, "tcp:")) { /* TCP connection to remote dsync */ ctx->remote_name = location+4; return dsync_connect_tcp(ctx, mail_set, ctx->remote_name, FALSE, error_r); } - if (strncmp(location, "tcps:", 5) == 0) { + if (str_begins(location, "tcps:")) { /* TCP+SSL connection to remote dsync */ ctx->remote_name = location+5; return dsync_connect_tcp(ctx, mail_set, ctx->remote_name, TRUE, error_r); } - if (strncmp(location, "remote:", 7) == 0) { + if (str_begins(location, "remote:")) { /* this is a remote (ssh) command */ ctx->remote_name = location+7; - } else if (strncmp(location, "remoteprefix:", 13) == 0) { + } else if (str_begins(location, "remoteprefix:")) { /* this is a remote (ssh) command with a "user\n" prefix sent before dsync actually starts */ ctx->remote_name = location+13; @@ -1380,7 +1380,7 @@ void doveadm_dsync_main(int *_argc, char **_argv[]) i_fatal("Invalid parameter: %s", argv[src]); src++; dest++; - if (src < argc && strncmp(argv[src], "-E", 2) == 0) { + if (src < argc && str_begins(argv[src], "-E")) { /* we're re-executing dsync due to doveconf */ return; } diff --git a/src/doveadm/doveadm-dump-dbox.c b/src/doveadm/doveadm-dump-dbox.c index 7a4b7ebb0b..fd67e1a6ac 100644 --- a/src/doveadm/doveadm-dump-dbox.c +++ b/src/doveadm/doveadm-dump-dbox.c @@ -221,7 +221,7 @@ static bool test_dump_dbox(const char *path) p = path; else p++; - return strncmp(p, "m.", 2) == 0 || strncmp(p, "u.", 2) == 0; + return str_begins(p, "m.") || str_begins(p, "u."); } struct doveadm_cmd_dump doveadm_cmd_dump_dbox = { diff --git a/src/doveadm/doveadm-mail-fetch.c b/src/doveadm/doveadm-mail-fetch.c index 4fd4816dc2..71ba2ec506 100644 --- a/src/doveadm/doveadm-mail-fetch.c +++ b/src/doveadm/doveadm-mail-fetch.c @@ -206,7 +206,7 @@ static int fetch_body_field(struct fetch_cmd_context *ctx) bool binary; int ret; - binary = strncmp(name, "binary.", 7) == 0; + binary = str_begins(name, "binary."); name += binary ? 7 : 5; if (imap_msgpart_parse(name, &msgpart) < 0) i_unreached(); /* we already verified this was ok */ @@ -549,15 +549,15 @@ static void parse_fetch_fields(struct fetch_cmd_context *ctx, const char *str) if ((field = fetch_field_find(name)) != NULL) { ctx->wanted_fields |= field->wanted_fields; array_append(&ctx->fields, field, 1); - } else if (strncmp(name, "hdr.", 4) == 0) { + } else if (str_begins(name, "hdr.")) { name += 4; hdr_field.name = name; array_append(&ctx->fields, &hdr_field, 1); name = t_strcut(name, '.'); array_append(&ctx->header_fields, &name, 1); - } else if (strncmp(name, "body.", 5) == 0 || - strncmp(name, "binary.", 7) == 0) { - bool binary = strncmp(name, "binary.", 7) == 0; + } else if (str_begins(name, "body.") || + str_begins(name, "binary.")) { + bool binary = str_begins(name, "binary."); body_field.name = t_strarray_join(t_strsplit(name, ","), " "); name += binary ? 7 : 5; diff --git a/src/doveadm/doveadm-mail-mailbox-metadata.c b/src/doveadm/doveadm-mail-mailbox-metadata.c index 1583c07739..40da3266f8 100644 --- a/src/doveadm/doveadm-mail-mailbox-metadata.c +++ b/src/doveadm/doveadm-mail-mailbox-metadata.c @@ -104,10 +104,10 @@ cmd_mailbox_metadata_parse_key(const char *arg, { arg = t_str_lcase(arg); - if (strncmp(arg, "/private/", 9) == 0) { + if (str_begins(arg, "/private/")) { *type_r = MAIL_ATTRIBUTE_TYPE_PRIVATE; *key_r = arg + 9; - } else if (strncmp(arg, "/shared/", 8) == 0) { + } else if (str_begins(arg, "/shared/")) { *type_r = MAIL_ATTRIBUTE_TYPE_SHARED; *key_r = arg + 8; } else if (strcmp(arg, "/private") == 0) { diff --git a/src/doveadm/doveadm-mail-server.c b/src/doveadm/doveadm-mail-server.c index d4f8b30735..d021584458 100644 --- a/src/doveadm/doveadm-mail-server.c +++ b/src/doveadm/doveadm-mail-server.c @@ -224,18 +224,18 @@ doveadm_mail_server_user_get_host(struct doveadm_mail_cmd_context *ctx, proxy_host = NULL; proxy_hostip = NULL; proxying = FALSE; proxy_port = ctx->set->doveadm_port; for (i = 0; fields[i] != NULL; i++) { - if (strncmp(fields[i], "proxy", 5) == 0 && + if (str_begins(fields[i], "proxy") && (fields[i][5] == '\0' || fields[i][5] == '=')) proxying = TRUE; - else if (strncmp(fields[i], "host=", 5) == 0) + else if (str_begins(fields[i], "host=")) proxy_host = fields[i]+5; - else if (strncmp(fields[i], "hostip=", 7) == 0) + else if (str_begins(fields[i], "hostip=")) proxy_hostip = fields[i]+7; - else if (strncmp(fields[i], "user=", 5) == 0) + else if (str_begins(fields[i], "user=")) *user_r = t_strdup(fields[i]+5); - else if (strncmp(fields[i], "destuser=", 9) == 0) + else if (str_begins(fields[i], "destuser=")) *user_r = t_strdup(fields[i]+9); - else if (strncmp(fields[i], "port=", 5) == 0) { + else if (str_begins(fields[i], "port=")) { if (net_str2port(fields[i]+5, &proxy_port) < 0) proxy_port = 0; } diff --git a/src/doveadm/doveadm-oldstats.c b/src/doveadm/doveadm-oldstats.c index f3fea81dc1..04c45d9677 100644 --- a/src/doveadm/doveadm-oldstats.c +++ b/src/doveadm/doveadm-oldstats.c @@ -529,7 +529,7 @@ static void stats_reset(const char *path, const char **items ATTR_UNUSED) if (line == NULL) { i_error("read(%s) failed: %s", path, i_stream_get_error(input)); - } else if (strncmp(line, "OK", 2) != 0) { + } else if (!str_begins(line, "OK")) { i_error("%s",line); } else { i_info("Stats reset"); diff --git a/src/doveadm/doveadm-proxy.c b/src/doveadm/doveadm-proxy.c index e1e83e7404..d2e76c0d69 100644 --- a/src/doveadm/doveadm-proxy.c +++ b/src/doveadm/doveadm-proxy.c @@ -62,7 +62,7 @@ static void cmd_proxy_list_header(const char *const *args) const char *arg = args[i]; if (strcmp(arg, "username") == 0 || - strncmp(arg, "user_", 5) == 0) { + str_begins(arg, "user_")) { doveadm_print_header(arg, arg, DOVEADM_PRINT_HEADER_FLAG_EXPAND); continue; diff --git a/src/doveadm/doveadm-util.c b/src/doveadm/doveadm-util.c index b8d90ae2e7..a65ef7f723 100644 --- a/src/doveadm/doveadm-util.c +++ b/src/doveadm/doveadm-util.c @@ -64,7 +64,7 @@ bool doveadm_has_unloaded_plugin(const char *name) while ((d = readdir(dir)) != NULL) { plugin_name = module_file_get_name(d->d_name); - if (strncmp(plugin_name, "doveadm_", 8) == 0) + if (str_begins(plugin_name, "doveadm_")) plugin_name += 8; if (strncmp(plugin_name, name, name_len) == 0 && diff --git a/src/doveadm/doveadm-zlib.c b/src/doveadm/doveadm-zlib.c index a2144d6921..e82dc19fec 100644 --- a/src/doveadm/doveadm-zlib.c +++ b/src/doveadm/doveadm-zlib.c @@ -61,7 +61,7 @@ static void cmd_dump_imapzlib(int argc ATTR_UNUSED, char *argv[]) continue; line++; - if (strncmp(line, "OK Begin compression", 20) == 0 || + if (str_begins(line, "OK Begin compression") || strcasecmp(line, "COMPRESS DEFLATE") == 0) break; } diff --git a/src/doveadm/server-connection.c b/src/doveadm/server-connection.c index e5b9930f53..fa83187c0a 100644 --- a/src/doveadm/server-connection.c +++ b/src/doveadm/server-connection.c @@ -366,7 +366,7 @@ static void server_connection_input(struct server_connection *conn) because v2.2.33 sent the version after and newer versions send before. */ if (!conn->version_received && - strncmp(line, "VERSION\t", 8) == 0) { + str_begins(line, "VERSION\t")) { if (!version_string_verify_full(line, "doveadm-client", DOVEADM_SERVER_PROTOCOL_VERSION_MAJOR, &conn->minor)) { diff --git a/src/imap-hibernate/imap-client.c b/src/imap-hibernate/imap-client.c index 0db3fc0eab..f336bdb476 100644 --- a/src/imap-hibernate/imap-client.c +++ b/src/imap-hibernate/imap-client.c @@ -112,7 +112,7 @@ imap_client_parse_userdb_fields(struct imap_client *client, field = t_strsplit_tabescaped(client->state.userdb_fields); for (i = 0; field[i] != NULL; i++) { - if (strncmp(field[i], "auth_user=", 10) == 0) + if (str_begins(field[i], "auth_user=")) *auth_user_r = field[i] + 10; } } diff --git a/src/imap-login/imap-proxy.c b/src/imap-login/imap-proxy.c index 6900d77e9c..768426644d 100644 --- a/src/imap-login/imap-proxy.c +++ b/src/imap-login/imap-proxy.c @@ -174,7 +174,7 @@ static int proxy_input_banner(struct imap_client *client, string_t *str; int ret; - if (strncmp(line, "* OK ", 5) != 0) { + if (!str_begins(line, "* OK ")) { client_log_err(&client->common, t_strdup_printf( "proxy: Remote returned invalid banner: %s", str_sanitize(line, 160))); @@ -182,7 +182,7 @@ static int proxy_input_banner(struct imap_client *client, } str = t_str_new(128); - if (strncmp(line + 5, "[CAPABILITY ", 12) == 0) { + if (str_begins(line + 5, "[CAPABILITY ")) { capabilities = t_strsplit(t_strcut(line + 5 + 12, ']'), " "); if (str_array_icase_find(capabilities, "SASL-IR")) client->proxy_sasl_ir = TRUE; @@ -310,11 +310,11 @@ int imap_proxy_parse_line(struct client *client, const char *line) imap_client->proxy_sent_state |= IMAP_PROXY_SENT_STATE_AUTH_CONTINUE; o_stream_nsend(output, str_data(str), str_len(str)); return 0; - } else if (strncmp(line, "S ", 2) == 0) { + } else if (str_begins(line, "S ")) { imap_client->proxy_sent_state &= ~IMAP_PROXY_SENT_STATE_STARTTLS; imap_client->proxy_rcvd_state = IMAP_PROXY_RCVD_STATE_STARTTLS; - if (strncmp(line, "S OK ", 5) != 0) { + if (!str_begins(line, "S OK ")) { /* STARTTLS failed */ client_log_err(client, t_strdup_printf( "proxy: Remote STARTTLS failed: %s", @@ -336,7 +336,7 @@ int imap_proxy_parse_line(struct client *client, const char *line) } o_stream_nsend(output, str_data(str), str_len(str)); return 1; - } else if (strncmp(line, "L OK ", 5) == 0) { + } else if (str_begins(line, "L OK ")) { /* Login successful. Send this line to client. */ imap_client->proxy_sent_state &= ~IMAP_PROXY_SENT_STATE_LOGIN; imap_client->proxy_rcvd_state = IMAP_PROXY_RCVD_STATE_LOGIN; @@ -346,7 +346,7 @@ int imap_proxy_parse_line(struct client *client, const char *line) client_proxy_finish_destroy_client(client); return 1; - } else if (strncmp(line, "L ", 2) == 0) { + } else if (str_begins(line, "L ")) { imap_client->proxy_sent_state &= ~IMAP_PROXY_SENT_STATE_LOGIN; imap_client->proxy_rcvd_state = IMAP_PROXY_RCVD_STATE_LOGIN; @@ -369,7 +369,7 @@ int imap_proxy_parse_line(struct client *client, const char *line) client_send_reply_code(client, IMAP_CMD_REPLY_NO, IMAP_RESP_CODE_AUTHFAILED, AUTH_FAILED_MSG); - } else if (strncmp(line, "NO [", 4) == 0) { + } else if (str_begins(line, "NO [")) { /* remote sent some other resp-code. forward it. */ client_send_raw(client, t_strconcat( imap_client->cmd_tag, " ", line, "\r\n", NULL)); @@ -393,11 +393,11 @@ int imap_proxy_parse_line(struct client *client, const char *line) i_free(imap_client->proxy_backend_capability); imap_client->proxy_backend_capability = i_strdup(line + 13); return 0; - } else if (strncmp(line, "C ", 2) == 0) { + } else if (str_begins(line, "C ")) { /* Reply to CAPABILITY command we sent */ imap_client->proxy_sent_state &= ~IMAP_PROXY_SENT_STATE_CAPABILITY; imap_client->proxy_rcvd_state = IMAP_PROXY_RCVD_STATE_CAPABILITY; - if (strncmp(line, "C OK ", 5) == 0 && + if (str_begins(line, "C OK ") && client->proxy_password != NULL) { /* pipelining was disabled, send the login now. */ str = t_str_new(128); @@ -429,7 +429,7 @@ int imap_proxy_parse_line(struct client *client, const char *line) } else if (strncasecmp(line, "* ID ", 5) == 0) { /* Reply to ID command we sent, ignore it */ return 0; - } else if (strncmp(line, "* ", 2) == 0) { + } else if (str_begins(line, "* ")) { /* untagged reply. just forward it. */ client_send_raw(client, t_strconcat(line, "\r\n", NULL)); return 0; diff --git a/src/imap-urlauth/imap-urlauth.c b/src/imap-urlauth/imap-urlauth.c index 9f9d12b692..70bc67a2c7 100644 --- a/src/imap-urlauth/imap-urlauth.c +++ b/src/imap-urlauth/imap-urlauth.c @@ -156,7 +156,7 @@ login_client_connected(const struct master_login_client *client, fields = array_get(&reply.extra_fields, &count); for (i = 0; i < count; i++) { - if (strncmp(fields[i], "client_service=", 15) == 0) { + if (str_begins(fields[i], "client_service=")) { service = fields[i] + 15; break; } diff --git a/src/imap/cmd-notify.c b/src/imap/cmd-notify.c index 494139e1dd..bc74122c96 100644 --- a/src/imap/cmd-notify.c +++ b/src/imap/cmd-notify.c @@ -157,7 +157,7 @@ cmd_notify_add_mailbox(struct imap_notify_context *ctx, char ns_sep = mail_namespace_get_sep(ns); if ((ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 && - strncmp(name, "INBOX", 5) != 0 && + !str_begins(name, "INBOX") && strncasecmp(name, "INBOX", 5) == 0 && (name[5] == '\0' || name[5] == ns_sep)) { /* we'll do only case-sensitive comparisons later, diff --git a/src/imap/imap-fetch-body.c b/src/imap/imap-fetch-body.c index cdb967075d..4714371cf2 100644 --- a/src/imap/imap-fetch-body.c +++ b/src/imap/imap-fetch-body.c @@ -315,12 +315,12 @@ bool imap_fetch_body_section_init(struct imap_fetch_init_context *ctx) unsigned int list_count; const char *str, *p, *error; - i_assert(strncmp(ctx->name, "BODY", 4) == 0); + i_assert(str_begins(ctx->name, "BODY")); p = ctx->name + 4; body = p_new(ctx->pool, struct imap_fetch_body_data, 1); - if (strncmp(p, ".PEEK", 5) == 0) + if (str_begins(p, ".PEEK")) p += 5; else ctx->fetch_ctx->flags_update_seen = TRUE; @@ -380,17 +380,17 @@ bool imap_fetch_binary_init(struct imap_fetch_init_context *ctx) unsigned int list_count; const char *str, *p, *error; - i_assert(strncmp(ctx->name, "BINARY", 6) == 0); + i_assert(str_begins(ctx->name, "BINARY")); p = ctx->name + 6; body = p_new(ctx->pool, struct imap_fetch_body_data, 1); body->binary = TRUE; - if (strncmp(p, ".SIZE", 5) == 0) { + if (str_begins(p, ".SIZE")) { /* fetch decoded size of the section */ p += 5; body->binary_size = TRUE; - } else if (strncmp(p, ".PEEK", 5) == 0) { + } else if (str_begins(p, ".PEEK")) { p += 5; } else { ctx->fetch_ctx->flags_update_seen = TRUE; diff --git a/src/imap/imap-sync.c b/src/imap/imap-sync.c index dbaacdfd0f..ad5d471f6a 100644 --- a/src/imap/imap-sync.c +++ b/src/imap/imap-sync.c @@ -237,7 +237,7 @@ imap_sync_send_highestmodseq(struct imap_sync_context *ctx, /* no sending */ } else if (sync_cmd->sync != NULL && /* IDLE doesn't have ->sync */ sync_cmd->sync->tagline != NULL && /* NOTIFY doesn't have tagline */ - strncmp(sync_cmd->sync->tagline, "OK ", 3) == 0 && + str_begins(sync_cmd->sync->tagline, "OK ") && sync_cmd->sync->tagline[3] != '[') { /* modify the tagged reply directly */ sync_cmd->sync->tagline = p_strdup_printf(sync_cmd->pool, diff --git a/src/lib-auth/auth-client-request.c b/src/lib-auth/auth-client-request.c index f6d0290a13..d06befba74 100644 --- a/src/lib-auth/auth-client-request.c +++ b/src/lib-auth/auth-client-request.c @@ -231,7 +231,7 @@ void auth_client_request_server_input(struct auth_client_request *request, switch (status) { case AUTH_REQUEST_STATUS_OK: for (tmp = args; *tmp != NULL; tmp++) { - if (strncmp(*tmp, "resp=", 5) == 0) { + if (str_begins(*tmp, "resp=")) { base64_data = *tmp + 5; break; } diff --git a/src/lib-auth/auth-master.c b/src/lib-auth/auth-master.c index c3763196a2..e92c40c52b 100644 --- a/src/lib-auth/auth-master.c +++ b/src/lib-auth/auth-master.c @@ -209,7 +209,7 @@ static bool auth_lookup_reply_callback(const char *cmd, const char *const *args, /* put the reason string into first field */ ctx->fields = p_new(ctx->pool, const char *, 2); for (i = 0; i < len; i++) { - if (strncmp(args[i], "reason=", 7) == 0) { + if (str_begins(args[i], "reason=")) { ctx->fields[0] = p_strdup(ctx->pool, args[i] + 7); break; @@ -526,15 +526,15 @@ void auth_user_fields_parse(const char *const *fields, pool_t pool, p_array_init(&reply_r->extra_fields, pool, 64); for (; *fields != NULL; fields++) { - if (strncmp(*fields, "uid=", 4) == 0) { + if (str_begins(*fields, "uid=")) { if (str_to_uid(*fields + 4, &reply_r->uid) < 0) i_error("Invalid uid in reply"); - } else if (strncmp(*fields, "gid=", 4) == 0) { + } else if (str_begins(*fields, "gid=")) { if (str_to_gid(*fields + 4, &reply_r->gid) < 0) i_error("Invalid gid in reply"); - } else if (strncmp(*fields, "home=", 5) == 0) + } else if (str_begins(*fields, "home=")) reply_r->home = p_strdup(pool, *fields + 5); - else if (strncmp(*fields, "chroot=", 7) == 0) + else if (str_begins(*fields, "chroot=")) reply_r->chroot = p_strdup(pool, *fields + 7); else if (strcmp(*fields, "anonymous") == 0) reply_r->anonymous = TRUE; diff --git a/src/lib-auth/auth-server-connection.c b/src/lib-auth/auth-server-connection.c index 7eea061cad..63f02b9fac 100644 --- a/src/lib-auth/auth-server-connection.c +++ b/src/lib-auth/auth-server-connection.c @@ -262,7 +262,7 @@ static void auth_server_connection_input(struct auth_server_connection *conn) return; /* make sure the major version matches */ - if (strncmp(line, "VERSION\t", 8) != 0 || + if (!str_begins(line, "VERSION\t") || !str_uint_equals(t_strcut(line + 8, '\t'), AUTH_CLIENT_PROTOCOL_MAJOR_VERSION)) { i_error("Authentication server not compatible with " diff --git a/src/lib-dcrypt/dcrypt-openssl.c b/src/lib-dcrypt/dcrypt-openssl.c index 28a72cf1a8..c2dbd301e5 100644 --- a/src/lib-dcrypt/dcrypt-openssl.c +++ b/src/lib-dcrypt/dcrypt-openssl.c @@ -2046,24 +2046,24 @@ dcrypt_openssl_key_string_get_info( i_assert(key_data != NULL); /* is it PEM key */ - if (strncmp(key_data, "-----BEGIN ", 11) == 0) { + if (str_begins(key_data, "-----BEGIN ")) { format = DCRYPT_FORMAT_PEM; version = DCRYPT_KEY_VERSION_NA; key_data += 11; - if (strncmp(key_data, "RSA ", 4) == 0) { + if (str_begins(key_data, "RSA ")) { if (error_r != NULL) *error_r = "RSA private key format not " "supported, convert it to PKEY format " "with openssl pkey"; return FALSE; } - if (strncmp(key_data, "ENCRYPTED ", 10) == 0) { + if (str_begins(key_data, "ENCRYPTED ")) { encryption_type = DCRYPT_KEY_ENCRYPTION_TYPE_PASSWORD; key_data += 10; } - if (strncmp(key_data, "PRIVATE KEY-----", 16) == 0) + if (str_begins(key_data, "PRIVATE KEY-----")) kind = DCRYPT_KEY_KIND_PRIVATE; - else if (strncmp(key_data, "PUBLIC KEY-----", 15) == 0) + else if (str_begins(key_data, "PUBLIC KEY-----")) kind = DCRYPT_KEY_KIND_PUBLIC; else { if (error_r != NULL) @@ -2071,12 +2071,12 @@ dcrypt_openssl_key_string_get_info( return FALSE; } } else { - if (strncmp(key_data, "1:", 2) == 0) { + if (str_begins(key_data, "1:")) { if (error_r != NULL) *error_r = "Dovecot v1 key format " "uses tab to separate fields"; return FALSE; - } else if (strncmp(key_data, "2\t", 2) == 0) { + } else if (str_begins(key_data, "2\t")) { if (error_r != NULL) *error_r = "Dovecot v2 key format uses " "colon to separate fields"; diff --git a/src/lib-dict-backend/dict-sql-settings.c b/src/lib-dict-backend/dict-sql-settings.c index 957b6463d5..c80bd85a6c 100644 --- a/src/lib-dict-backend/dict-sql-settings.c +++ b/src/lib-dict-backend/dict-sql-settings.c @@ -223,17 +223,17 @@ parse_setting(const char *key, const char *value, field = array_append_space(&ctx->cur_fields); field->sql_field.name = p_strdup(ctx->pool, key); value_len = strlen(value); - if (strncmp(value, "${hexblob:", 10) == 0 && + if (str_begins(value, "${hexblob:") && value[value_len-1] == '}') { field->variable = p_strndup(ctx->pool, value + 10, value_len-10-1); field->sql_field.value_type = DICT_SQL_TYPE_HEXBLOB; - } else if (strncmp(value, "${int:", 6) == 0 && + } else if (str_begins(value, "${int:") && value[value_len-1] == '}') { field->variable = p_strndup(ctx->pool, value + 6, value_len-6-1); field->sql_field.value_type = DICT_SQL_TYPE_INT; - } else if (strncmp(value, "${uint:", 7) == 0 && + } else if (str_begins(value, "${uint:") && value[value_len-1] == '}') { field->variable = p_strndup(ctx->pool, value + 7, value_len-7-1); diff --git a/src/lib-dict/dict-client.c b/src/lib-dict/dict-client.c index cc945c573a..f4d6d84b87 100644 --- a/src/lib-dict/dict-client.c +++ b/src/lib-dict/dict-client.c @@ -721,7 +721,7 @@ client_dict_init(struct dict *driver, const char *uri, /* uri = [idle_msecs=:] [warn_slow_msecs=:] [] ":" */ for (;;) { - if (strncmp(uri, "idle_msecs=", 11) == 0) { + if (str_begins(uri, "idle_msecs=")) { p = strchr(uri+11, ':'); if (p == NULL) { *error_r = t_strdup_printf("Invalid URI: %s", uri); @@ -732,7 +732,7 @@ client_dict_init(struct dict *driver, const char *uri, return -1; } uri = p+1; - } else if (strncmp(uri, "warn_slow_msecs=", 16) == 0) { + } else if (str_begins(uri, "warn_slow_msecs=")) { p = strchr(uri+11, ':'); if (p == NULL) { *error_r = t_strdup_printf("Invalid URI: %s", uri); diff --git a/src/lib-dict/dict-memcached-ascii.c b/src/lib-dict/dict-memcached-ascii.c index 4fd540020f..30e4f531da 100644 --- a/src/lib-dict/dict-memcached-ascii.c +++ b/src/lib-dict/dict-memcached-ascii.c @@ -168,7 +168,7 @@ static int memcached_ascii_input_reply_read(struct memcached_ascii_dict *dict, case MEMCACHED_INPUT_STATE_GET: /* VALUE END */ - if (strncmp(line, "VALUE ", 6) == 0) { + if (str_begins(line, "VALUE ")) { p = strrchr(line, ' '); if (str_to_uint(p+1, &conn->reply_bytes_left) < 0) break; @@ -398,22 +398,22 @@ memcached_ascii_dict_init(struct dict *driver, const char *uri, args = t_strsplit(uri, ":"); for (; *args != NULL; args++) { - if (strncmp(*args, "host=", 5) == 0) { + if (str_begins(*args, "host=")) { if (net_addr2ip(*args+5, &dict->ip) < 0) { *error_r = t_strdup_printf("Invalid IP: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "port=", 5) == 0) { + } else if (str_begins(*args, "port=")) { if (net_str2port(*args+5, &dict->port) < 0) { *error_r = t_strdup_printf("Invalid port: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "prefix=", 7) == 0) { + } else if (str_begins(*args, "prefix=")) { i_free(dict->key_prefix); dict->key_prefix = i_strdup(*args + 7); - } else if (strncmp(*args, "timeout_msecs=", 14) == 0) { + } else if (str_begins(*args, "timeout_msecs=")) { if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) { *error_r = t_strdup_printf( "Invalid timeout_msecs: %s", *args+14); diff --git a/src/lib-dict/dict-memcached.c b/src/lib-dict/dict-memcached.c index e7a27f4058..cea4a33b69 100644 --- a/src/lib-dict/dict-memcached.c +++ b/src/lib-dict/dict-memcached.c @@ -191,22 +191,22 @@ memcached_dict_init(struct dict *driver, const char *uri, args = t_strsplit(uri, ":"); for (; *args != NULL; args++) { - if (strncmp(*args, "host=", 5) == 0) { + if (str_begins(*args, "host=")) { if (net_addr2ip(*args+5, &dict->ip) < 0) { *error_r = t_strdup_printf("Invalid IP: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "port=", 5) == 0) { + } else if (str_begins(*args, "port=")) { if (net_str2port(*args+5, &dict->port) < 0) { *error_r = t_strdup_printf("Invalid port: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "prefix=", 7) == 0) { + } else if (str_begins(*args, "prefix=")) { i_free(dict->key_prefix); dict->key_prefix = i_strdup(*args + 7); - } else if (strncmp(*args, "timeout_msecs=", 14) == 0) { + } else if (str_begins(*args, "timeout_msecs=")) { if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) { *error_r = t_strdup_printf( "Invalid timeout_msecs: %s", *args+14); diff --git a/src/lib-dict/dict-redis.c b/src/lib-dict/dict-redis.c index 14b4db0c48..97dc40e65a 100644 --- a/src/lib-dict/dict-redis.c +++ b/src/lib-dict/dict-redis.c @@ -376,30 +376,30 @@ redis_dict_init(struct dict *driver, const char *uri, args = t_strsplit(uri, ":"); for (; *args != NULL; args++) { - if (strncmp(*args, "path=", 5) == 0) { + if (str_begins(*args, "path=")) { unix_path = *args + 5; - } else if (strncmp(*args, "host=", 5) == 0) { + } else if (str_begins(*args, "host=")) { if (net_addr2ip(*args+5, &ip) < 0) { *error_r = t_strdup_printf("Invalid IP: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "port=", 5) == 0) { + } else if (str_begins(*args, "port=")) { if (net_str2port(*args+5, &port) < 0) { *error_r = t_strdup_printf("Invalid port: %s", *args+5); ret = -1; } - } else if (strncmp(*args, "prefix=", 7) == 0) { + } else if (str_begins(*args, "prefix=")) { i_free(dict->key_prefix); dict->key_prefix = i_strdup(*args + 7); - } else if (strncmp(*args, "db=", 3) == 0) { + } else if (str_begins(*args, "db=")) { if (str_to_uint(*args+3, &dict->db_id) < 0) { *error_r = t_strdup_printf( "Invalid db number: %s", *args+3); ret = -1; } - } else if (strncmp(*args, "expire_secs=", 12) == 0) { + } else if (str_begins(*args, "expire_secs=")) { const char *value = *args + 12; if (str_to_uint(value, &secs) < 0 || secs == 0) { @@ -409,13 +409,13 @@ redis_dict_init(struct dict *driver, const char *uri, } i_free(dict->expire_value); dict->expire_value = i_strdup(value); - } else if (strncmp(*args, "timeout_msecs=", 14) == 0) { + } else if (str_begins(*args, "timeout_msecs=")) { if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) { *error_r = t_strdup_printf( "Invalid timeout_msecs: %s", *args+14); ret = -1; } - } else if (strncmp(*args, "password=", 9) == 0) { + } else if (str_begins(*args, "password=")) { i_free(dict->password); dict->password = i_strdup(*args + 9); } else { diff --git a/src/lib-dns/dns-lookup.c b/src/lib-dns/dns-lookup.c index 4f444a80b4..c8c71d1da7 100644 --- a/src/lib-dns/dns-lookup.c +++ b/src/lib-dns/dns-lookup.c @@ -87,7 +87,7 @@ static int dns_lookup_input_line(struct dns_lookup *lookup, const char *line) if (result->ips_count == 0) { if (lookup->ptr_lookup) { /* [] */ - if (strncmp(line, "0 ", 2) == 0) { + if (str_begins(line, "0 ")) { result->name = lookup->name = i_strdup(line + 2); result->ret = 0; diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 167c1c3d72..34488f9afa 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -98,7 +98,7 @@ fs_posix_init(struct fs *_fs, const char *args, const struct fs_settings *set) fs->lock_method = FS_POSIX_LOCK_METHOD_FLOCK; else if (strcmp(arg, "lock=dotlock") == 0) fs->lock_method = FS_POSIX_LOCK_METHOD_DOTLOCK; - else if (strncmp(arg, "prefix=", 7) == 0) { + else if (str_begins(arg, "prefix=")) { i_free(fs->path_prefix); fs->path_prefix = i_strdup(arg + 7); } else if (strcmp(arg, "mode=auto") == 0) { @@ -107,9 +107,9 @@ fs_posix_init(struct fs *_fs, const char *args, const struct fs_settings *set) fs->have_dirs = TRUE; } else if (strcmp(arg, "no-fsync") == 0) { fs->disable_fsync = TRUE; - } else if (strcmp(arg, "accurate-mtime") == 0) { + } else if (str_begins(arg, "accurate-mtime") == 0) { fs->accurate_mtime = TRUE; - } else if (strncmp(arg, "mode=", 5) == 0) { + } else if (str_begins(arg, "mode=")) { unsigned int mode; if (str_to_uint_oct(arg+5, &mode) < 0) { fs_set_error(_fs, "Invalid mode value: %s", arg+5); diff --git a/src/lib-imap-storage/imap-msgpart.c b/src/lib-imap-storage/imap-msgpart.c index b6d3c31dfa..a69aa99118 100644 --- a/src/lib-imap-storage/imap-msgpart.c +++ b/src/lib-imap-storage/imap-msgpart.c @@ -264,16 +264,16 @@ int imap_msgpart_parse(const char *section, struct imap_msgpart **msgpart_r) /* body (for root or for message/rfc822) */ msgpart->fetch_type = FETCH_BODY; msgpart->wanted_fields |= MAIL_FETCH_STREAM_BODY; - } else if (strncmp(section, "HEADER", 6) == 0) { + } else if (str_begins(section, "HEADER")) { /* header (for root or for message/rfc822) */ if (section[6] == '\0') { msgpart->fetch_type = FETCH_HEADER; ret = 0; - } else if (strncmp(section, "HEADER.FIELDS.NOT", 17) == 0) { + } else if (str_begins(section, "HEADER.FIELDS.NOT")) { msgpart->fetch_type = FETCH_HEADER_FIELDS_NOT; ret = imap_msgpart_parse_header_fields(msgpart, section+17); - } else if (strncmp(section, "HEADER.FIELDS", 13) == 0) { + } else if (str_begins(section, "HEADER.FIELDS")) { msgpart->fetch_type = FETCH_HEADER_FIELDS; ret = imap_msgpart_parse_header_fields(msgpart, section+13); diff --git a/src/lib-imap/imap-base-subject.c b/src/lib-imap/imap-base-subject.c index 062cbe1864..02469d3dda 100644 --- a/src/lib-imap/imap-base-subject.c +++ b/src/lib-imap/imap-base-subject.c @@ -129,11 +129,11 @@ static bool remove_subj_leader(buffer_t *buf, size_t *start_pos, return ret; } - if (strncmp(data, "RE", 2) == 0) + if (str_begins(data, "RE")) data += 2; - else if (strncmp(data, "FWD", 3) == 0) + else if (str_begins(data, "FWD")) data += 3; - else if (strncmp(data, "FW", 2) == 0) + else if (str_begins(data, "FW")) data += 2; else return ret; @@ -178,7 +178,7 @@ static bool remove_subj_fwd_hdr(buffer_t *buf, size_t *start_pos, subj-fwd-hdr = "[fwd:" subj-fwd-trl = "]" */ - if (strncmp(data + *start_pos, "[FWD:", 5) != 0) + if (!str_begins(data + *start_pos, "[FWD:")) return FALSE; if (data[size-2] != ']') diff --git a/src/lib-index/mail-index.c b/src/lib-index/mail-index.c index bde6fb1b86..ef5c93ed85 100644 --- a/src/lib-index/mail-index.c +++ b/src/lib-index/mail-index.c @@ -1067,7 +1067,7 @@ void mail_index_file_set_syscall_error(struct mail_index *index, if (errno == EACCES) { function = t_strcut(function, '('); if (strcmp(function, "creat") == 0 || - strncmp(function, "file_dotlock_", 13) == 0) + str_begins(function, "file_dotlock_")) errstr = eacces_error_get_creating(function, filepath); else errstr = eacces_error_get(function, filepath); diff --git a/src/lib-mail/test-message-header-encode.c b/src/lib-mail/test-message-header-encode.c index 69a4cb22f7..ebcfedd1e1 100644 --- a/src/lib-mail/test-message-header-encode.c +++ b/src/lib-mail/test-message-header-encode.c @@ -11,13 +11,13 @@ static bool verify_q(const char *str, unsigned int i, bool starts_with_a) { unsigned int line_start = i, char_count = 0; - if (strncmp(str+i, "\n\t", 2) == 0) { + if (str_begins(str+i, "\n\t")) { i += 2; line_start = i - 1; } for (;;) { - if (strncmp(str+i, "=?utf-8?q?", 10) != 0) + if (!str_begins(str+i, "=?utf-8?q?")) return FALSE; i += 10; @@ -27,8 +27,8 @@ static bool verify_q(const char *str, unsigned int i, bool starts_with_a) starts_with_a = FALSE; i++; } - while (strncmp(str+i, "?=", 2) != 0) { - if (strncmp(str+i, "=C3=A4", 6) != 0) + while (!str_begins(str+i, "?=")) { + if (!str_begins(str+i, "=C3=A4")) return FALSE; i += 6; char_count++; @@ -39,7 +39,7 @@ static bool verify_q(const char *str, unsigned int i, bool starts_with_a) if (str[i] == '\0') break; - if (strncmp(str+i, "\n\t", 2) != 0) + if (!str_begins(str+i, "\n\t")) return FALSE; i += 2; line_start = i - 1; @@ -82,13 +82,13 @@ static bool verify_b(const char *str, unsigned int i, bool starts_with_a) buffer_t buf; buffer_create_from_data(&buf, bufdata, sizeof(bufdata)); - if (strncmp(str+i, "\n\t", 2) == 0) { + if (str_begins(str+i, "\n\t")) { i += 2; line_start = i - 1; } for (;;) { - if (strncmp(str+i, "=?utf-8?b?", 10) != 0) + if (!str_begins(str+i, "=?utf-8?b?")) return FALSE; i += 10; @@ -126,7 +126,7 @@ static bool verify_b(const char *str, unsigned int i, bool starts_with_a) if (str[i] == '\0') break; - if (strncmp(str+i, "\n\t", 2) != 0) + if (!str_begins(str+i, "\n\t")) return FALSE; i += 2; line_start = i - 1; diff --git a/src/lib-master/master-login-auth.c b/src/lib-master/master-login-auth.c index de0315ca68..f448332fee 100644 --- a/src/lib-master/master-login-auth.c +++ b/src/lib-master/master-login-auth.c @@ -281,7 +281,7 @@ master_login_auth_input_fail(struct master_login_auth *auth, return FALSE; } for (i = 1; args[i] != NULL; i++) { - if (strncmp(args[i], "reason=", 7) == 0) + if (str_begins(args[i], "reason=")) error = args[i] + 7; } @@ -329,7 +329,7 @@ static void master_login_auth_input(struct master_login_auth *auth) return; /* make sure the major version matches */ - if (strncmp(line, "VERSION\t", 8) != 0 || + if (!str_begins(line, "VERSION\t") || !str_uint_equals(t_strcut(line + 8, '\t'), AUTH_MASTER_PROTOCOL_MAJOR_VERSION)) { i_error("Authentication server not compatible with " @@ -344,7 +344,7 @@ static void master_login_auth_input(struct master_login_auth *auth) if (line == NULL) return; - if (strncmp(line, "SPID\t", 5) != 0 || + if (!str_begins(line, "SPID\t") || str_to_pid(line + 5, &auth->auth_server_pid) < 0) { i_error("Authentication server didn't " "send valid SPID as expected: %s", line); @@ -357,11 +357,11 @@ static void master_login_auth_input(struct master_login_auth *auth) auth->refcount++; while ((line = i_stream_next_line(auth->input)) != NULL) { - if (strncmp(line, "USER\t", 5) == 0) + if (str_begins(line, "USER\t")) ret = master_login_auth_input_user(auth, line + 5); - else if (strncmp(line, "NOTFOUND\t", 9) == 0) + else if (str_begins(line, "NOTFOUND\t")) ret = master_login_auth_input_notfound(auth, line + 9); - else if (strncmp(line, "FAIL\t", 5) == 0) + else if (str_begins(line, "FAIL\t")) ret = master_login_auth_input_fail(auth, line + 5); else ret = TRUE; diff --git a/src/lib-master/master-login.c b/src/lib-master/master-login.c index 370bc52bde..15c53e0b0c 100644 --- a/src/lib-master/master-login.c +++ b/src/lib-master/master-login.c @@ -346,7 +346,7 @@ static const char * auth_args_find_postlogin_socket(const char *const *auth_args) { for (unsigned int i = 0; auth_args[i] != NULL; i++) { - if (strncmp(auth_args[i], "postlogin=", 10) == 0) + if (str_begins(auth_args[i], "postlogin=")) return auth_args[i]+10; } return NULL; diff --git a/src/lib-master/master-service-settings-cache.c b/src/lib-master/master-service-settings-cache.c index 0e52b8dc49..3126f1127d 100644 --- a/src/lib-master/master-service-settings-cache.c +++ b/src/lib-master/master-service-settings-cache.c @@ -101,13 +101,13 @@ int master_service_settings_cache_init_filter(struct master_service_settings_cac struct config_filter *filter = p_new(cache->pool, struct config_filter, 1); while(*keys != NULL) { - if (strncmp(*keys, "local-net=", 10) == 0) { + if (str_begins(*keys, "local-net=")) { (void)net_parse_range((*keys)+10, &filter->local_ip, &filter->local_bits); - } else if (strncmp(*keys, "remote-net=", 11) == 0) { + } else if (str_begins(*keys, "remote-net=")) { (void)net_parse_range((*keys)+11, &filter->remote_ip, &filter->remote_bits); - } else if (strncmp(*keys, "local-name=", 11) == 0) { + } else if (str_begins(*keys, "local-name=")) { filter->local_name = p_strdup(cache->pool, (*keys)+11); } keys++; diff --git a/src/lib-master/master-service-settings.c b/src/lib-master/master-service-settings.c index 6feac9d2f7..14ee16197d 100644 --- a/src/lib-master/master-service-settings.c +++ b/src/lib-master/master-service-settings.c @@ -444,7 +444,7 @@ config_read_reply_header(struct istream *istream, const char *path, pool_t pool, output_r->used_local = TRUE; else if (strcmp(*arg, "used-remote") == 0) output_r->used_remote = TRUE; - else if (strncmp(*arg, "service=", 8) == 0) { + else if (str_begins(*arg, "service=")) { const char *name = p_strdup(pool, *arg + 8); array_append(&services, &name, 1); } @@ -515,7 +515,7 @@ int master_service_settings_get_filters(struct master_service *service, while((line = i_stream_read_next_line(is)) != NULL) { if (*line == '\0') break; - if (strncmp(line, "FILTER\t", 7) == 0) { + if (str_begins(line, "FILTER\t")) { line = t_strdup(line+7); array_append(&filters_tmp, &line, 1); } diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index 7cc9142721..225c90b936 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -1248,7 +1248,7 @@ bool version_string_verify_full(const char *line, const char *service_name, size_t service_name_len = strlen(service_name); bool ret; - if (strncmp(line, "VERSION\t", 8) != 0) + if (!str_begins(line, "VERSION\t")) return FALSE; line += 8; diff --git a/src/lib-program-client/program-client.c b/src/lib-program-client/program-client.c index 8762a67086..5c2f30bcb5 100644 --- a/src/lib-program-client/program-client.c +++ b/src/lib-program-client/program-client.c @@ -614,19 +614,19 @@ int program_client_create(const char *uri, const char *const *args, bool noreply, struct program_client **pc_r, const char **error_r) { - if (strncmp(uri, "exec:", 5) == 0) { + if (str_begins(uri, "exec:") == 0) { *pc_r = program_client_local_create( uri+5, args, set); return 0; - } else if (strncmp(uri, "unix:", 5) == 0) { + } else if (str_begins(uri, "unix:") == 0) { *pc_r = program_client_unix_create( uri+5, args, set, noreply); return 0; - } else if (strncmp(uri, "tcp:", 4) == 0) { + } else if (str_begins(uri, "tcp:")) { const char *host; in_port_t port; if (net_str2hostport(uri+4, 0, &host, &port) < 0 || port == 0) { diff --git a/src/lib-settings/settings-parser.c b/src/lib-settings/settings-parser.c index 1538ab6402..9980e223df 100644 --- a/src/lib-settings/settings-parser.c +++ b/src/lib-settings/settings-parser.c @@ -982,7 +982,7 @@ int settings_parse_stream(struct setting_parser_context *ctx, return 0; } ctx->linenum++; - if (ctx->linenum == 1 && strncmp(line, "ERROR ", 6) == 0) { + if (ctx->linenum == 1 && str_begins(line, "ERROR ")) { ctx->error = p_strdup(ctx->parser_pool, line + 6); return -1; } diff --git a/src/lib-smtp/test-smtp-client-errors.c b/src/lib-smtp/test-smtp-client-errors.c index 7e999062f0..19912477ce 100644 --- a/src/lib-smtp/test-smtp-client-errors.c +++ b/src/lib-smtp/test-smtp-client-errors.c @@ -2647,7 +2647,7 @@ server_connection_input(struct connection *_conn) conn->state = SERVER_CONNECTION_STATE_MAIL_FROM; return; case SERVER_CONNECTION_STATE_MAIL_FROM: - if (strncmp(line, "AUTH ", 5) == 0) { + if (str_begins(line, "AUTH ")) { o_stream_send_str(conn->conn.output, "235 2.7.0 " "Authentication successful\r\n"); diff --git a/src/lib-sql/driver-pgsql.c b/src/lib-sql/driver-pgsql.c index 1e99208e23..86400d38af 100644 --- a/src/lib-sql/driver-pgsql.c +++ b/src/lib-sql/driver-pgsql.c @@ -284,7 +284,7 @@ static struct sql_db *driver_pgsql_init_v(const char *connect_string) const char *const *arg = t_strsplit(connect_string, " "); for (; *arg != NULL; arg++) { - if (strncmp(*arg, "host=", 5) == 0) + if (str_begins(*arg, "host=")) db->host = i_strdup(*arg + 5); } } T_END; diff --git a/src/lib-storage/index/imapc/imapc-mail-fetch.c b/src/lib-storage/index/imapc/imapc-mail-fetch.c index 16af70933e..a71497d700 100644 --- a/src/lib-storage/index/imapc/imapc-mail-fetch.c +++ b/src/lib-storage/index/imapc/imapc-mail-fetch.c @@ -147,8 +147,8 @@ imapc_mail_try_merge_fetch(struct imapc_mailbox *mbox, string_t *str) const char *s2 = str_c(mbox->pending_fetch_cmd); const char *p1, *p2; - i_assert(strncmp(s1, "UID FETCH ", 10) == 0); - i_assert(strncmp(s2, "UID FETCH ", 10) == 0); + i_assert(str_begins(s1, "UID FETCH ")); + i_assert(str_begins(s2, "UID FETCH ")); /* skip over UID range */ p1 = strchr(s1+10, ' '); diff --git a/src/lib-storage/index/index-status.c b/src/lib-storage/index/index-status.c index e7b9906176..89edf5cb4d 100644 --- a/src/lib-storage/index/index-status.c +++ b/src/lib-storage/index/index-status.c @@ -234,7 +234,7 @@ static void get_metadata_precache_fields(struct mailbox *box, for (i = 0; i < count; i++) { const char *name = fields[i].name; - if (strncmp(name, "hdr.", 4) == 0 || + if (str_begins(name, "hdr.") || strcmp(name, "date.sent") == 0 || strcmp(name, "imap.envelope") == 0) cache |= MAIL_FETCH_STREAM_HEADER; diff --git a/src/lib-storage/index/index-storage.c b/src/lib-storage/index/index-storage.c index d6d364cf8f..71103cb40e 100644 --- a/src/lib-storage/index/index-storage.c +++ b/src/lib-storage/index/index-storage.c @@ -478,7 +478,7 @@ index_storage_mailbox_update_cache(struct mailbox *box, } if (j != old_count) { field = old_fields[j]; - } else if (strncmp(updates[i].name, "hdr.", 4) == 0) { + } else if (str_begins(updates[i].name, "hdr.")) { /* new header */ i_zero(&field); field.name = updates[i].name; diff --git a/src/lib-storage/index/maildir/maildir-sync-index.c b/src/lib-storage/index/maildir/maildir-sync-index.c index eda8447666..928d6b1044 100644 --- a/src/lib-storage/index/maildir/maildir-sync-index.c +++ b/src/lib-storage/index/maildir/maildir-sync-index.c @@ -204,7 +204,7 @@ static int maildir_handle_uid_insertion(struct maildir_index_sync_context *ctx, i_warning("Maildir %s: Expunged message reappeared, giving a new UID " "(old uid=%u, file=%s)%s", mailbox_get_path(&ctx->mbox->box), - uid, filename, strncmp(filename, "msg.", 4) != 0 ? "" : + uid, filename, !str_begins(filename, "msg.") ? "" : " (Your MDA is saving MH files into Maildir?)"); return 0; } diff --git a/src/lib-storage/index/mbox/mbox-save.c b/src/lib-storage/index/mbox/mbox-save.c index 5117309df4..a683544525 100644 --- a/src/lib-storage/index/mbox/mbox-save.c +++ b/src/lib-storage/index/mbox/mbox-save.c @@ -328,7 +328,7 @@ save_header_callback(struct header_filter_istream *input ATTR_UNUSED, bool *matched, struct mbox_save_context *ctx) { if (hdr != NULL) { - if (strncmp(hdr->name, "From ", 5) == 0) { + if (str_begins(hdr->name, "From ")) { /* we can't allow From_-lines in headers. there's no legitimate reason for allowing them in any case, so just drop them. */ diff --git a/src/lib-storage/index/pop3c/pop3c-mail.c b/src/lib-storage/index/pop3c/pop3c-mail.c index 19e42f7d1a..8e845eaa7d 100644 --- a/src/lib-storage/index/pop3c/pop3c-mail.c +++ b/src/lib-storage/index/pop3c/pop3c-mail.c @@ -193,9 +193,9 @@ pop3c_mail_get_stream(struct mail *_mail, bool get_body, if (get_body && mail->data.stream != NULL) { name = i_stream_get_name(mail->data.stream); - if (strncmp(name, "RETR", 4) == 0) { + if (str_begins(name, "RETR")) { /* we've fetched the body */ - } else if (strncmp(name, "TOP", 3) == 0) { + } else if (str_begins(name, "TOP")) { /* we've fetched the header, but we need the body now too */ index_mail_close_streams(mail); diff --git a/src/lib-storage/index/pop3c/pop3c-storage.c b/src/lib-storage/index/pop3c/pop3c-storage.c index 60c0816b3d..e6a57951ca 100644 --- a/src/lib-storage/index/pop3c/pop3c-storage.c +++ b/src/lib-storage/index/pop3c/pop3c-storage.c @@ -141,7 +141,7 @@ static void pop3c_login_callback(enum pop3c_command_state state, mbox->logged_in = TRUE; break; case POP3C_COMMAND_STATE_ERR: - if (strncmp(reply, "[IN-USE] ", 9) == 0) { + if (str_begins(reply, "[IN-USE] ")) { mail_storage_set_error(mbox->box.storage, MAIL_ERROR_INUSE, reply + 9); } else { diff --git a/src/lib-storage/list/mailbox-list-fs-flags.c b/src/lib-storage/list/mailbox-list-fs-flags.c index 45c0c127ac..3f3bd94eea 100644 --- a/src/lib-storage/list/mailbox-list-fs-flags.c +++ b/src/lib-storage/list/mailbox-list-fs-flags.c @@ -47,7 +47,7 @@ list_is_maildir_mailbox(struct mailbox_list *list, const char *dir, } } if (!S_ISDIR(st.st_mode)) { - if (strncmp(fname, ".nfs", 4) == 0) { + if (str_begins(fname, ".nfs")) { /* temporary NFS file */ *flags_r |= MAILBOX_NONEXISTENT; } else { @@ -186,7 +186,7 @@ int fs_list_get_mailbox_flags(struct mailbox_list *list, } if (!S_ISDIR(st.st_mode)) { - if (strncmp(fname, ".nfs", 4) == 0) { + if (str_begins(fname, ".nfs")) { /* temporary NFS file */ *flags_r |= MAILBOX_NONEXISTENT; return 0; diff --git a/src/lib-storage/list/mailbox-list-maildir-iter.c b/src/lib-storage/list/mailbox-list-maildir-iter.c index 923101a56c..28dafd7510 100644 --- a/src/lib-storage/list/mailbox-list-maildir-iter.c +++ b/src/lib-storage/list/mailbox-list-maildir-iter.c @@ -177,7 +177,7 @@ maildir_get_type(const char *dir, const char *fname, *type_r = MAILBOX_LIST_FILE_TYPE_DIR; return TRUE; } else { - if (strncmp(fname, ".nfs", 4) == 0) + if (str_begins(fname, ".nfs")) *flags |= MAILBOX_NONEXISTENT; else *flags |= MAILBOX_NOSELECT; @@ -202,7 +202,7 @@ int maildir_list_get_mailbox_flags(struct mailbox_list *list, /* need to check with stat() to be sure */ if (!list->mail_set->maildir_stat_dirs && *fname != '\0' && strcmp(list->name, MAILBOX_LIST_NAME_MAILDIRPLUSPLUS) == 0 && - strncmp(fname, ".nfs", 4) != 0) { + !str_begins(fname, ".nfs")) { /* just assume it's a valid mailbox */ return 1; } diff --git a/src/lib-storage/mail-namespace.c b/src/lib-storage/mail-namespace.c index 5bbeecccc5..1ea4ec78f1 100644 --- a/src/lib-storage/mail-namespace.c +++ b/src/lib-storage/mail-namespace.c @@ -688,7 +688,7 @@ static bool mail_namespace_is_usable_prefix(struct mail_namespace *ns, return TRUE; } - if (inbox && strncmp(ns->prefix, "INBOX", 5) == 0 && + if (inbox && str_begins(ns->prefix, "INBOX") && strncmp(ns->prefix+5, mailbox+5, ns->prefix_len-5) == 0) { /* we already checked that mailbox begins with case-insensitive INBOX. this namespace also begins with INBOX and the rest diff --git a/src/lib-storage/mail-search-register-imap.c b/src/lib-storage/mail-search-register-imap.c index 5d29466ed1..b64fb5f135 100644 --- a/src/lib-storage/mail-search-register-imap.c +++ b/src/lib-storage/mail-search-register-imap.c @@ -355,7 +355,7 @@ arg_modseq_set_ext(struct mail_search_build_context *ctx, const char *value; name = t_str_lcase(name); - if (strncmp(name, "/flags/", 7) != 0) + if (!str_begins(name, "/flags/")) return 0; name += 7; diff --git a/src/lib-storage/mail-storage-hooks.c b/src/lib-storage/mail-storage-hooks.c index 4856c6f390..1e2d31b018 100644 --- a/src/lib-storage/mail-storage-hooks.c +++ b/src/lib-storage/mail-storage-hooks.c @@ -118,9 +118,9 @@ mail_storage_module_hooks_cmp(const struct mail_storage_module_hooks *h1, p = strrchr(s2, '/'); if (p != NULL) s2 = p+1; - if (strncmp(s1, "lib", 3) == 0) + if (str_begins(s1, "lib")) s1 += 3; - if (strncmp(s2, "lib", 3) == 0) + if (str_begins(s2, "lib")) s2 += 3; return strcmp(s1, s2); diff --git a/src/lib-storage/mail-storage-service.c b/src/lib-storage/mail-storage-service.c index 0a27b745b9..38eab63b60 100644 --- a/src/lib-storage/mail-storage-service.c +++ b/src/lib-storage/mail-storage-service.c @@ -277,12 +277,12 @@ user_reply_handle(struct mail_storage_service_ctx *ctx, str = array_get(&reply->extra_fields, &count); for (i = 0; i < count; i++) { line = str[i]; - if (strncmp(line, "system_groups_user=", 19) == 0) { + if (str_begins(line, "system_groups_user=")) { user->system_groups_user = p_strdup(user->pool, line + 19); - } else if (strncmp(line, "chdir=", 6) == 0) { + } else if (str_begins(line, "chdir=")) { user->chdir_path = p_strdup(user->pool, line+6); - } else if (strncmp(line, "nice=", 5) == 0) { + } else if (str_begins(line, "nice=")) { #ifdef HAVE_SETPRIORITY int n; if (str_to_int(line + 5, &n) < 0) { @@ -293,11 +293,11 @@ user_reply_handle(struct mail_storage_service_ctx *ctx, i_error("setpriority(%d) failed: %m", n); } #endif - } else if (strncmp(line, "auth_token=", 11) == 0) { + } else if (str_begins(line, "auth_token=")) { user->auth_token = p_strdup(user->pool, line+11); - } else if (strncmp(line, "auth_user=", 10) == 0) { + } else if (str_begins(line, "auth_user=")) { user->auth_user = p_strdup(user->pool, line+10); - } else if (strncmp(line, "admin=", 6) == 0) { + } else if (str_begins(line, "admin=")) { user->admin = line[6] == 'y' || line[6] == 'Y' || line[6] == '1'; } else T_BEGIN { diff --git a/src/lib-storage/mail-storage-settings.c b/src/lib-storage/mail-storage-settings.c index 66121a8570..f846556b3c 100644 --- a/src/lib-storage/mail-storage-settings.c +++ b/src/lib-storage/mail-storage-settings.c @@ -540,7 +540,7 @@ static bool mail_storage_settings_check(void *_set, pool_t pool, set->parsed_mail_attachment_detection_add_flags_on_save = TRUE; } else if (strcmp(opt, "exclude-inlined") == 0) { set->parsed_mail_attachment_exclude_inlined = TRUE; - } else if (strncmp(opt, "content-type=", 13) == 0) { + } else if (str_begins(opt, "content-type=")) { const char *value = p_strdup(pool, opt+13); array_append(&content_types, &value, 1); } else { diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index 930d1d14af..b17ff61049 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -225,7 +225,7 @@ mail_storage_get_class(struct mail_namespace *ns, const char *driver, if (ns->set->location == NULL || *ns->set->location == '\0') { *error_r = t_strdup_printf( "Mail storage autodetection failed with home=%s", home); - } else if (strncmp(ns->set->location, "auto:", 5) == 0) { + } else if (str_begins(ns->set->location, "auto:")) { *error_r = t_strdup_printf( "Autodetection failed for %s (home=%s)", ns->set->location, home); @@ -816,7 +816,7 @@ struct mailbox *mailbox_alloc(struct mailbox_list *list, const char *vname, i_assert(uni_utf8_str_is_valid(vname)); if (strncasecmp(vname, "INBOX", 5) == 0 && - strncmp(vname, "INBOX", 5) != 0) { + !str_begins(vname, "INBOX")) { /* make sure INBOX shows up in uppercase everywhere. do this regardless of whether we're in inbox=yes namespace, because clients expect INBOX to be case insensitive regardless of @@ -826,7 +826,7 @@ struct mailbox *mailbox_alloc(struct mailbox_list *list, const char *vname, else if (vname[5] != mail_namespace_get_sep(list->ns)) /* not INBOX prefix */ ; else if (strncasecmp(list->ns->prefix, vname, 6) == 0 && - strncmp(list->ns->prefix, "INBOX", 5) != 0) { + !str_begins(list->ns->prefix, "INBOX")) { mailbox_list_set_critical(list, "Invalid server configuration: " "Namespace prefix=%s must be uppercase INBOX", diff --git a/src/lib-storage/mailbox-list.c b/src/lib-storage/mailbox-list.c index 45086f8a74..e395f90778 100644 --- a/src/lib-storage/mailbox-list.c +++ b/src/lib-storage/mailbox-list.c @@ -307,7 +307,7 @@ mailbox_list_settings_parse_full(struct mail_user *user, const char *data, *error_r = t_strconcat(error, "mail root dir in: ", data, NULL); return -1; } - if (strncmp(set_r->root_dir, "INBOX=", 6) == 0) { + if (str_begins(set_r->root_dir, "INBOX=")) { /* probably mbox user trying to avoid root_dir */ *error_r = t_strconcat("Mail root directory not given: ", data, NULL); diff --git a/src/lib-storage/test-mail-storage.c b/src/lib-storage/test-mail-storage.c index 5eb8520dbc..73169d0e6f 100644 --- a/src/lib-storage/test-mail-storage.c +++ b/src/lib-storage/test-mail-storage.c @@ -104,7 +104,7 @@ static void test_mail_storage_errors(void) test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL); test_assert(mail_error == MAIL_ERROR_TEMP); errstr = mail_storage_get_last_internal_error(&storage, &mail_error); - test_assert(strncmp(errstr, "critical3: ", 11) == 0); + test_assert(str_begins(errstr, "critical3: ")); test_assert(strstr(errstr+11, MAIL_ERRSTR_CRITICAL_MSG) != NULL); test_assert(mail_error == MAIL_ERROR_TEMP); test_assert(storage.last_error_is_internal); diff --git a/src/lib/iostream-rawlog.c b/src/lib/iostream-rawlog.c index 9ded6b4bc9..fcde45d5c9 100644 --- a/src/lib/iostream-rawlog.c +++ b/src/lib/iostream-rawlog.c @@ -170,7 +170,7 @@ iostream_rawlog_try_create_tcp(const char *path, int ret, fd; /* tcp:host:port */ - if (strncmp(path, "tcp:", 4) != 0) + if (!str_begins(path, "tcp:")) return 0; path += 4; diff --git a/src/lib/module-dir.c b/src/lib/module-dir.c index d55340c5f0..e26025971c 100644 --- a/src/lib/module-dir.c +++ b/src/lib/module-dir.c @@ -279,9 +279,9 @@ static int module_name_cmp(const char *const *n1, const char *const *n2) { const char *s1 = *n1, *s2 = *n2; - if (strncmp(s1, "lib", 3) == 0) + if (str_begins(s1, "lib")) s1 += 3; - if (strncmp(s2, "lib", 3) == 0) + if (str_begins(s2, "lib")) s2 += 3; return strcmp(s1, s2); @@ -662,7 +662,7 @@ const char *module_file_get_name(const char *fname) const char *p; /* [lib][nn_]name(.so) */ - if (strncmp(fname, "lib", 3) == 0) + if (str_begins(fname, "lib")) fname += 3; for (p = fname; *p != '\0'; p++) { diff --git a/src/lib/test-str-sanitize.c b/src/lib/test-str-sanitize.c index c2642e8576..d3c9716887 100644 --- a/src/lib/test-str-sanitize.c +++ b/src/lib/test-str-sanitize.c @@ -56,7 +56,7 @@ static void test_str_sanitize_max_bytes(void) str_append(str2, "1234567890"); str_sanitize_append(str2, tests[i].str, tests[i].max_len); - test_assert_idx(strncmp(str_c(str2), "1234567890", 10) == 0, i); + test_assert_idx(str_begins(str_c(str2), "1234567890"), i); if (tests[i].sanitized != NULL) test_assert_idx(strcmp(str_c(str2)+10, tests[i].sanitized) == 0, i); else diff --git a/src/lib/unlink-directory.c b/src/lib/unlink-directory.c index 8c4f015f69..98fddd1ab6 100644 --- a/src/lib/unlink-directory.c +++ b/src/lib/unlink-directory.c @@ -201,7 +201,7 @@ unlink_directory_r(const char *dir, enum unlink_directory_flags flags, (flags & UNLINK_DIRECTORY_FLAG_FILES_ONLY) != 0) { /* skip directory */ } else if (old_errno == EBUSY && - strncmp(d->d_name, ".nfs", 4) == 0) { + str_begins(d->d_name, ".nfs")) { /* can't delete NFS files that are still in use. let the caller decide if this error is worth logging about */ diff --git a/src/log/log-connection.c b/src/log/log-connection.c index e117c2b184..f9866f2724 100644 --- a/src/log/log-connection.c +++ b/src/log/log-connection.c @@ -110,9 +110,9 @@ static void log_parse_option(struct log_connection *log, struct log_client *client; client = log_client_get(log, failure->pid); - if (strncmp(failure->text, "ip=", 3) == 0) + if (str_begins(failure->text, "ip=")) (void)net_addr2ip(failure->text + 3, &client->ip); - else if (strncmp(failure->text, "prefix=", 7) == 0) { + else if (str_begins(failure->text, "prefix=")) { i_free(client->prefix); client->prefix = i_strdup(failure->text + 7); } @@ -219,9 +219,9 @@ log_parse_master_line(const char *line, const struct timeval *log_time, return; } log_client_free(log, client, pid); - } else if (strncmp(cmd, "FATAL ", 6) == 0) { + } else if (str_begins(cmd, "FATAL ")) { client_log_fatal(log, client, cmd + 6, log_time, tm); - } else if (strncmp(cmd, "DEFAULT-FATAL ", 14) == 0) { + } else if (str_begins(cmd, "DEFAULT-FATAL ")) { /* If the client has logged a fatal/panic, don't log this message. */ if (client == NULL || !client->fatal_logged) diff --git a/src/login-common/client-common-auth.c b/src/login-common/client-common-auth.c index 76cd407a26..071ee11022 100644 --- a/src/login-common/client-common-auth.c +++ b/src/login-common/client-common-auth.c @@ -209,12 +209,12 @@ static void client_auth_parse_args(struct client *client, bool success, } else if (strcmp(key, "user") == 0 || strcmp(key, "postlogin_socket") == 0) { /* already handled in sasl-server.c */ - } else if (strncmp(key, "user_", 5) == 0) { + } else if (str_begins(key, "user_")) { if (success) { alt_username_set(&alt_usernames, client->pool, key, value); } - } else if (strncmp(key, "forward_", 8) == 0) { + } else if (str_begins(key, "forward_")) { /* these are passed to upstream */ } else if (client->set->auth_debug) i_debug("Ignoring unknown passdb extra field: %s", key); diff --git a/src/login-common/sasl-server.c b/src/login-common/sasl-server.c index c8c2f5107e..dedfb5b8f9 100644 --- a/src/login-common/sasl-server.c +++ b/src/login-common/sasl-server.c @@ -269,26 +269,26 @@ authenticate_callback(struct auth_client_request *request, nologin = FALSE; for (i = 0; args[i] != NULL; i++) { - if (strncmp(args[i], "user=", 5) == 0) { + if (str_begins(args[i], "user=")) { i_free(client->virtual_user); i_free_and_null(client->virtual_user_orig); i_free_and_null(client->virtual_auth_user); client->virtual_user = i_strdup(args[i] + 5); - } else if (strncmp(args[i], "original_user=", 14) == 0) { + } else if (str_begins(args[i], "original_user=")) { i_free(client->virtual_user_orig); client->virtual_user_orig = i_strdup(args[i] + 14); - } else if (strncmp(args[i], "auth_user=", 10) == 0) { + } else if (str_begins(args[i], "auth_user=")) { i_free(client->virtual_auth_user); client->virtual_auth_user = i_strdup(args[i] + 10); - } else if (strncmp(args[i], "postlogin_socket=", 17) == 0) { + } else if (str_begins(args[i], "postlogin_socket=")) { client->postlogin_socket_path = p_strdup(client->pool, args[i] + 17); } else if (strcmp(args[i], "nologin") == 0 || strcmp(args[i], "proxy") == 0) { /* user can't login */ nologin = TRUE; - } else if (strncmp(args[i], "resp=", 5) == 0 && + } else if (str_begins(args[i], "resp=") && login_binary->sasl_support_final_reply) { client->sasl_final_resp = p_strdup(client->pool, args[i] + 5); @@ -313,17 +313,17 @@ authenticate_callback(struct auth_client_request *request, if (args != NULL) { /* parse our username if it's there */ for (i = 0; args[i] != NULL; i++) { - if (strncmp(args[i], "user=", 5) == 0) { + if (str_begins(args[i], "user=")) { i_free(client->virtual_user); i_free_and_null(client->virtual_user_orig); i_free_and_null(client->virtual_auth_user); client->virtual_user = i_strdup(args[i] + 5); - } else if (strncmp(args[i], "original_user=", 14) == 0) { + } else if (str_begins(args[i], "original_user=")) { i_free(client->virtual_user_orig); client->virtual_user_orig = i_strdup(args[i] + 14); - } else if (strncmp(args[i], "auth_user=", 10) == 0) { + } else if (str_begins(args[i], "auth_user=")) { i_free(client->virtual_auth_user); client->virtual_auth_user = i_strdup(args[i] + 10); diff --git a/src/master/main.c b/src/master/main.c index a29926fdf8..4c04c7efac 100644 --- a/src/master/main.c +++ b/src/master/main.c @@ -755,7 +755,7 @@ int main(int argc, char *argv[]) /* drop -- prefix from all --args. ugly, but the only way that it works with standard getopt() in all OSes.. */ for (i = 1; i < argc; i++) { - if (strncmp(argv[i], "--", 2) == 0) { + if (str_begins(argv[i], "--")) { if (argv[i][2] == '\0') break; argv[i] += 2; diff --git a/src/old-stats/client-export.c b/src/old-stats/client-export.c index e8478ec971..e020d134fa 100644 --- a/src/old-stats/client-export.c +++ b/src/old-stats/client-export.c @@ -72,19 +72,19 @@ mail_export_parse_filter(const char *const *args, pool_t pool, */ i_zero(filter_r); for (; *args != NULL; args++) { - if (strncmp(*args, "user=", 5) == 0) + if (str_begins(*args, "user=")) filter_r->user = p_strdup(pool, *args + 5); - else if (strncmp(*args, "domain=", 7) == 0) + else if (str_begins(*args, "domain=")) filter_r->domain = p_strdup(pool, *args + 7); - else if (strncmp(*args, "session=", 8) == 0) + else if (str_begins(*args, "session=")) filter_r->session = p_strdup(pool, *args + 8); - else if (strncmp(*args, "ip=", 3) == 0) { + else if (str_begins(*args, "ip=")) { if (net_parse_range(*args + 3, &filter_r->ip, &filter_r->ip_bits) < 0) { *error_r = "Invalid ip filter"; return -1; } - } else if (strncmp(*args, "since=", 6) == 0) { + } else if (str_begins(*args, "since=")) { if (str_to_ulong(*args + 6, &l) < 0) { *error_r = "Invalid since filter"; return -1; diff --git a/src/old-stats/mail-session.c b/src/old-stats/mail-session.c index 86f0944c38..db202b305a 100644 --- a/src/old-stats/mail-session.c +++ b/src/old-stats/mail-session.c @@ -112,7 +112,7 @@ int mail_session_connect_parse(const char *const *args, const char **error_r) mail_domain_login(session->user->domain); for (i = 3; args[i] != NULL; i++) { - if (strncmp(args[i], "rip=", 4) == 0 && + if (str_begins(args[i], "rip=") && net_addr2ip(args[i] + 4, &ip) == 0) session->ip = mail_ip_login(&ip); } diff --git a/src/plugins/acl/acl-backend-vfile.c b/src/plugins/acl/acl-backend-vfile.c index aeb4961cb2..c1fe2b6078 100644 --- a/src/plugins/acl/acl-backend-vfile.c +++ b/src/plugins/acl/acl-backend-vfile.c @@ -43,7 +43,7 @@ acl_backend_vfile_init(struct acl_backend *_backend, const char *data) if (*tmp != NULL) tmp++; for (; *tmp != NULL; tmp++) { - if (strncmp(*tmp, "cache_secs=", 11) == 0) { + if (str_begins(*tmp, "cache_secs=")) { if (str_to_uint(*tmp + 11, &backend->cache_secs) < 0) { i_error("acl vfile: Invalid cache_secs value: %s", *tmp + 11); diff --git a/src/plugins/acl/acl-backend.c b/src/plugins/acl/acl-backend.c index a07d797919..026aef59c5 100644 --- a/src/plugins/acl/acl-backend.c +++ b/src/plugins/acl/acl-backend.c @@ -48,7 +48,7 @@ acl_backend_init(const char *data, struct mailbox_list *list, group_count = str_array_length(groups); - if (strncmp(data, "vfile:", 6) == 0) + if (str_begins(data, "vfile:")) data += 6; else if (strcmp(data, "vfile") == 0) data = ""; diff --git a/src/plugins/fs-compress/fs-compress.c b/src/plugins/fs-compress/fs-compress.c index 5540cfa582..ff8445783c 100644 --- a/src/plugins/fs-compress/fs-compress.c +++ b/src/plugins/fs-compress/fs-compress.c @@ -48,7 +48,7 @@ fs_compress_init(struct fs *_fs, const char *args, const const char *parent_name, *parent_args; /* get compression handler name */ - if (strncmp(args, "maybe-", 6) == 0) { + if (str_begins(args, "maybe-")) { fs->try_plain = TRUE; args += 6; } diff --git a/src/plugins/fts-lucene/fts-lucene-plugin.c b/src/plugins/fts-lucene/fts-lucene-plugin.c index d00c674cf8..9a8647f95e 100644 --- a/src/plugins/fts-lucene/fts-lucene-plugin.c +++ b/src/plugins/fts-lucene/fts-lucene-plugin.c @@ -20,14 +20,14 @@ fts_lucene_plugin_init_settings(struct mail_user *user, const char *const *tmp; for (tmp = t_strsplit_spaces(str, " "); *tmp != NULL; tmp++) { - if (strncmp(*tmp, "default_language=", 17) == 0) { + if (str_begins(*tmp, "default_language=")) { set->default_language = p_strdup(user->pool, *tmp + 17); - } else if (strncmp(*tmp, "textcat_conf=", 13) == 0) { + } else if (str_begins(*tmp, "textcat_conf=")) { set->textcat_conf = p_strdup(user->pool, *tmp + 13); - } else if (strncmp(*tmp, "textcat_dir=", 12) == 0) { + } else if (str_begins(*tmp, "textcat_dir=")) { set->textcat_dir = p_strdup(user->pool, *tmp + 12); - } else if (strncmp(*tmp, "whitespace_chars=", 17) == 0) { + } else if (str_begins(*tmp, "whitespace_chars=")) { set->whitespace_chars = p_strdup(user->pool, *tmp + 17); } else if (strcmp(*tmp, "normalize") == 0) { set->normalize = TRUE; diff --git a/src/plugins/fts-solr/fts-solr-plugin.c b/src/plugins/fts-solr/fts-solr-plugin.c index 16f6af005f..19b89b1906 100644 --- a/src/plugins/fts-solr/fts-solr-plugin.c +++ b/src/plugins/fts-solr/fts-solr-plugin.c @@ -26,7 +26,7 @@ fts_solr_plugin_init_settings(struct mail_user *user, str = ""; for (tmp = t_strsplit_spaces(str, " "); *tmp != NULL; tmp++) { - if (strncmp(*tmp, "url=", 4) == 0) { + if (str_begins(*tmp, "url=")) { set->url = p_strdup(user->pool, *tmp + 4); } else if (strcmp(*tmp, "debug") == 0) { set->debug = TRUE; diff --git a/src/plugins/fts-squat/fts-backend-squat.c b/src/plugins/fts-squat/fts-backend-squat.c index 19f09fccec..cd82400ef2 100644 --- a/src/plugins/fts-squat/fts-backend-squat.c +++ b/src/plugins/fts-squat/fts-backend-squat.c @@ -57,14 +57,14 @@ fts_backend_squat_init(struct fts_backend *_backend, const char **error_r) return 0; for (tmp = t_strsplit_spaces(env, " "); *tmp != NULL; tmp++) { - if (strncmp(*tmp, "partial=", 8) == 0) { + if (str_begins(*tmp, "partial=")) { if (str_to_uint(*tmp + 8, &len) < 0 || len == 0) { *error_r = t_strdup_printf( "Invalid partial length: %s", *tmp + 8); return -1; } backend->partial_len = len; - } else if (strncmp(*tmp, "full=", 5) == 0) { + } else if (str_begins(*tmp, "full=")) { if (str_to_uint(*tmp + 5, &len) < 0 || len == 0) { *error_r = t_strdup_printf( "Invalid full length: %s", *tmp + 5); diff --git a/src/plugins/fts-squat/squat-test.c b/src/plugins/fts-squat/squat-test.c index f7f23c4a16..e2c5a9c355 100644 --- a/src/plugins/fts-squat/squat-test.c +++ b/src/plugins/fts-squat/squat-test.c @@ -78,7 +78,7 @@ int main(int argc ATTR_UNUSED, char *argv[]) fflush(stderr); last = input->v_offset/(1024*100); } - if (strncmp(line, "From ", 5) == 0) { + if (str_begins(line, "From ")) { if (!first) seq++; data_header = TRUE; @@ -88,7 +88,7 @@ int main(int argc ATTR_UNUSED, char *argv[]) } first = FALSE; - if (strncmp(line, "--", 2) == 0) { + if (str_begins(line, "--")) { skip_body = FALSE; mime_header = TRUE; } diff --git a/src/plugins/fts/fts-build-mail.c b/src/plugins/fts/fts-build-mail.c index 0d2518b88b..3125f6c2e5 100644 --- a/src/plugins/fts/fts-build-mail.c +++ b/src/plugins/fts/fts-build-mail.c @@ -230,7 +230,7 @@ fts_build_body_begin(struct fts_mail_build_context *ctx, i_zero(&parser_context); parser_context.content_type = ctx->content_type != NULL ? ctx->content_type : "text/plain"; - if (strncmp(parser_context.content_type, "multipart/", 10) == 0) { + if (str_begins(parser_context.content_type, "multipart/")) { /* multiparts are never indexed, only their contents */ return FALSE; } @@ -243,8 +243,8 @@ fts_build_body_begin(struct fts_mail_build_context *ctx, /* extract text using the the returned parser */ *binary_body_r = TRUE; key.type = FTS_BACKEND_BUILD_KEY_BODY_PART; - } else if (strncmp(parser_context.content_type, "text/", 5) == 0 || - strncmp(parser_context.content_type, "message/", 8) == 0) { + } else if (str_begins(parser_context.content_type, "text/") || + str_begins(parser_context.content_type, "message/")) { /* text body parts */ key.type = FTS_BACKEND_BUILD_KEY_BODY_PART; ctx->body_parser = fts_parser_text_init(); diff --git a/src/plugins/fts/fts-indexer.c b/src/plugins/fts/fts-indexer.c index bda0551037..0fd7a8f260 100644 --- a/src/plugins/fts/fts-indexer.c +++ b/src/plugins/fts/fts-indexer.c @@ -169,7 +169,7 @@ static int fts_indexer_input(struct fts_indexer_context *ctx) while ((line = i_stream_read_next_line(ctx->input)) != NULL) { /* initial reply: \t OK following: \t */ - if (strncmp(line, "1\t", 2) != 0) { + if (!str_begins(line, "1\t")) { i_error("indexer sent invalid reply: %s", line); return -1; } diff --git a/src/plugins/old-stats/mail-stats-fill.c b/src/plugins/old-stats/mail-stats-fill.c index c8fc244dcf..6477190299 100644 --- a/src/plugins/old-stats/mail-stats-fill.c +++ b/src/plugins/old-stats/mail-stats-fill.c @@ -19,16 +19,16 @@ process_io_buffer_parse(const char *buf, struct mail_stats *stats) tmp = t_strsplit(buf, "\n"); for (; *tmp != NULL; tmp++) { - if (strncmp(*tmp, "rchar: ", 7) == 0) { + if (str_begins(*tmp, "rchar: ")) { if (str_to_uint64(*tmp + 7, &stats->read_bytes) < 0) return -1; - } else if (strncmp(*tmp, "wchar: ", 7) == 0) { + } else if (str_begins(*tmp, "wchar: ")) { if (str_to_uint64(*tmp + 7, &stats->write_bytes) < 0) return -1; - } else if (strncmp(*tmp, "syscr: ", 7) == 0) { + } else if (str_begins(*tmp, "syscr: ")) { if (str_to_uint32(*tmp + 7, &stats->read_count) < 0) return -1; - } else if (strncmp(*tmp, "syscw: ", 7) == 0) { + } else if (str_begins(*tmp, "syscw: ")) { if (str_to_uint32(*tmp + 7, &stats->write_count) < 0) return -1; } diff --git a/src/plugins/quota/quota-status.c b/src/plugins/quota/quota-status.c index 0b58c17645..330272381b 100644 --- a/src/plugins/quota/quota-status.c +++ b/src/plugins/quota/quota-status.c @@ -163,7 +163,7 @@ static int client_input_line(struct connection *conn, const char *line) return 1; } if (client->recipient == NULL && - strncmp(line, "recipient=", 10) == 0) { + str_begins(line, "recipient=")) { if (smtp_address_parse_path(default_pool, line + 10, SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART | SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL, @@ -173,7 +173,7 @@ static int client_input_line(struct connection *conn, const char *line) error); return 0; } - } else if (strncmp(line, "size=", 5) == 0) { + } else if (str_begins(line, "size=")) { if (str_to_uoff(line+5, &client->size) < 0) client->size = 0; } diff --git a/src/plugins/quota/quota-util.c b/src/plugins/quota/quota-util.c index 95c49695d7..224916d5a0 100644 --- a/src/plugins/quota/quota-util.c +++ b/src/plugins/quota/quota-util.c @@ -262,7 +262,7 @@ int quota_root_add_rule(struct quota_root_settings *root_set, return 0; } - if (strncmp(p, "backend=", 8) == 0) { + if (str_begins(p, "backend=")) { if (root_set->backend->v.parse_rule == NULL) { *error_r = "backend rule not supported"; ret = -1; diff --git a/src/plugins/quota/quota.c b/src/plugins/quota/quota.c index 4d2ef17556..7fbdc1be93 100644 --- a/src/plugins/quota/quota.c +++ b/src/plugins/quota/quota.c @@ -184,7 +184,7 @@ quota_root_parse_set(struct mail_user *user, const char *root_name, if (value == NULL) return 0; - if (strncmp(value, "dict:", 5) != 0) { + if (!str_begins(value, "dict:")) { *error_r = t_strdup_printf("%s supports only dict backend", name); return -1; } diff --git a/src/pop3-login/pop3-proxy.c b/src/pop3-login/pop3-proxy.c index 5554097da3..817d40145a 100644 --- a/src/pop3-login/pop3-proxy.c +++ b/src/pop3-login/pop3-proxy.c @@ -154,7 +154,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) switch (pop3_client->proxy_state) { case POP3_PROXY_BANNER: /* this is a banner */ - if (strncmp(line, "+OK", 3) != 0) { + if (!str_begins(line, "+OK")) { client_log_err(client, t_strdup_printf( "proxy: Remote returned invalid banner: %s", str_sanitize(line, 160))); @@ -162,7 +162,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) return -1; } pop3_client->proxy_xclient = - strncmp(line+3, " [XCLIENT]", 10) == 0; + str_begins(line+3, " [XCLIENT]"); ssl_flags = login_proxy_get_ssl_flags(client->login_proxy); if ((ssl_flags & PROXY_SSL_FLAG_STARTTLS) == 0) { @@ -176,7 +176,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) } return 0; case POP3_PROXY_STARTTLS: - if (strncmp(line, "+OK", 3) != 0) { + if (!str_begins(line, "+OK")) { client_log_err(client, t_strdup_printf( "proxy: Remote STLS failed: %s", str_sanitize(line, 160))); @@ -195,7 +195,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) } return 1; case POP3_PROXY_XCLIENT: - if (strncmp(line, "+OK", 3) != 0) { + if (!str_begins(line, "+OK")) { client_log_err(client, t_strdup_printf( "proxy: Remote XCLIENT failed: %s", str_sanitize(line, 160))); @@ -207,7 +207,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) return 0; case POP3_PROXY_LOGIN1: i_assert(client->proxy_sasl_client == NULL); - if (strncmp(line, "+OK", 3) != 0) + if (!str_begins(line, "+OK")) break; /* USER successful, send PASS */ @@ -217,7 +217,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) pop3_client->proxy_state = POP3_PROXY_LOGIN2; return 0; case POP3_PROXY_LOGIN2: - if (strncmp(line, "+ ", 2) == 0 && + if (str_begins(line, "+ ") && client->proxy_sasl_client != NULL) { /* continue SASL authentication */ if (pop3_proxy_continue_sasl_auth(client, output, @@ -227,7 +227,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) } return 0; } - if (strncmp(line, "+OK", 3) != 0) + if (!str_begins(line, "+OK")) break; /* Login successful. Send this line to client. */ @@ -255,7 +255,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) So for now we'll just forward the error message. This shouldn't be a real problem since of course everyone will be using only Dovecot as their backend :) */ - if (strncmp(line, "-ERR ", 5) != 0) { + if (!str_begins(line, "-ERR ")) { client_send_reply(client, POP3_CMD_REPLY_ERROR, AUTH_FAILED_MSG); } else { @@ -263,7 +263,7 @@ int pop3_proxy_parse_line(struct client *client, const char *line) } if (client->set->auth_verbose) { - if (strncmp(line, "-ERR ", 5) == 0) + if (str_begins(line, "-ERR ")) line += 5; client_proxy_log_failure(client, line); } diff --git a/src/util/script.c b/src/util/script.c index 5f88cc9075..ee16dbd133 100644 --- a/src/util/script.c +++ b/src/util/script.c @@ -156,14 +156,14 @@ static bool client_exec_script(struct master_service_connection *conn) if (*args != NULL) { const char *p; - if (strncmp(*args, "alarm=", 6) == 0) { + if (str_begins(*args, "alarm=")) { unsigned int seconds; if (str_to_uint(*args + 6, &seconds) < 0) i_fatal("invalid alarm option"); alarm(seconds); args++; } - while (strncmp(*args, "env_", 4) == 0) { + while (str_begins(*args, "env_")) { const char *envname, *env; env = t_str_tabunescape(*args+4);