Skip to content

Commit ec047be

Browse files
contrib/, lib/, src/: Use consistent style using strchr(3) in conditionals
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]>
1 parent f43ec93 commit ec047be

File tree

9 files changed

+13
-15
lines changed

9 files changed

+13
-15
lines changed

contrib/adduser.c

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

lib/env.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ void sanitize_env (void)
226226
if (!strprefix(*cur, *bad)) {
227227
continue;
228228
}
229-
if (strchr (*cur, '/') == NULL) {
229+
if (!strchr(*cur, '/'))
230230
continue; /* OK */
231-
}
231+
232232
for (move = cur; NULL != *move; move++) {
233233
*move = *(move + 1);
234234
}

lib/obscure.c

Lines changed: 2 additions & 2 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"
@@ -66,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
6667
}
6768

6869
for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
69-
if (strchr (new, old[i]) != NULL) {
70+
if (strchr(new, old[i]))
7071
j++;
71-
}
7272
}
7373

7474
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 @@ void 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid)
191191
shadow_progname, TCB_DIR, strerror (errno));
192192
goto out_free_path;
193193
}
194-
while ((ind = strchr (ptr, '/'))) {
194+
while (NULL != (ind = strchr(ptr, '/'))) {
195195
stpcpy(ind, "");
196196
if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
197197
OUT_OF_MEMORY;

src/chfn.c

Lines changed: 2 additions & 2 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

@@ -159,9 +160,8 @@ static bool may_change_field (int field)
159160
cp = "frwh";
160161
}
161162

162-
if (strchr (cp, field) != NULL) {
163+
if (strchr(cp, field))
163164
return true;
164-
}
165165

166166
return false;
167167
}

src/chpasswd.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdint.h>
1818
#include <stdio.h>
1919
#include <stdlib.h>
20+
#include <string.h>
2021

2122
#ifdef USE_PAM
2223
#include "pam_defs.h"
@@ -509,10 +510,8 @@ int main (int argc, char **argv)
509510
if (feof (stdin) == 0) {
510511
// Drop all remaining characters on this line.
511512
while (fgets (buf, sizeof buf, stdin) != NULL) {
512-
cp = strchr (buf, '\n');
513-
if (cp != NULL) {
513+
if (strchr(buf, '\n'))
514514
break;
515-
}
516515
}
517516

518517
fprintf (stderr,

src/login_nopam.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ static bool from_match (char *tok, const char *string)
314314
return true;
315315
}
316316
} else if (strcaseeq(tok, "LOCAL")) { /* LOCAL: no dots */
317-
if (strchr (string, '.') == NULL) {
317+
if (!strchr(string, '.'))
318318
return true;
319-
}
320319
} else if ( (!streq(tok, "") && tok[strlen(tok) - 1] == '.') /* network */
321320
&& strprefix(resolve_hostname(string), tok)) {
322321
return true;

src/su.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ static void set_environment (struct passwd *pw)
951951
cp = getdef_str ((pw->pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
952952
if (NULL == cp) {
953953
addenv ((pw->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
954-
} else if (strchr (cp, '=') != NULL) {
954+
} else if (strchr(cp, '=')) {
955955
addenv (cp, NULL);
956956
} else {
957957
addenv ("PATH", cp);

0 commit comments

Comments
 (0)