Skip to content

Commit fdbd965

Browse files
lib/, src/: Use !streq() instead of its pattern
Except for the added (and sorted) includes, the removal of redundant parentheses, and a few non-string cases that I've left out of the change, this patch can be approximated with the following semantic patch: $ cat ~/tmp/spatch/strneq.sp @@ expression s; @@ - '\0' != *s + !streq(s, "") @@ expression s; @@ - '\0' != s[0] + !streq(s, "") @@ expression s; @@ - *s != '\0' + !streq(s, "") @@ expression s; @@ - s[0] != '\0' + !streq(s, "") $ find contrib/ lib* src/ -type f \ | xargs spatch --in-place --sp-file ~/tmp/spatch/strneq.sp; Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 22adffd commit fdbd965

24 files changed

+53
-48
lines changed

lib/chkname.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "defines.h"
3535
#include "chkname.h"
36+
#include "string/strcmp/streq.h"
3637

3738

3839
int allow_bad_names = false;
@@ -85,7 +86,7 @@ is_valid_name(const char *name)
8586

8687
numeric = isdigit(*name);
8788

88-
while ('\0' != *++name) {
89+
while (!streq(++name, "")) {
8990
if (!((*name >= 'a' && *name <= 'z') ||
9091
(*name >= 'A' && *name <= 'Z') ||
9192
(*name >= '0' && *name <= '9') ||

lib/fields.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "prototypes.h"
1919
#include "string/strchr/stpspn.h"
2020
#include "string/strchr/strrspn.h"
21+
#include "string/strcmp/streq.h"
2122
#include "string/strtok/stpsep.h"
2223

2324

@@ -47,7 +48,7 @@ int valid_field (const char *field, const char *illegal)
4748
}
4849

4950
/* Search if there are non-printable or control characters */
50-
for (cp = field; '\0' != *cp; cp++) {
51+
for (cp = field; !streq(cp, ""); cp++) {
5152
unsigned char c = *cp;
5253
if (!isprint (c)) {
5354
err = 1;
@@ -86,7 +87,7 @@ change_field(char *buf, size_t maxsize, const char *prompt)
8687
if (stpsep(newf, "\n") == NULL)
8788
return;
8889

89-
if ('\0' != newf[0]) {
90+
if (!streq(newf, "")) {
9091
/*
9192
* Remove leading and trailing whitespace. This also
9293
* makes it possible to change the field to empty, by

lib/fputsx.c

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

1515
#include "defines.h"
1616
#include "prototypes.h"
17-
18-
#ident "$Id$"
17+
#include "string/strcmp/streq.h"
1918

2019

2120
/*@null@*/char *
@@ -48,7 +47,7 @@ int fputsx (const char *s, FILE * stream)
4847
{
4948
int i;
5049

51-
for (i = 0; '\0' != *s; i++, s++) {
50+
for (i = 0; !streq(s, ""); i++, s++) {
5251
if (putc (*s, stream) == EOF) {
5352
return EOF;
5453
}

lib/getdate.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static int LookupWord (char *buff)
627627
bool abbrev;
628628

629629
/* Make it lowercase. */
630-
for (p = buff; '\0' != *p; p++)
630+
for (p = buff; !streq(p, ""); p++)
631631
if (isupper (*p))
632632
*p = tolower (*p);
633633

@@ -720,7 +720,7 @@ static int LookupWord (char *buff)
720720
}
721721

722722
/* Drop out any periods and try the timezone table again. */
723-
for (i = 0, p = q = buff; '\0' != *q; q++)
723+
for (i = 0, p = q = buff; !streq(q, ""); q++)
724724
if (*q != '.')
725725
*p++ = *q;
726726
else

lib/gshadow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ build_list(char *s)
4242

4343
l = XMALLOC(strchrcnt(s, ',') + 2, char *);
4444

45-
for (i = 0; s != NULL && *s != '\0'; i++)
45+
for (i = 0; s != NULL && !streq(s, ""); i++)
4646
l[i] = strsep(&s, ",");
4747

4848
l[i] = NULL;

lib/limits.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int do_user_limits (const char *buf, const char *name)
203203
pp = "A- C- D- F- I- L- M- N- O- P- R- S- T- U-";
204204
}
205205

206-
while ('\0' != *pp) {
206+
while (!streq(pp, "")) {
207207
switch (*pp++) {
208208
case 'a':
209209
case 'A':

lib/myname.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515

1616
#ident "$Id$"
1717

18-
#include "defines.h"
1918
#include <pwd.h>
19+
20+
#include "defines.h"
2021
#include "prototypes.h"
22+
#include "string/strcmp/streq.h"
23+
24+
2125
/*@null@*/ /*@only@*/struct passwd *get_my_pwent (void)
2226
{
2327
struct passwd *pw;
@@ -34,7 +38,7 @@
3438
* XXX - when running from su, will return the current user (not
3539
* the original user, like getlogin() does). Does this matter?
3640
*/
37-
if ((NULL != cp) && ('\0' != *cp)) {
41+
if ((NULL != cp) && !streq(cp, "")) {
3842
pw = xgetpwnam (cp);
3943
if ((NULL != pw) && (pw->pw_uid == ruid)) {
4044
return pw;

lib/nss.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ nss_init(const char *nsswitch_path) {
8787
continue;
8888
p = &line[6];
8989
p = stpspn(p, " \t\n");
90-
if (*p != '\0')
90+
if (!streq(p, ""))
9191
break;
9292
p = NULL;
9393
}

lib/obscure.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static char *str_lower (/*@returned@*/char *string)
8282
{
8383
char *cp;
8484

85-
for (cp = string; '\0' != *cp; cp++) {
85+
for (cp = string; !streq(cp, ""); cp++) {
8686
*cp = tolower (*cp);
8787
}
8888
return string;

lib/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int portcmp (const char *pattern, const char *port)
3939
{
4040
const char *orig = port;
4141

42-
while (('\0' != *pattern) && (*pattern == *port)) {
42+
while (!streq(pattern, "") && (*pattern == *port)) {
4343
pattern++;
4444
port++;
4545
}
@@ -215,7 +215,7 @@ getportent(void)
215215
* Get the next comma separated entry
216216
*/
217217

218-
for (j = 0; ('\0' != *cp) && (j < PORT_TIMES); j++) {
218+
for (j = 0; !streq(cp, "") && (j < PORT_TIMES); j++) {
219219

220220
/*
221221
* Start off with no days of the week

lib/strtoday.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ long strtoday (const char *str)
5454
s++;
5555
}
5656
s = stpspn(s, " ");
57-
while (isnum && ('\0' != *s)) {
57+
while (isnum && !streq(s, "")) {
5858
if (!isdigit (*s)) {
5959
isnum = false;
6060
}

lib/ttytype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void ttytype (const char *line)
5858
break;
5959
}
6060
}
61-
if ((feof (fp) == 0) && (ferror (fp) == 0) && (type[0] != '\0')) {
61+
if ((feof(fp) == 0) && (ferror(fp) == 0) && !streq(type, "")) {
6262
addenv ("TERM", type);
6363
}
6464

lib/utmp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ prepare_utmp(const char *name, const char *line, const char *host,
254254

255255

256256

257-
if (NULL != host && '\0' != host[0])
257+
if (NULL != host && !streq(host, ""))
258258
hostname = xstrdup(host);
259259
#if defined(HAVE_STRUCT_UTMPX_UT_HOST)
260260
else if (NULL != ut && '\0' != ut->ut_host[0])

src/chfn.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static char *copy_field (char *in, char *out, char *extra)
225225
break;
226226

227227
if (NULL != extra) {
228-
if ('\0' != extra[0]) {
228+
if (!streq(extra, "")) {
229229
strcat (extra, ",");
230230
}
231231

@@ -543,7 +543,7 @@ static void get_old_fields (const char *gecos)
543543
* Anything left over is "slop".
544544
*/
545545
if ((NULL != cp) && !oflg) {
546-
if ('\0' != slop[0]) {
546+
if (!streq(slop, "")) {
547547
strcat (slop, ",");
548548
}
549549

@@ -702,7 +702,7 @@ int main (int argc, char **argv)
702702
}
703703
SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s",
704704
fullnm, roomno, workph, homeph,
705-
('\0' != slop[0]) ? "," : "", slop);
705+
(!streq(slop, "")) ? "," : "", slop);
706706

707707
/* Rewrite the user's gecos in the passwd file */
708708
update_gecos (user, new_gecos);

src/gpasswd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static bool is_valid_user_list (const char *users)
179179

180180
tmpusers = dup = xstrdup(users);
181181

182-
while (NULL != tmpusers && '\0' != *tmpusers) {
182+
while (NULL != tmpusers && !streq(tmpusers, "")) {
183183
const char *u;
184184

185185
u = strsep(&tmpusers, ",");

src/login.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static /*@observer@*/const char *get_failent_user (/*@returned@*/const char *use
422422
const char *failent_user = "UNKNOWN";
423423
bool log_unkfail_enab = getdef_bool("LOG_UNKFAIL_ENAB");
424424

425-
if ((NULL != user) && ('\0' != user[0])) {
425+
if ((NULL != user) && !streq(user, "")) {
426426
if ( log_unkfail_enab
427427
|| (getpwnam (user) != NULL)) {
428428
failent_user = user;
@@ -589,13 +589,13 @@ int main (int argc, char **argv)
589589

590590
if (hflg) {
591591
cp = hostname;
592-
} else if ((host != NULL) && (host[0] != '\0')) {
592+
} else if ((host != NULL) && !streq(host, "")) {
593593
cp = host;
594594
} else {
595595
cp = "";
596596
}
597597

598-
if ('\0' != *cp) {
598+
if (!streq(cp, "")) {
599599
SNPRINTF(fromhost, " on '%.100s' from '%.200s'", tty, cp);
600600
} else {
601601
SNPRINTF(fromhost, " on '%.100s'", tty);
@@ -925,7 +925,7 @@ int main (int argc, char **argv)
925925
failed = true;
926926
}
927927
if ( !failed
928-
&& !login_access (username, ('\0' != *hostname) ? hostname : tty)) {
928+
&& !login_access(username, (!streq(hostname, "")) ? hostname : tty)) {
929929
SYSLOG ((LOG_WARN, "LOGIN '%s' REFUSED %s",
930930
username, fromhost));
931931
failed = true;

src/login_nopam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static bool from_match (const char *tok, const char *string)
320320
if (strchr (string, '.') == NULL) {
321321
return true;
322322
}
323-
} else if ( (tok[0] != '\0' && tok[(tok_len = strlen (tok)) - 1] == '.') /* network */
323+
} else if ( (!streq(tok, "") && tok[(tok_len = strlen(tok)) - 1] == '.') /* network */
324324
&& (strncmp (tok, resolve_hostname (string), tok_len) == 0)) {
325325
return true;
326326
}

src/newgrp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static void check_perms (const struct group *grp,
150150
spw_free (spwd);
151151
}
152152

153-
if (streq(pwd->pw_passwd, "") && (grp->gr_passwd[0] != '\0')) {
153+
if (streq(pwd->pw_passwd, "") && !streq(grp->gr_passwd, "")) {
154154
needspasswd = true;
155155
}
156156

@@ -786,7 +786,7 @@ int main (int argc, char **argv)
786786
cp = getenv ("SHELL");
787787
if (!initflag && (NULL != cp)) {
788788
prog = cp;
789-
} else if ((NULL != pwd->pw_shell) && ('\0' != pwd->pw_shell[0])) {
789+
} else if ((NULL != pwd->pw_shell) && !streq(pwd->pw_shell, "")) {
790790
prog = pwd->pw_shell;
791791
} else {
792792
prog = SHELL;

src/newusers.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
282282
/*
283283
* Now I have all of the fields required to create the new group.
284284
*/
285-
if (('\0' != gid[0]) && (!isdigit (gid[0]))) {
285+
if (!streq(gid, "") && (!isdigit(gid[0]))) {
286286
grent.gr_name = xstrdup (gid);
287287
} else {
288288
grent.gr_name = xstrdup (name);
@@ -355,7 +355,7 @@ static int get_user_id (const char *uid, uid_t *nuid) {
355355
return -1;
356356
}
357357
} else {
358-
if ('\0' != uid[0]) {
358+
if (!streq(uid, "")) {
359359
const struct passwd *pwd;
360360
/* local, no need for xgetpwnam */
361361
pwd = getpwnam (uid);
@@ -1222,19 +1222,19 @@ int main (int argc, char **argv)
12221222
Prog, line);
12231223
fail_exit (EXIT_FAILURE);
12241224
}
1225-
if ('\0' != fields[4][0]) {
1225+
if (!streq(fields[4], "")) {
12261226
newpw.pw_gecos = fields[4];
12271227
}
12281228

1229-
if ('\0' != fields[5][0]) {
1229+
if (!streq(fields[5], "")) {
12301230
newpw.pw_dir = fields[5];
12311231
}
12321232

1233-
if ('\0' != fields[6][0]) {
1233+
if (!streq(fields[6], "")) {
12341234
newpw.pw_shell = fields[6];
12351235
}
12361236

1237-
if ( ('\0' != fields[5][0])
1237+
if ( !streq(fields[5], "")
12381238
&& (access (newpw.pw_dir, F_OK) != 0)) {
12391239
/* FIXME: should check for directory */
12401240
mode_t mode = getdef_num ("HOME_MODE",

src/passwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static int new_password (const struct passwd *pw)
194194
* password.
195195
*/
196196

197-
if (!amroot && ('\0' != crypt_passwd[0])) {
197+
if (!amroot && !streq(crypt_passwd, "")) {
198198
clear = agetpass (_("Old password: "));
199199
if (NULL == clear) {
200200
return -1;

src/pwck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ static void check_pw_file (int *errors, bool *changed)
533533
* Make sure the login shell is executable
534534
*/
535535
if ( !quiet
536-
&& ('\0' != pwd->pw_shell[0])
536+
&& !streq(pwd->pw_shell, "")
537537
&& (access (pwd->pw_shell, F_OK) != 0)) {
538538

539539
/*

src/su.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ int main (int argc, char **argv)
10871087
sulog (caller_tty, true, caller_name, name); /* save SU information */
10881088
if (getdef_bool ("SYSLOG_SU_ENAB")) {
10891089
SYSLOG ((LOG_INFO, "+ %s %s:%s", caller_tty,
1090-
('\0' != caller_name[0]) ? caller_name : "???",
1091-
('\0' != name[0]) ? name : "???"));
1090+
(!streq(caller_name, "")) ? caller_name : "???",
1091+
(!streq(name, "")) ? name : "???"));
10921092
}
10931093

10941094
#ifdef USE_PAM
@@ -1143,7 +1143,7 @@ int main (int argc, char **argv)
11431143
AUDIT_USER_ROLE_CHANGE,
11441144
NULL, /* Prog. name */
11451145
"su",
1146-
('\0' != caller_name[0]) ? caller_name : "???",
1146+
(!streq(caller_name, "")) ? caller_name : "???",
11471147
AUDIT_NO_ID,
11481148
"localhost",
11491149
NULL, /* addr */

src/useradd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static bool
177177
Uflg = false; /* create a group having the same name as the user */
178178

179179
#ifdef WITH_SELINUX
180-
#define Zflg ('\0' != *user_selinux)
180+
#define Zflg (!streq(user_selinux, ""))
181181
#endif /* WITH_SELINUX */
182182

183183
static bool home_added = false;
@@ -1268,7 +1268,7 @@ static void process_flags (int argc, char **argv)
12681268
Dflg = true;
12691269
break;
12701270
case 'e':
1271-
if ('\0' != *optarg) {
1271+
if (!streq(optarg, "")) {
12721272
user_expire = strtoday (optarg);
12731273
if (user_expire < -1) {
12741274
fprintf (stderr,
@@ -1403,15 +1403,15 @@ static void process_flags (int argc, char **argv)
14031403
break;
14041404
case 's':
14051405
if ( ( !VALID (optarg) )
1406-
|| ( ('\0' != optarg[0])
1406+
|| ( !streq(optarg, "")
14071407
&& ('/' != optarg[0])
14081408
&& ('*' != optarg[0]) )) {
14091409
fprintf (stderr,
14101410
_("%s: invalid shell '%s'\n"),
14111411
Prog, optarg);
14121412
exit (E_BAD_ARG);
14131413
}
1414-
if ( '\0' != optarg[0]
1414+
if (!streq(optarg, "")
14151415
&& '*' != optarg[0]
14161416
&& !streq(optarg, "/sbin/nologin")
14171417
&& ( stat(optarg, &st) != 0

0 commit comments

Comments
 (0)