]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use !streq() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Sun, 24 Nov 2024 17:40:48 +0000 (18:40 +0100)
committerSerge Hallyn <serge@hallyn.com>
Mon, 2 Dec 2024 04:23:19 +0000 (22:23 -0600)
Except for the added (and sorted) includes, and the removal of redundant
parentheses, and one special case, this patch can be approximated with
the following semantic patch:

$ cat ~/tmp/spatch/strneq.sp;
@@
expression a, b;
@@

- strcmp(a, b) != 0
+ !streq(a, b)

@@
expression a, b;
@@

- 0 != strcmp(a, b)
+ !streq(a, b)

$ find contrib/ lib* src/ -type f \
| xargs spatch --sp-file ~/tmp/spatch/strneq.sp --in-place;

Signed-off-by: Alejandro Colomar <alx@kernel.org>
16 files changed:
lib/list.c
lib/salt.c
lib/subordinateio.c
lib/tcbfuncs.c
src/chgpasswd.c
src/chpasswd.c
src/groupmod.c
src/grpck.c
src/grpconv.c
src/newgrp.c
src/newusers.c
src/passwd.c
src/pwck.c
src/su.c
src/useradd.c
src/usermod.c

index 466bf7bf4128b3a1ef536eebacf02a8ed6af9d44..a18c71862e4d4c9feea6c15170d2f620686f9a42 100644 (file)
@@ -93,7 +93,7 @@ del_list(/*@returned@*/ /*@only@*/char **list, const char *member)
         */
 
        for (i = j = 0; list[i] != NULL; i++) {
-               if (strcmp (list[i], member) != 0) {
+               if (!streq(list[i], member)) {
                        j++;
                }
        }
@@ -116,7 +116,7 @@ del_list(/*@returned@*/ /*@only@*/char **list, const char *member)
         */
 
        for (i = j = 0; list[i] != NULL; i++) {
-               if (strcmp (list[i], member) != 0) {
+               if (!streq(list[i], member)) {
                        tmp[j] = list[i];
                        j++;
                }
index 8acaa5748478445b7e62d248ec81b854283045e0..f2432512ea27d1e6451b52fd45bab06d9ada9681 100644 (file)
@@ -404,7 +404,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
                rounds = SHA_get_salt_rounds (arg);
                SHA_salt_rounds_to_buf (result, rounds);
 #endif /* USE_SHA_CRYPT */
-       } else if (0 != strcmp (method, "DES")) {
+       } else if (!streq(method, "DES")) {
                fprintf (log_get_logfd(),
                         _("Invalid ENCRYPT_METHOD value: '%s'.\n"
                           "Defaulting to DES.\n"),
index 45085481acfdb5649349a994e0a0be8f7019ed99..9329c455b3791deb51964c2a29a6a13a55bda998 100644 (file)
@@ -197,7 +197,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
                unsigned long first = range->start;
                unsigned long last = first + range->count - 1;
 
-               if (0 != strcmp(range->owner, owner))
+               if (!streq(range->owner, owner))
                        continue;
 
                if ((val >= first) && (val <= last))
@@ -208,7 +208,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
         /*
          * We only do special handling for these two files
          */
-        if ((0 != strcmp(db->filename, SUBUID_FILE)) && (0 != strcmp(db->filename, SUBGID_FILE)))
+        if (!streq(db->filename, SUBUID_FILE) && !streq(db->filename, SUBGID_FILE))
                 return NULL;
 
         /*
@@ -465,7 +465,7 @@ static int remove_range (struct commonio_db *db,
                last = first + range->count - 1;
 
                /* Skip entries with a different owner */
-               if (0 != strcmp (range->owner, owner)) {
+               if (!streq(range->owner, owner)) {
                        continue;
                }
 
@@ -1062,7 +1062,7 @@ bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, b
        if (reuse) {
                while ((r = commonio_next(db)) != NULL) {
                        // TODO account for username vs uid_t
-                       if (0 != strcmp(r->owner, range->owner))
+                       if (!streq(r->owner, range->owner))
                                continue;
                        if (r->count >= range->count) {
                                range->count = r->count;
index c8a6f8d816613dc6c2aee0c86f251985026d163b..6e48db4e67a933d61cf31eb43a8eadc900f54620 100644 (file)
@@ -342,7 +342,7 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
        if (NULL == real_new_dir_rel) {
                goto out_free;
        }
-       if (   (strcmp (real_new_dir, newdir) != 0)
+       if (   !streq(real_new_dir, newdir)
            && (symlink (real_new_dir_rel, newdir) != 0)) {
                fprintf (shadow_logfd,
                         _("%s: Cannot create symbolic link %s: %s\n"),
index 1eb7d1a299499ed806628898f582b000c27f65c9..2d50337e6490e8918ebbf86d081ee62ac911f7ca 100644 (file)
@@ -260,18 +260,18 @@ static void check_flags (void)
        }
 
        if (cflg) {
-               if (   (0 != strcmp (crypt_method, "DES"))
-                   && (0 != strcmp (crypt_method, "MD5"))
-                   && (0 != strcmp (crypt_method, "NONE"))
+               if (   !streq(crypt_method, "DES")
+                   && !streq(crypt_method, "MD5")
+                   && !streq(crypt_method, "NONE")
 #ifdef USE_SHA_CRYPT
-                   && (0 != strcmp (crypt_method, "SHA256"))
-                   && (0 != strcmp (crypt_method, "SHA512"))
+                   && !streq(crypt_method, "SHA256")
+                   && !streq(crypt_method, "SHA512")
 #endif                         /* USE_SHA_CRYPT */
 #ifdef USE_BCRYPT
-                   && (0 != strcmp (crypt_method, "BCRYPT"))
+                   && !streq(crypt_method, "BCRYPT")
 #endif                         /* USE_BCRYPT */
 #ifdef USE_YESCRYPT
-                   && (0 != strcmp (crypt_method, "YESCRYPT"))
+                   && !streq(crypt_method, "YESCRYPT")
 #endif                         /* USE_YESCRYPT */
                    ) {
                        fprintf (stderr,
@@ -490,7 +490,7 @@ int main (int argc, char **argv)
                newpwd = cp;
                if (   (!eflg)
                    && (   (NULL == crypt_method)
-                       || (0 != strcmp (crypt_method, "NONE")))) {
+                       || !streq(crypt_method, "NONE"))) {
                        void *arg = NULL;
                        const char *salt;
                        if (md5flg) {
@@ -577,7 +577,7 @@ int main (int argc, char **argv)
                        newsg.sg_passwd = cp;
                }
                if (   (NULL == sg)
-                   || (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0))
+                   || !streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
 #endif
                {
                        newgr = *gr;
@@ -600,7 +600,7 @@ int main (int argc, char **argv)
                        }
                }
                if (   (NULL == sg)
-                   || (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0))
+                   || !streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
 #endif
                {
                        if (gr_update (&newgr) == 0) {
index 1074d99f585852217a516cf07b746fd7adcb5fa9..dfe5074097eedbc0f19123320cbc27256cb22301 100644 (file)
@@ -627,7 +627,7 @@ int main (int argc, char **argv)
                }
 
                if (   (NULL == sp)
-                   || (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
+                   || !streq(pw->pw_passwd, SHADOW_PASSWD_STRING)) {
                        newpw = *pw;
                        newpw.pw_passwd = cp;
                }
@@ -647,7 +647,7 @@ int main (int argc, char **argv)
                        }
                }
                if (   (NULL == sp)
-                   || (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
+                   || !streq(pw->pw_passwd, SHADOW_PASSWD_STRING)) {
                        if (pw_update (&newpw) == 0) {
                                fprintf (stderr,
                                         _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
index bedbc60673700dba9a8bb310aa16f6b1d26525e9..ec2e6043480baaffa374aa628aa122dc1c177826 100644 (file)
@@ -155,7 +155,7 @@ static void new_grent (struct group *grent)
        if (   pflg
 #ifdef SHADOWGRP
            && (   (!is_shadow_grp)
-               || (strcmp (grent->gr_passwd, SHADOW_PASSWD_STRING) != 0))
+               || !streq(grent->gr_passwd, SHADOW_PASSWD_STRING))
 #endif
                ) {
                /* Update the password in group if there is no gshadow
index a97e756c55f22fbdab86b8017697b0dc70eae9db..405ae6c5e5014cd3b1464fd197457a1c5a880d1e 100644 (file)
@@ -537,7 +537,7 @@ static void check_grp_file (int *errors, bool *changed)
                                continue;
                        }
 
-                       if (strcmp (grp->gr_name, ent->gr_name) != 0) {
+                       if (!streq(grp->gr_name, ent->gr_name)) {
                                continue;
                        }
 
@@ -649,7 +649,7 @@ static void check_grp_file (int *errors, bool *changed)
                                /* The group entry has a gshadow counterpart.
                                 * Make sure no passwords are in group.
                                 */
-                               if (strcmp (grp->gr_passwd, SHADOW_PASSWD_STRING) != 0) {
+                               if (!streq(grp->gr_passwd, SHADOW_PASSWD_STRING)) {
                                        printf (_("group %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
                                                grp->gr_name, sgr_file, grp_file);
                                        *errors += 1;
@@ -739,7 +739,7 @@ static void check_sgr_file (int *errors, bool *changed)
                                continue;
                        }
 
-                       if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
+                       if (!streq(sgr->sg_name, ent->sg_name)) {
                                continue;
                        }
 
index ea236e4ed3cdc172041c8cbb2db13a942dadb807..16cd52c9a281e69ffdcd651998918216cfeff840 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <grp.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include <time.h>
 #include <unistd.h>
-#include <getopt.h>
 
 #include "attr.h"
-#include "nscd.h"
-#include "sssd.h"
-#include "prototypes.h"
 /*@-exitarg@*/
 #include "exitcodes.h"
+#include "nscd.h"
+#include "prototypes.h"
+#include "string/strcmp/streq.h"
+
 #ifdef SHADOWGRP
 #include "groupio.h"
 #include "sgroupio.h"
 #include "shadowlog.h"
+#include "sssd.h"
+
+
 /*
  * Global variables
  */
@@ -194,7 +198,7 @@ int main (int argc, char **argv)
                if (NULL != sg) {
                        /* update existing shadow group entry */
                        sgent = *sg;
-                       if (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0)
+                       if (!streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
                                sgent.sg_passwd = gr->gr_passwd;
                } else {
                        static char *empty = NULL;
index 4955ebcefcd9f6c02d16550f550a28d259e6cd0a..427ae586a2cb7f9ea9745acca5b31209cf0be5e4 100644 (file)
@@ -75,7 +75,7 @@ static bool ingroup(const char *name, struct group *gr)
 
        look = gr->gr_mem;
        while (*look && notfound)
-               notfound = strcmp (*look++, name);
+               notfound = !streq(*look++, name);
 
        return !notfound;
 }
@@ -189,7 +189,7 @@ static void check_perms (const struct group *grp,
                }
 
                if (grp->gr_passwd[0] == '\0' ||
-                   strcmp (cpasswd, grp->gr_passwd) != 0) {
+                   !streq(cpasswd, grp->gr_passwd)) {
 #ifdef WITH_AUDIT
                        SNPRINTF(audit_buf, "authentication new-gid=%lu",
                                 (unsigned long) grp->gr_gid);
index eee0d260d9ea95a8fd804fb96d33117bf9c2d0ea..32d224d20c1f3a765d1c71313b239a66bf0ac0f3 100644 (file)
@@ -557,7 +557,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
         * when the entry was created, so this user would have to have had
         * the password set someplace else.
         */
-       if (strcmp (pwd->pw_passwd, "x") != 0) {
+       if (!streq(pwd->pw_passwd, "x")) {
                return update_passwd (pwd, password);
        }
 #else                          /* USE_PAM */
@@ -568,7 +568,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
         * The password will be updated later for all users using PAM.
         */
        if (   (NULL != sp)
-           || (strcmp (pwd->pw_passwd, "x") != 0)) {
+           || !streq(pwd->pw_passwd, "x")) {
                return 0;
        }
 #endif                         /* USE_PAM */
@@ -754,18 +754,18 @@ static void check_flags (void)
 #endif                         /* USE_SHA_CRYPT || USE_BCRYPT || USE_YESCRYPT */
 
        if (cflg) {
-               if (   (0 != strcmp (crypt_method, "DES"))
-                   && (0 != strcmp (crypt_method, "MD5"))
-                   && (0 != strcmp (crypt_method, "NONE"))
+               if (   !streq(crypt_method, "DES")
+                   && !streq(crypt_method, "MD5")
+                   && !streq(crypt_method, "NONE")
 #ifdef USE_SHA_CRYPT
-                   && (0 != strcmp (crypt_method, "SHA256"))
-                   && (0 != strcmp (crypt_method, "SHA512"))
+                   && !streq(crypt_method, "SHA256")
+                   && !streq(crypt_method, "SHA512")
 #endif                         /* USE_SHA_CRYPT */
 #ifdef USE_BCRYPT
-                   && (0 != strcmp (crypt_method, "BCRYPT"))
+                   && !streq(crypt_method, "BCRYPT")
 #endif                         /* USE_BCRYPT */
 #ifdef USE_YESCRYPT
-                   && (0 != strcmp (crypt_method, "YESCRYPT"))
+                   && !streq(crypt_method, "YESCRYPT")
 #endif                         /* USE_YESCRYPT */
                    ) {
                        fprintf (stderr,
index a201e5d0db6def8bd376967e9f56454e90840c11..e75b0b94f26e1ab8af44a827f4086f3f844fff21 100644 (file)
@@ -213,7 +213,7 @@ static int new_password (const struct passwd *pw)
                        return -1;
                }
 
-               if (strcmp (cipher, crypt_passwd) != 0) {
+               if (!streq(cipher, crypt_passwd)) {
                        erase_pass (clear);
                        strzero (cipher);
                        SYSLOG ((LOG_WARN, "incorrect password for %s",
@@ -299,7 +299,7 @@ static int new_password (const struct passwd *pw)
                                MEMZERO(pass);
                                return -1;
                        }
-                       if (warned && (strcmp (pass, cp) != 0)) {
+                       if (warned && !streq(pass, cp)) {
                                warned = false;
                        }
                        ret = STRTCPY (pass, cp);
@@ -333,7 +333,7 @@ static int new_password (const struct passwd *pw)
                                MEMZERO(pass);
                                return -1;
                        }
-                       if (strcmp (cp, pass) != 0) {
+                       if (!streq(cp, pass)) {
                                erase_pass (cp);
                                (void) fputs (_("They don't match; try again.\n"), stderr);
                        } else {
@@ -839,7 +839,7 @@ main(int argc, char **argv)
                        case 'r':
                                /* -r repository (files|nis|nisplus) */
                                /* only "files" supported for now */
-                               if (strcmp (optarg, "files") != 0) {
+                               if (!streq(optarg, "files")) {
                                        fprintf (stderr,
                                                 _("%s: repository %s not supported\n"),
                                                 Prog, optarg);
index 2df5b6c6fe8d2094a47e788e4f5a51aaf612751e..271a2c21b048a0382d21492ff8a1e93309e859e5 100644 (file)
 #ident "$Id$"
 
 #include <fcntl.h>
+#include <getopt.h>
 #include <grp.h>
 #include <pwd.h>
 #include <stdio.h>
-#include <getopt.h>
+
 #include "chkname.h"
 #include "commonio.h"
 #include "defines.h"
+#include "getdef.h"
+#include "nscd.h"
 #include "prototypes.h"
 #include "pwio.h"
 #include "shadowio.h"
-#include "getdef.h"
-#include "nscd.h"
+#include "shadowlog.h"
 #include "sssd.h"
+#include "string/strcmp/streq.h"
 #ifdef WITH_TCB
 #include "tcbfuncs.h"
 #endif                         /* WITH_TCB */
-#include "shadowlog.h"
+
 
 /*
  * Exit codes
@@ -447,7 +450,7 @@ static void check_pw_file (int *errors, bool *changed)
                                continue;
                        }
 
-                       if (strcmp (pwd->pw_name, ent->pw_name) != 0) {
+                       if (!streq(pwd->pw_name, ent->pw_name)) {
                                continue;
                        }
 
@@ -518,7 +521,7 @@ static void check_pw_file (int *errors, bool *changed)
                                /*
                                 * Home directory does not exist, give a warning (unless intentional)
                                 */
-                               if (NULL == nonexistent || strcmp (pwd->pw_dir, nonexistent) != 0) {
+                               if (NULL == nonexistent || !streq(pwd->pw_dir, nonexistent)) {
                                        printf (_("user '%s': directory '%s' does not exist\n"),
                                                        pwd->pw_name, pwd->pw_dir);
                                        *errors += 1;
@@ -644,8 +647,7 @@ static void check_pw_file (int *errors, bool *changed)
                                 * Make sure no passwords are in passwd.
                                 */
                                if (   !quiet
-                                   && (strcmp (pwd->pw_passwd,
-                                               SHADOW_PASSWD_STRING) != 0)) {
+                                   && !streq(pwd->pw_passwd, SHADOW_PASSWD_STRING)) {
                                        printf (_("user %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
                                                pwd->pw_name, spw_dbname (), pw_dbname ());
                                        *errors += 1;
@@ -773,7 +775,7 @@ static void check_spw_file (int *errors, bool *changed)
                                continue;
                        }
 
-                       if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
+                       if (!streq(spw->sp_namp, ent->sp_namp)) {
                                continue;
                        }
 
index 6c1fdc5bc6f44ec2b03eb6ed7ef15cc52d14c097..67195ea0dc5530cab2d7fa054240e1d8914856e8 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -681,7 +681,7 @@ static /*@only@*/struct passwd * do_check_perms (void)
                su_failure (caller_tty, 0 == pw->pw_uid);
        }
        tmp_name = item;
-       if (strcmp (name, tmp_name) != 0) {
+       if (!streq(name, tmp_name)) {
                SYSLOG ((LOG_INFO,
                         "Change user from '%s' to '%s' as requested by PAM",
                         name, tmp_name));
index 9fc3db90a47167b1f6e800ac3ef7331573b69c77..058b09d929d185b1ae0f026969d8ce2a1a580799 100644 (file)
@@ -1413,7 +1413,7 @@ static void process_flags (int argc, char **argv)
                                }
                                if (    '\0' != optarg[0]
                                     && '*'  != optarg[0]
-                                    && strcmp(optarg, "/sbin/nologin") != 0
+                                    && !streq(optarg, "/sbin/nologin")
                                     && (   stat(optarg, &st) != 0
                                         || S_ISDIR(st.st_mode)
                                         || access(optarg, X_OK) != 0)) {
index 4cde39bef17c23f93329ffe4be548df88af684a3..8cff1d67f8db140283dfca3e74c190ab729c8eb1 100644 (file)
@@ -496,7 +496,7 @@ static void new_pwent (struct passwd *pwent)
         * used for this account.
         */
        if (   (!is_shadow_pwd)
-           || (strcmp (pwent->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
+           || !streq(pwent->pw_passwd, SHADOW_PASSWD_STRING)) {
                pwent->pw_passwd = new_pw_passwd (pwent->pw_passwd);
        }
 
@@ -1164,7 +1164,7 @@ process_flags(int argc, char **argv)
                                }
                                if (    '\0' != optarg[0]
                                     && '*'  != optarg[0]
-                                    && strcmp(optarg, "/sbin/nologin") != 0
+                                    && !streq(optarg, "/sbin/nologin")
                                     && (   stat(optarg, &st) != 0
                                         || S_ISDIR(st.st_mode)
                                         || access(optarg, X_OK) != 0)) {