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 Dec 10, 2024
1 parent f4a7f27 commit f6c8482
Show file tree
Hide file tree
Showing 9 changed files with 27 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 @@ -224,7 +224,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 @@ -202,7 +202,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 @@ -226,9 +227,9 @@ void sanitize_env (void)
if (!strprefix(*cur, *bad)) {
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 "prototypes.h"
Expand Down Expand Up @@ -51,7 +52,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 @@ -66,9 +68,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 @@ -234,7 +234,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
5 changes: 3 additions & 2 deletions lib/tcbfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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 @@ -191,7 +192,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 @@ -124,7 +125,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 @@ -158,9 +160,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 @@ -440,12 +441,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 @@ -502,15 +503,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
3 changes: 1 addition & 2 deletions src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,8 @@ static bool 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 ( (!streq(tok, "") && tok[strlen(tok) - 1] == '.') /* network */
&& strprefix(resolve_hostname(string), tok)) {
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ static void 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 @@ -950,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 f6c8482

Please sign in to comment.