From 5f0b6d47cd13cd8bcd05a947eef41e8ad4eab7cf Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 4 Jul 2024 19:00:53 +0200 Subject: [PATCH] contrib/, lib/, src/: Use consistent style using strchr(3) in conditionals For negative matches, use if (strchr(...) == NULL) For positive matches, use the cast-to-bool operator: if (!!strchr(...)) For positive matches, when a variable is also set, use while (NULL != (p = strchr(...))) Signed-off-by: Alejandro Colomar --- contrib/adduser.c | 2 +- lib/env.c | 7 ++++--- lib/obscure.c | 7 ++++--- lib/setupenv.c | 2 +- lib/tcbfuncs.c | 5 +++-- src/chfn.c | 7 ++++--- src/chpasswd.c | 11 ++++++----- src/login_nopam.c | 4 ++-- src/su.c | 5 +++-- 9 files changed, 28 insertions(+), 22 deletions(-) diff --git a/contrib/adduser.c b/contrib/adduser.c index 444f8d6ec8..5be2205a8d 100644 --- a/contrib/adduser.c +++ b/contrib/adduser.c @@ -222,7 +222,7 @@ main (void) printf ("That name is in use, choose another.\n"); done = 0; } - else if (strchr (usrname, ' ') != NULL) + else if (!!strchr(usrname, ' ')) { printf ("No spaces in username!!\n"); done = 0; diff --git a/lib/env.c b/lib/env.c index 9cb3137122..a49648d8b5 100644 --- a/lib/env.c +++ b/lib/env.c @@ -201,7 +201,8 @@ void set_env (int argc, char *const *argv) * but... I feel better with that silly precaution. -j. */ -void sanitize_env (void) +void +sanitize_env(void) { char **envp = environ; const char *const *bad; @@ -225,9 +226,9 @@ void sanitize_env (void) if (strncmp (*cur, *bad, strlen (*bad)) != 0) { continue; } - if (strchr (*cur, '/') == NULL) { + if (strchr(*cur, '/') == NULL) continue; /* OK */ - } + for (move = cur; NULL != *move; move++) { *move = *(move + 1); } diff --git a/lib/obscure.c b/lib/obscure.c index 74a32f0c3d..6c76a81d8d 100644 --- a/lib/obscure.c +++ b/lib/obscure.c @@ -14,6 +14,7 @@ #include #include +#include #include "attr.h" #include "memzero.h" @@ -50,7 +51,8 @@ static bool palindrome (MAYBE_UNUSED const char *old, const char *new) * more than half of the characters are different ones. */ -static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new) +static bool +similar(/*@notnull@*/const char *old, /*@notnull@*/const char *new) { int i, j; @@ -65,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new) } for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) { - if (strchr (new, old[i]) != NULL) { + if (!!strchr(new, old[i])) j++; - } } if (i >= j * 2) { diff --git a/lib/setupenv.c b/lib/setupenv.c index f4de82ad70..2e0b163628 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -235,7 +235,7 @@ setup_env(struct passwd *info) if (NULL == cp) { /* not specified, use a minimal default */ addenv ((info->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL); - } else if (strchr (cp, '=')) { + } else if (!!strchr(cp, '=')) { /* specified as name=value (PATH=...) */ addenv (cp, NULL); } else { diff --git a/lib/tcbfuncs.c b/lib/tcbfuncs.c index 3dc2abd700..34844b7b2d 100644 --- a/lib/tcbfuncs.c +++ b/lib/tcbfuncs.c @@ -184,7 +184,8 @@ static /*@null@*/ char *shadowtcb_path_existing (const char *name) return ret; } -static shadowtcb_status mkdir_leading (const char *name, uid_t uid) +static shadowtcb_status +mkdir_leading(const char *name, uid_t uid) { char *ind, *dir, *ptr, *path = shadowtcb_path_rel (name, uid); struct stat st; @@ -199,7 +200,7 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid) shadow_progname, TCB_DIR, strerror (errno)); goto out_free_path; } - while ((ind = strchr (ptr, '/'))) { + while (NULL != (ind = strchr(ptr, '/'))) { stpcpy(ind, ""); if (asprintf (&dir, TCB_DIR "/%s", path) == -1) { OUT_OF_MEMORY; diff --git a/src/chfn.c b/src/chfn.c index 1872b2df4c..0a837fbe35 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -122,7 +123,8 @@ usage (int status) * * Return true if the user can change the field and false otherwise. */ -static bool may_change_field (int field) +static bool +may_change_field(int field) { const char *cp; @@ -156,9 +158,8 @@ static bool may_change_field (int field) cp = "frwh"; } - if (strchr (cp, field) != NULL) { + if (!!strchr(cp, field)) return true; - } return false; } diff --git a/src/chpasswd.c b/src/chpasswd.c index 55817ddf4d..6bbcbac9eb 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef USE_PAM #include "pam_defs.h" @@ -439,12 +440,12 @@ static const char *get_salt(void) return crypt_make_salt (crypt_method, arg); } -int main (int argc, char **argv) +int +main(int argc, char **argv) { char buf[BUFSIZ]; char *name; char *newpwd; - char *cp; const char *salt; #ifdef USE_PAM @@ -501,15 +502,15 @@ int main (int argc, char **argv) * present. */ while (fgets (buf, sizeof buf, stdin) != NULL) { + char *cp; + line++; if (stpsep(buf, "\n") == NULL) { if (feof (stdin) == 0) { // Drop all remaining characters on this line. while (fgets (buf, sizeof buf, stdin) != NULL) { - cp = strchr (buf, '\n'); - if (cp != NULL) { + if (!!strchr(buf, '\n')) break; - } } fprintf (stderr, diff --git a/src/login_nopam.c b/src/login_nopam.c index 0aa70375ce..f5a7f96543 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -320,9 +320,9 @@ from_match(const char *tok, const char *string) return true; } } else if (strcasecmp (tok, "LOCAL") == 0) { /* local: no dots */ - if (strchr (string, '.') == NULL) { + if (strchr(string, '.') == NULL) return true; - } + } else if ( (strcmp(tok, "") != 0) && (tok[(tok_len = strlen (tok)) - 1] == '.') /* network */ && (strncmp (tok, resolve_hostname (string), tok_len) == 0)) { diff --git a/src/su.c b/src/su.c index 60a94e6a3d..126dc0a8e6 100644 --- a/src/su.c +++ b/src/su.c @@ -883,7 +883,8 @@ process_flags(int argc, char **argv) } } -static void set_environment (struct passwd *pw) +static void +set_environment(struct passwd *pw) { const char *cp; /* @@ -951,7 +952,7 @@ static void set_environment (struct passwd *pw) cp = getdef_str ((pw->pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH"); if (NULL == cp) { addenv ((pw->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL); - } else if (strchr (cp, '=') != NULL) { + } else if (!!strchr(cp, '=')) { addenv (cp, NULL); } else { addenv ("PATH", cp);