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

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 <[email protected]>
  • Loading branch information
alejandro-colomar committed Jul 30, 2024
1 parent 3473796 commit 5f0b6d4
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 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 "memzero.h"
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
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 @@ 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
5 changes: 3 additions & 2 deletions lib/tcbfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 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 @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef USE_PAM
#include "pam_defs.h"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5f0b6d4

Please sign in to comment.