Skip to content

Commit

Permalink
contrib/, lib/, src/: Use consistent style using strchr(3) in conditi…
Browse files Browse the repository at this point in the history
…onals

While the return value is a pointer, it can be interpreted as a boolean
value meaning "found".  In general, we use explicit comparisons of
pointers to NULL, but in this specific case, let's use that
interpretation, and make an exception, using an implicit conversion to
boolean.

For negative matches, use
	if (!strchr(...))

For positive matches, use
	if (strchr(...))

For positive matches, when a variable is also set, use
	while (NULL != (p = strchr(...)))

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 24, 2025
1 parent 34efa97 commit e7cd55d
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,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;
Expand Down
4 changes: 2 additions & 2 deletions lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ void sanitize_env (void)
if (!strprefix(*cur, *bad)) {
continue;
}
if (strchr (*cur, '/') == NULL) {
if (!strchr(*cur, '/'))
continue; /* OK */
}

for (move = cur; NULL != *move; move++) {
*move = *(move + 1);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/obscure.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "attr.h"
#include "prototypes.h"
Expand Down Expand Up @@ -66,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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void 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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/tcbfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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;
Expand Down
4 changes: 2 additions & 2 deletions src/chfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <getopt.h>

Expand Down Expand Up @@ -158,9 +159,8 @@ static bool may_change_field (int field)
cp = "frwh";
}

if (strchr (cp, field) != NULL) {
if (strchr(cp, field))
return true;
}

return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef USE_PAM
#include "pam_defs.h"
Expand Down Expand Up @@ -509,10 +510,8 @@ int main (int argc, char **argv)
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,
Expand Down
3 changes: 1 addition & 2 deletions src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ static bool from_match (char *tok, const char *string)
return true;
}
} else if (strcasecmp (tok, "LOCAL") == 0) { /* local: no dots */
if (strchr (string, '.') == NULL) {
if (!strchr(string, '.'))
return true;
}
} else if ( (!streq(tok, "") && tok[strlen(tok) - 1] == '.') /* network */
&& strprefix(resolve_hostname(string), tok)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,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);
Expand Down

0 comments on commit e7cd55d

Please sign in to comment.