]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use !streq() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Fri, 6 Dec 2024 00:59:23 +0000 (01:59 +0100)
committerSerge Hallyn <serge@hallyn.com>
Tue, 10 Dec 2024 02:54:42 +0000 (20:54 -0600)
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 <alx@kernel.org>
24 files changed:
lib/chkname.c
lib/fields.c
lib/fputsx.c
lib/getdate.y
lib/gshadow.c
lib/limits.c
lib/myname.c
lib/nss.c
lib/obscure.c
lib/port.c
lib/strtoday.c
lib/ttytype.c
lib/utmp.c
src/chfn.c
src/gpasswd.c
src/login.c
src/login_nopam.c
src/newgrp.c
src/newusers.c
src/passwd.c
src/pwck.c
src/su.c
src/useradd.c
src/usermod.c

index 8bde7a2d96ec25b72dafcc6d3adc631fe5af3c65..98f7917062dd46c7a3ff349310720f597df52acb 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "defines.h"
 #include "chkname.h"
+#include "string/strcmp/streq.h"
 
 
 int allow_bad_names = false;
@@ -85,7 +86,7 @@ is_valid_name(const char *name)
 
        numeric = isdigit(*name);
 
-       while ('\0' != *++name) {
+       while (!streq(++name, "")) {
                if (!((*name >= 'a' && *name <= 'z') ||
                      (*name >= 'A' && *name <= 'Z') ||
                      (*name >= '0' && *name <= '9') ||
index b4aa868c816b9c0916d282582432310c1dd9c1e0..ada6726e7f63139436c48a8bec940fc57be0ace8 100644 (file)
@@ -18,6 +18,7 @@
 #include "prototypes.h"
 #include "string/strchr/stpspn.h"
 #include "string/strchr/strrspn.h"
+#include "string/strcmp/streq.h"
 #include "string/strtok/stpsep.h"
 
 
@@ -47,7 +48,7 @@ int valid_field (const char *field, const char *illegal)
        }
 
        /* Search if there are non-printable or control characters */
-       for (cp = field; '\0' != *cp; cp++) {
+       for (cp = field; !streq(cp, ""); cp++) {
                unsigned char c = *cp;
                if (!isprint (c)) {
                        err = 1;
@@ -86,7 +87,7 @@ change_field(char *buf, size_t maxsize, const char *prompt)
        if (stpsep(newf, "\n") == NULL)
                return;
 
-       if ('\0' != newf[0]) {
+       if (!streq(newf, "")) {
                /*
                 * Remove leading and trailing whitespace.  This also
                 * makes it possible to change the field to empty, by
index a43c2ac139e8cf0a29300d591e169d81647935fb..96aea27897ad6ade66e791a9dcda5740aa28d0d4 100644 (file)
@@ -14,8 +14,7 @@
 
 #include "defines.h"
 #include "prototypes.h"
-
-#ident "$Id$"
+#include "string/strcmp/streq.h"
 
 
 /*@null@*/char *
@@ -48,7 +47,7 @@ int fputsx (const char *s, FILE * stream)
 {
        int i;
 
-       for (i = 0; '\0' != *s; i++, s++) {
+       for (i = 0; !streq(s, ""); i++, s++) {
                if (putc (*s, stream) == EOF) {
                        return EOF;
                }
index 9a7e1d36fb2053a6f0d16f55189ff3520683b26f..a6f9bc674921d57acbb7e39191bf1e4726992435 100644 (file)
@@ -627,7 +627,7 @@ static int LookupWord (char *buff)
   bool abbrev;
 
   /* Make it lowercase. */
-  for (p = buff; '\0' != *p; p++)
+  for (p = buff; !streq(p, ""); p++)
     if (isupper (*p))
       *p = tolower (*p);
 
@@ -720,7 +720,7 @@ static int LookupWord (char *buff)
     }
 
   /* Drop out any periods and try the timezone table again. */
-  for (i = 0, p = q = buff; '\0' != *q; q++)
+  for (i = 0, p = q = buff; !streq(q, ""); q++)
     if (*q != '.')
       *p++ = *q;
     else
index 16c432b65f71c8238276aff528e570a6fce43290..e66704a2f367e7be9a550e5a092e2df42b2dc161 100644 (file)
@@ -42,7 +42,7 @@ build_list(char *s)
 
        l = XMALLOC(strchrcnt(s, ',') + 2, char *);
 
-       for (i = 0; s != NULL && *s != '\0'; i++)
+       for (i = 0; s != NULL && !streq(s, ""); i++)
                l[i] = strsep(&s, ",");
 
        l[i] = NULL;
index e1c3b179789921d9928277db5ae71076db30ca8a..9fb1a1ffb33029290893df443a3dd0f28fa40472 100644 (file)
@@ -203,7 +203,7 @@ static int do_user_limits (const char *buf, const char *name)
                pp = "A- C- D- F- I- L- M- N- O- P- R- S- T- U-";
        }
 
-       while ('\0' != *pp) {
+       while (!streq(pp, "")) {
                switch (*pp++) {
                case 'a':
                case 'A':
index 1b0261791edef794cdf732ea13b2fb58e8fb7a8c..fab5d6b52c2cc0b7caf968fc76eb2f4c43a12a7a 100644 (file)
 
 #ident "$Id$"
 
-#include "defines.h"
 #include <pwd.h>
+
+#include "defines.h"
 #include "prototypes.h"
+#include "string/strcmp/streq.h"
+
+
 /*@null@*/ /*@only@*/struct passwd *get_my_pwent (void)
 {
        struct passwd *pw;
@@ -34,7 +38,7 @@
         * XXX - when running from su, will return the current user (not
         * the original user, like getlogin() does).  Does this matter?
         */
-       if ((NULL != cp) && ('\0' != *cp)) {
+       if ((NULL != cp) && !streq(cp, "")) {
                pw = xgetpwnam (cp);
                if ((NULL != pw) && (pw->pw_uid == ruid)) {
                        return pw;
index 9373bba3328ef1f6ae64e02d76ff9b485f1f5097..f1cf1351b627c3968b19ac316fb21e55d6f3491c 100644 (file)
--- a/lib/nss.c
+++ b/lib/nss.c
@@ -87,7 +87,7 @@ nss_init(const char *nsswitch_path) {
                        continue;
                p = &line[6];
                p = stpspn(p, " \t\n");
-               if (*p != '\0')
+               if (!streq(p, ""))
                        break;
                p = NULL;
        }
index 66b2361c3953863f4dcb4c2d8bf608cba0799210..d508200f4df6b3ddc7959cd131f85fed4943414e 100644 (file)
@@ -82,7 +82,7 @@ static char *str_lower (/*@returned@*/char *string)
 {
        char *cp;
 
-       for (cp = string; '\0' != *cp; cp++) {
+       for (cp = string; !streq(cp, ""); cp++) {
                *cp = tolower (*cp);
        }
        return string;
index 4f5d06066a3fedb58c681ca4dae5f5837a471276..b19ecd6be5c2ce0caed3bd01d98c897ade033fbb 100644 (file)
@@ -39,7 +39,7 @@ static int portcmp (const char *pattern, const char *port)
 {
        const char *orig = port;
 
-       while (('\0' != *pattern) && (*pattern == *port)) {
+       while (!streq(pattern, "") && (*pattern == *port)) {
                pattern++;
                port++;
        }
@@ -215,7 +215,7 @@ next:
         * Get the next comma separated entry
         */
 
-       for (j = 0; ('\0' != *cp) && (j < PORT_TIMES); j++) {
+       for (j = 0; !streq(cp, "") && (j < PORT_TIMES); j++) {
 
                /*
                 * Start off with no days of the week
index 056fa931e9cd77a7832b942d38e44b5648343b45..01f2e9b7edf54363db2a18ba9a3708dffdaf3af9 100644 (file)
@@ -54,7 +54,7 @@ long strtoday (const char *str)
                s++;
        }
        s = stpspn(s, " ");
-       while (isnum && ('\0' != *s)) {
+       while (isnum && !streq(s, "")) {
                if (!isdigit (*s)) {
                        isnum = false;
                }
index fbe71ee001772982aa9f1d0b52dee6244893cc73..740510421be350f62ade072c366b733e411dc45d 100644 (file)
@@ -58,7 +58,7 @@ void ttytype (const char *line)
                        break;
                }
        }
-       if ((feof (fp) == 0) && (ferror (fp) == 0) && (type[0] != '\0')) {
+       if ((feof(fp) == 0) && (ferror(fp) == 0) && !streq(type, "")) {
                addenv ("TERM", type);
        }
 
index 2b59dc6d9b6010deece47031840cc1597fff09b5..6dc1f5df29597601623c4ad6ef191fef04c53151 100644 (file)
@@ -254,7 +254,7 @@ prepare_utmp(const char *name, const char *line, const char *host,
 
 
 
-       if (NULL != host && '\0' != host[0])
+       if (NULL != host && !streq(host, ""))
                hostname = xstrdup(host);
 #if defined(HAVE_STRUCT_UTMPX_UT_HOST)
        else if (NULL != ut && '\0' != ut->ut_host[0])
index 2bb2463825dd0353c5636c240f1c2371f2c56260..4c96fba28879600dd5b6a4fe165bfab528c94841 100644 (file)
@@ -225,7 +225,7 @@ static char *copy_field (char *in, char *out, char *extra)
                        break;
 
                if (NULL != extra) {
-                       if ('\0' != extra[0]) {
+                       if (!streq(extra, "")) {
                                strcat (extra, ",");
                        }
 
@@ -543,7 +543,7 @@ static void get_old_fields (const char *gecos)
         * Anything left over is "slop".
         */
        if ((NULL != cp) && !oflg) {
-               if ('\0' != slop[0]) {
+               if (!streq(slop, "")) {
                        strcat (slop, ",");
                }
 
@@ -702,7 +702,7 @@ int main (int argc, char **argv)
        }
        SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s",
                 fullnm, roomno, workph, homeph,
-                ('\0' != slop[0]) ? "," : "", slop);
+                (!streq(slop, "")) ? "," : "", slop);
 
        /* Rewrite the user's gecos in the passwd file */
        update_gecos (user, new_gecos);
index d129aca21c3e20d91821a15323845d01065a063d..560b0ea796b676beca90eb9b8b3b05c3ba4414cc 100644 (file)
@@ -179,7 +179,7 @@ static bool is_valid_user_list (const char *users)
 
        tmpusers = dup = xstrdup(users);
 
-       while (NULL != tmpusers && '\0' != *tmpusers) {
+       while (NULL != tmpusers && !streq(tmpusers, "")) {
                const char  *u;
 
                u = strsep(&tmpusers, ",");
index 9cd1688c998b1e78972325a67fc6018587135da0..eafd498dda6529c4f288a14a3811f91a6698b850 100644 (file)
@@ -422,7 +422,7 @@ static /*@observer@*/const char *get_failent_user (/*@returned@*/const char *use
        const char *failent_user = "UNKNOWN";
        bool log_unkfail_enab = getdef_bool("LOG_UNKFAIL_ENAB");
 
-       if ((NULL != user) && ('\0' != user[0])) {
+       if ((NULL != user) && !streq(user, "")) {
                if (   log_unkfail_enab
                    || (getpwnam (user) != NULL)) {
                        failent_user = user;
@@ -589,13 +589,13 @@ int main (int argc, char **argv)
 
        if (hflg) {
                cp = hostname;
-       } else if ((host != NULL) && (host[0] != '\0')) {
+       } else if ((host != NULL) && !streq(host, "")) {
                cp = host;
        } else {
                cp = "";
        }
 
-       if ('\0' != *cp) {
+       if (!streq(cp, "")) {
                SNPRINTF(fromhost, " on '%.100s' from '%.200s'", tty, cp);
        } else {
                SNPRINTF(fromhost, " on '%.100s'", tty);
@@ -925,7 +925,7 @@ int main (int argc, char **argv)
                        failed = true;
                }
                if (   !failed
-                   && !login_access (username, ('\0' != *hostname) ? hostname : tty)) {
+                   && !login_access(username, (!streq(hostname, "")) ? hostname : tty)) {
                        SYSLOG ((LOG_WARN, "LOGIN '%s' REFUSED %s",
                                 username, fromhost));
                        failed = true;
index fd058a6565ad5c435e1b09a6249ae77a4f2bfcae..65e8c06d9602218168082b730faac92e03783105 100644 (file)
@@ -320,7 +320,7 @@ static bool from_match (const char *tok, const char *string)
                if (strchr (string, '.') == NULL) {
                        return true;
                }
-       } else if (   (tok[0] != '\0' && tok[(tok_len = strlen (tok)) - 1] == '.') /* network */
+       } else if (   (!streq(tok, "") && tok[(tok_len = strlen(tok)) - 1] == '.') /* network */
                   && (strncmp (tok, resolve_hostname (string), tok_len) == 0)) {
                return true;
        }
index 17f961126d4b9acb0e612544874194ce1669b646..f73508663c8102c87d1874fd5ad4343eeba5acc2 100644 (file)
@@ -150,7 +150,7 @@ static void check_perms (const struct group *grp,
                spw_free (spwd);
        }
 
-       if (streq(pwd->pw_passwd, "") && (grp->gr_passwd[0] != '\0')) {
+       if (streq(pwd->pw_passwd, "") && !streq(grp->gr_passwd, "")) {
                needspasswd = true;
        }
 
@@ -786,7 +786,7 @@ int main (int argc, char **argv)
        cp = getenv ("SHELL");
        if (!initflag && (NULL != cp)) {
                prog = cp;
-       } else if ((NULL != pwd->pw_shell) && ('\0' != pwd->pw_shell[0])) {
+       } else if ((NULL != pwd->pw_shell) && !streq(pwd->pw_shell, "")) {
                prog = pwd->pw_shell;
        } else {
                prog = SHELL;
index 32d224d20c1f3a765d1c71313b239a66bf0ac0f3..5e78dd9760679190f4546e7dfa042ccab1b3d511 100644 (file)
@@ -282,7 +282,7 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
        /*
         * Now I have all of the fields required to create the new group.
         */
-       if (('\0' != gid[0]) && (!isdigit (gid[0]))) {
+       if (!streq(gid, "") && (!isdigit(gid[0]))) {
                grent.gr_name = xstrdup (gid);
        } else {
                grent.gr_name = xstrdup (name);
@@ -355,7 +355,7 @@ static int get_user_id (const char *uid, uid_t *nuid) {
                        return -1;
                }
        } else {
-               if ('\0' != uid[0]) {
+               if (!streq(uid, "")) {
                        const struct passwd *pwd;
                        /* local, no need for xgetpwnam */
                        pwd = getpwnam (uid);
@@ -1222,19 +1222,19 @@ int main (int argc, char **argv)
                                 Prog, line);
                        fail_exit (EXIT_FAILURE);
                }
-               if ('\0' != fields[4][0]) {
+               if (!streq(fields[4], "")) {
                        newpw.pw_gecos = fields[4];
                }
 
-               if ('\0' != fields[5][0]) {
+               if (!streq(fields[5], "")) {
                        newpw.pw_dir = fields[5];
                }
 
-               if ('\0' != fields[6][0]) {
+               if (!streq(fields[6], "")) {
                        newpw.pw_shell = fields[6];
                }
 
-               if (   ('\0' != fields[5][0])
+               if (   !streq(fields[5], "")
                    && (access (newpw.pw_dir, F_OK) != 0)) {
 /* FIXME: should check for directory */
                        mode_t mode = getdef_num ("HOME_MODE",
index be038172cf0f20f37ca88e1d453149c254005223..cc79960a54026aae1855818aa3294e4a084a0539 100644 (file)
@@ -194,7 +194,7 @@ static int new_password (const struct passwd *pw)
         * password.
         */
 
-       if (!amroot && ('\0' != crypt_passwd[0])) {
+       if (!amroot && !streq(crypt_passwd, "")) {
                clear = agetpass (_("Old password: "));
                if (NULL == clear) {
                        return -1;
index 271a2c21b048a0382d21492ff8a1e93309e859e5..ae7ddaddf9f69b4f0822ed6ffe9e0add8ad1d631 100644 (file)
@@ -533,7 +533,7 @@ static void check_pw_file (int *errors, bool *changed)
                 * Make sure the login shell is executable
                 */
                if (   !quiet
-                   && ('\0' != pwd->pw_shell[0])
+                   && !streq(pwd->pw_shell, "")
                    && (access (pwd->pw_shell, F_OK) != 0)) {
 
                        /*
index 653b58beb9ad2c2cebe3c2cd34d86b1bd2174850..b90b6f40053bde17084f116ba1ca84088d6c0b56 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -1087,8 +1087,8 @@ int main (int argc, char **argv)
        sulog (caller_tty, true, caller_name, name);    /* save SU information */
        if (getdef_bool ("SYSLOG_SU_ENAB")) {
                SYSLOG ((LOG_INFO, "+ %s %s:%s", caller_tty,
-                        ('\0' != caller_name[0]) ? caller_name : "???",
-                        ('\0' != name[0]) ? name : "???"));
+                        (!streq(caller_name, "")) ? caller_name : "???",
+                        (!streq(name, "")) ? name : "???"));
        }
 
 #ifdef USE_PAM
@@ -1143,7 +1143,7 @@ int main (int argc, char **argv)
                                AUDIT_USER_ROLE_CHANGE,
                                NULL,    /* Prog. name */
                                "su",
-                               ('\0' != caller_name[0]) ? caller_name : "???",
+                               (!streq(caller_name, "")) ? caller_name : "???",
                                AUDIT_NO_ID,
                                "localhost",
                                NULL,    /* addr */
index a9944eef6ca5c963e7e979de40fe8f4034a5be7b..7623dabd4a49fd79d17ac31b2a0932217f62547b 100644 (file)
@@ -177,7 +177,7 @@ static bool
     Uflg = false;              /* create a group having the same name as the user */
 
 #ifdef WITH_SELINUX
-#define Zflg ('\0' != *user_selinux)
+#define Zflg  (!streq(user_selinux, ""))
 #endif                         /* WITH_SELINUX */
 
 static bool home_added = false;
@@ -1268,7 +1268,7 @@ static void process_flags (int argc, char **argv)
                                Dflg = true;
                                break;
                        case 'e':
-                               if ('\0' != *optarg) {
+                               if (!streq(optarg, "")) {
                                        user_expire = strtoday (optarg);
                                        if (user_expire < -1) {
                                                fprintf (stderr,
@@ -1403,7 +1403,7 @@ static void process_flags (int argc, char **argv)
                                break;
                        case 's':
                                if (   ( !VALID (optarg) )
-                                   || (   ('\0' != optarg[0])
+                                   || (   !streq(optarg, "")
                                        && ('/'  != optarg[0])
                                        && ('*'  != optarg[0]) )) {
                                        fprintf (stderr,
@@ -1411,7 +1411,7 @@ static void process_flags (int argc, char **argv)
                                                 Prog, optarg);
                                        exit (E_BAD_ARG);
                                }
-                               if (    '\0' != optarg[0]
+                               if (!streq(optarg, "")
                                     && '*'  != optarg[0]
                                     && !streq(optarg, "/sbin/nologin")
                                     && (   stat(optarg, &st) != 0
index 69a19ff2728e48b43848847b29d0017887d5cf91..24c5a4d23c23928fb74232c8be55c9dde57e16cd 100644 (file)
@@ -1154,7 +1154,7 @@ process_flags(int argc, char **argv)
                                break;
                        case 's':
                                if (   ( !VALID (optarg) )
-                                   || (   ('\0' != optarg[0])
+                                   || (   !streq(optarg, "")
                                        && ('/'  != optarg[0])
                                        && ('*'  != optarg[0]) )) {
                                        fprintf (stderr,
@@ -1162,7 +1162,7 @@ process_flags(int argc, char **argv)
                                                 Prog, optarg);
                                        exit (E_BAD_ARG);
                                }
-                               if (    '\0' != optarg[0]
+                               if (!streq(optarg, "")
                                     && '*'  != optarg[0]
                                     && !streq(optarg, "/sbin/nologin")
                                     && (   stat(optarg, &st) != 0
@@ -2334,7 +2334,7 @@ int main (int argc, char **argv)
 
 #ifdef WITH_SELINUX
        if (Zflg) {
-               if ('\0' != *user_selinux) {
+               if (!streq(user_selinux, "")) {
                        if (set_seuser (user_name, user_selinux, user_selinux_range) != 0) {
                                fprintf (stderr,
                                         _("%s: warning: the user name %s to %s SELinux user mapping failed.\n"),