Skip to content

Commit 9af0b28

Browse files
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 <[email protected]>
1 parent a2c70df commit 9af0b28

File tree

9 files changed

+28
-22
lines changed

9 files changed

+28
-22
lines changed

contrib/adduser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ main (void)
222222
printf ("That name is in use, choose another.\n");
223223
done = 0;
224224
}
225-
else if (strchr (usrname, ' ') != NULL)
225+
else if (!!strchr(usrname, ' '))
226226
{
227227
printf ("No spaces in username!!\n");
228228
done = 0;

lib/env.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ void set_env (int argc, char *const *argv)
201201
* but... I feel better with that silly precaution. -j.
202202
*/
203203

204-
void sanitize_env (void)
204+
void
205+
sanitize_env(void)
205206
{
206207
char **envp = environ;
207208
const char *const *bad;
@@ -225,9 +226,9 @@ void sanitize_env (void)
225226
if (strncmp (*cur, *bad, strlen (*bad)) != 0) {
226227
continue;
227228
}
228-
if (strchr (*cur, '/') == NULL) {
229+
if (strchr(*cur, '/') == NULL)
229230
continue; /* OK */
230-
}
231+
231232
for (move = cur; NULL != *move; move++) {
232233
*move = *(move + 1);
233234
}

lib/obscure.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <ctype.h>
1616
#include <stdio.h>
17+
#include <string.h>
1718

1819
#include "attr.h"
1920
#include "prototypes.h"
@@ -50,7 +51,8 @@ static bool palindrome (MAYBE_UNUSED const char *old, const char *new)
5051
* more than half of the characters are different ones.
5152
*/
5253

53-
static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
54+
static bool
55+
similar(/*@notnull@*/const char *old, /*@notnull@*/const char *new)
5456
{
5557
int i, j;
5658

@@ -65,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
6567
}
6668

6769
for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
68-
if (strchr (new, old[i]) != NULL) {
70+
if (!!strchr(new, old[i]))
6971
j++;
70-
}
7172
}
7273

7374
if (i >= j * 2) {

lib/setupenv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ setup_env(struct passwd *info)
235235
if (NULL == cp) {
236236
/* not specified, use a minimal default */
237237
addenv ((info->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
238-
} else if (strchr (cp, '=')) {
238+
} else if (!!strchr(cp, '=')) {
239239
/* specified as name=value (PATH=...) */
240240
addenv (cp, NULL);
241241
} else {

lib/tcbfuncs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ static /*@null@*/ char *shadowtcb_path_existing (const char *name)
184184
return ret;
185185
}
186186

187-
static shadowtcb_status mkdir_leading (const char *name, uid_t uid)
187+
static shadowtcb_status
188+
mkdir_leading(const char *name, uid_t uid)
188189
{
189190
char *ind, *dir, *ptr, *path = shadowtcb_path_rel (name, uid);
190191
struct stat st;
@@ -199,7 +200,7 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid)
199200
shadow_progname, TCB_DIR, strerror (errno));
200201
goto out_free_path;
201202
}
202-
while ((ind = strchr (ptr, '/'))) {
203+
while (NULL != (ind = strchr(ptr, '/'))) {
203204
stpcpy(ind, "");
204205
if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
205206
OUT_OF_MEMORY;

src/chfn.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <pwd.h>
1616
#include <signal.h>
1717
#include <stdio.h>
18+
#include <string.h>
1819
#include <sys/types.h>
1920
#include <getopt.h>
2021

@@ -122,7 +123,8 @@ usage (int status)
122123
*
123124
* Return true if the user can change the field and false otherwise.
124125
*/
125-
static bool may_change_field (int field)
126+
static bool
127+
may_change_field(int field)
126128
{
127129
const char *cp;
128130

@@ -156,9 +158,8 @@ static bool may_change_field (int field)
156158
cp = "frwh";
157159
}
158160

159-
if (strchr (cp, field) != NULL) {
161+
if (!!strchr(cp, field))
160162
return true;
161-
}
162163

163164
return false;
164165
}

src/chpasswd.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <pwd.h>
1717
#include <stdio.h>
1818
#include <stdlib.h>
19+
#include <string.h>
1920

2021
#ifdef USE_PAM
2122
#include "pam_defs.h"
@@ -439,12 +440,12 @@ static const char *get_salt(void)
439440
return crypt_make_salt (crypt_method, arg);
440441
}
441442

442-
int main (int argc, char **argv)
443+
int
444+
main(int argc, char **argv)
443445
{
444446
char buf[BUFSIZ];
445447
char *name;
446448
char *newpwd;
447-
char *cp;
448449
const char *salt;
449450

450451
#ifdef USE_PAM
@@ -501,15 +502,15 @@ int main (int argc, char **argv)
501502
* present.
502503
*/
503504
while (fgets (buf, sizeof buf, stdin) != NULL) {
505+
char *cp;
506+
504507
line++;
505508
if (stpsep(buf, "\n") == NULL) {
506509
if (feof (stdin) == 0) {
507510
// Drop all remaining characters on this line.
508511
while (fgets (buf, sizeof buf, stdin) != NULL) {
509-
cp = strchr (buf, '\n');
510-
if (cp != NULL) {
512+
if (!!strchr(buf, '\n'))
511513
break;
512-
}
513514
}
514515

515516
fprintf (stderr,

src/login_nopam.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ from_match(const char *tok, const char *string)
320320
return true;
321321
}
322322
} else if (strcasecmp (tok, "LOCAL") == 0) { /* local: no dots */
323-
if (strchr (string, '.') == NULL) {
323+
if (strchr(string, '.') == NULL)
324324
return true;
325-
}
325+
326326
} else if ( (strcmp(tok, "") != 0)
327327
&& (tok[(tok_len = strlen (tok)) - 1] == '.') /* network */
328328
&& (strncmp (tok, resolve_hostname (string), tok_len) == 0)) {

src/su.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@ process_flags(int argc, char **argv)
883883
}
884884
}
885885

886-
static void set_environment (struct passwd *pw)
886+
static void
887+
set_environment(struct passwd *pw)
887888
{
888889
const char *cp;
889890
/*
@@ -951,7 +952,7 @@ static void set_environment (struct passwd *pw)
951952
cp = getdef_str ((pw->pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
952953
if (NULL == cp) {
953954
addenv ((pw->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
954-
} else if (strchr (cp, '=') != NULL) {
955+
} else if (!!strchr(cp, '=')) {
955956
addenv (cp, NULL);
956957
} else {
957958
addenv ("PATH", cp);

0 commit comments

Comments
 (0)