From: Alejandro Colomar Date: Tue, 10 Dec 2024 13:27:02 +0000 (+0100) Subject: lib/, src/: Use strprefix() instead of its pattern X-Git-Tag: 4.18.0-rc1~74 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d1cbf68f61b182af3f2209458756b60f62f304ad;p=thirdparty%2Fshadow.git lib/, src/: Use strprefix() instead of its pattern Signed-off-by: Alejandro Colomar --- diff --git a/contrib/adduser.c b/contrib/adduser.c index 8061c76a4..8efec7aa3 100644 --- a/contrib/adduser.c +++ b/contrib/adduser.c @@ -119,6 +119,7 @@ #include #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #define IMMEDIATE_CHANGE /* Expire newly created password, must be changed @@ -389,7 +390,7 @@ main (void) fflush (stdout); safeget (foo, sizeof (foo)); - done = bad = correct = (foo[0] == 'y' || foo[0] == 'Y'); + done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y")); if (bad != 1) printf ("\nUser [%s] not added\n", usrname); diff --git a/lib/commonio.c b/lib/commonio.c index fc8ad7a57..38bf65c59 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -37,6 +37,7 @@ #include "string/memset/memzero.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" @@ -515,7 +516,7 @@ static void add_one_entry (struct commonio_db *db, static bool name_is_nis (const char *name) { - return (('+' == name[0]) || ('-' == name[0])); + return strprefix(name, "+") || strprefix(name, "-"); } diff --git a/lib/encrypt.c b/lib/encrypt.c index 9c1cb4067..1abe0a623 100644 --- a/lib/encrypt.c +++ b/lib/encrypt.c @@ -17,6 +17,8 @@ #include "prototypes.h" #include "defines.h" #include "shadowlog_internal.h" +#include "string/strcmp/strprefix.h" + /*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt) { @@ -35,7 +37,7 @@ /* Some crypt() do not return NULL if the algorithm is not * supported, and return a DES encrypted password. */ - if ((NULL != salt) && (salt[0] == '$') && (strlen (cp) <= 13)) + if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13)) { /*@observer@*/const char *method; switch (salt[1]) diff --git a/lib/getdef.c b/lib/getdef.c index 8883a29cc..f963e5bc8 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -32,6 +32,7 @@ #include "string/sprintf/xasprintf.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -569,7 +570,7 @@ static void def_load (void) * Break the line into two fields. */ name = stpspn(buf, " \t"); /* first nonwhite */ - if (streq(name, "") || *name == '#') + if (streq(name, "") || strprefix(name, "#")) continue; /* comment or empty */ s = stpsep(name, " \t"); /* next field */ diff --git a/lib/limits.c b/lib/limits.c index 43a25967b..55a5fa9d9 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -63,7 +63,7 @@ static int setrlimit_value (unsigned int resource, /* The "-" is special, not belonging to a strange negative limit. * It is infinity, in a controlled way. */ - if ('-' == value[0]) { + if (strprefix(value, "-")) { limit = RLIM_INFINITY; } else { @@ -371,7 +371,7 @@ static int setup_user_limits (const char *uname) * FIXME: A better (smarter) checking should be done */ while (fgets (buf, 1024, fil) != NULL) { - if (('#' == buf[0]) || ('\n' == buf[0])) { + if (strprefix(buf, "#") || strprefix(buf, "\n")) { continue; } MEMZERO(tempbuf); @@ -402,7 +402,7 @@ static int setup_user_limits (const char *uname) break; } else if (streq(name, "*")) { strcpy (deflimits, tempbuf); - } else if (name[0] == '@') { + } else if (strprefix(name, "@")) { /* If the user is in the group, the group * limits apply unless later a line for * the specific user is found. diff --git a/lib/nss.c b/lib/nss.c index d76cef870..a2eb9e006 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -16,6 +16,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strtok/stpsep.h" @@ -79,7 +80,7 @@ nss_init(const char *nsswitch_path) { } p = NULL; while (getline(&line, &len, nssfp) != -1) { - if (line[0] == '#') + if (strprefix(line, "#")) continue; if (strlen(line) < 8) continue; diff --git a/lib/port.c b/lib/port.c index a30e68189..7e1a7b70e 100644 --- a/lib/port.c +++ b/lib/port.c @@ -20,6 +20,7 @@ #include "port.h" #include "prototypes.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" @@ -140,7 +141,7 @@ next: errno = saveerr; return NULL; } - if ('#' == buf[0]) + if (strprefix(buf, "#")) goto next; stpsep(buf, "\n"); diff --git a/lib/setupenv.c b/lib/setupenv.c index b7dcb3fa1..1bc3da314 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -28,6 +28,7 @@ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" #include "string/strspn/stpspn.h" #include "string/strtok/stpsep.h" @@ -61,7 +62,7 @@ static void read_env_file (const char *filename) cp = buf; /* ignore whitespace and comments */ cp = stpspn(cp, " \t"); - if (streq(cp, "") || ('#' == *cp)) { + if (streq(cp, "") || strprefix(cp, "#")) { continue; } /* diff --git a/lib/ttytype.c b/lib/ttytype.c index 740510421..3514f2941 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -18,6 +18,7 @@ #include "getdef.h" #include "prototypes.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" @@ -47,7 +48,7 @@ void ttytype (const char *line) return; } while (fgets (buf, sizeof buf, fp) == buf) { - if (buf[0] == '#') { + if (strprefix(buf, "#")) { continue; } diff --git a/lib/yesno.c b/lib/yesno.c index 029cd815e..27f156bca 100644 --- a/lib/yesno.c +++ b/lib/yesno.c @@ -14,7 +14,9 @@ #include #include #include + #include "prototypes.h" +#include "string/strcmp/strprefix.h" /* @@ -76,10 +78,9 @@ yes_or_no(bool read_only) static int rpmatch(const char *response) { - if (response[0] == 'y' || response[0] == 'Y') + if (strprefix(response, "y") || strprefix(response, "Y")) return 1; - - if (response[0] == 'n' || response[0] == 'n') + if (strprefix(response, "n") || strprefix(response, "N")) return 0; return -1; diff --git a/src/check_subid_range.c b/src/check_subid_range.c index 482267581..a33f92238 100644 --- a/src/check_subid_range.c +++ b/src/check_subid_range.c @@ -15,10 +15,11 @@ #include "atoi/getnum.h" #include "atoi/str2i/str2u.h" #include "defines.h" -#include "prototypes.h" -#include "subordinateio.h" #include "idmapping.h" +#include "prototypes.h" #include "shadowlog.h" +#include "string/strcmp/strprefix.h" +#include "subordinateio.h" static const char Prog[] = "check_subid_range"; @@ -39,7 +40,7 @@ main(int argc, char **argv) exit(1); owner = argv[1]; - check_uids = argv[2][0] == 'u'; + check_uids = strprefix(argv[2], "u"); if (get_uid(argv[3], &start) == -1) exit(1); if (str2ul(&count, argv[4]) == -1) diff --git a/src/grpck.c b/src/grpck.c index d3f2baee6..5fcf6fadf 100644 --- a/src/grpck.c +++ b/src/grpck.c @@ -25,6 +25,7 @@ #include "shadowlog.h" #include "sssd.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #ifdef SHADOWGRP #include "sgroupio.h" @@ -470,7 +471,7 @@ static void check_grp_file (bool *errors, bool *changed) * Skip all NIS entries. */ - if ((gre->line[0] == '+') || (gre->line[0] == '-')) { + if (strprefix(gre->line, "+") || strprefix(gre->line, "-")) { continue; } diff --git a/src/login.c b/src/login.c index 2866b1523..57e106b82 100644 --- a/src/login.c +++ b/src/login.c @@ -41,6 +41,7 @@ #include "string/memset/memzero.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/xstrdup.h" #include "string/strftime.h" @@ -267,7 +268,7 @@ static void process_flags (int argc, char *const *argv) * clever telnet, and getty holes. */ for (arg = 1; arg < argc; arg++) { - if (argv[arg][0] == '-' && strlen (argv[arg]) > 2) { + if (strprefix(argv[arg], "-") && strlen(argv[arg]) > 2) { usage (); } if (streq(argv[arg], "--")) { @@ -348,7 +349,7 @@ static void init_env (void) else { cp = getdef_str ("ENV_TZ"); if (NULL != cp) { - addenv (('/' == *cp) ? tz (cp) : cp, NULL); + addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL); } } #endif /* !USE_PAM */ @@ -856,8 +857,8 @@ int main (int argc, char **argv) * login, even if they have been * "pre-authenticated." */ - if ( ('!' == user_passwd[0]) - || ('*' == user_passwd[0])) { + if ( strprefix(user_passwd, "!") + || strprefix(user_passwd, "*")) { failed = true; } @@ -1015,7 +1016,7 @@ int main (int argc, char **argv) addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */ } - if (pwd->pw_shell[0] == '*') { /* subsystem root */ + if (strprefix(pwd->pw_shell, "*")) { /* subsystem root */ pwd->pw_shell++; /* skip the '*' */ subsystem (pwd); /* figure out what to execute */ subroot = true; /* say I was here again */ diff --git a/src/login_nopam.c b/src/login_nopam.c index 09993b627..b7a7bd54f 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -110,7 +110,7 @@ login_access(const char *user, const char *from) TABLE, lineno)); continue; } - if (line[0] == '#') { + if (strprefix(line, "#")) { continue; /* comment line */ } stpcpy(stprspn(line, " \t"), ""); @@ -141,7 +141,7 @@ login_access(const char *user, const char *from) int err = errno; SYSLOG ((LOG_ERR, "cannot open %s: %s", TABLE, strerror (err))); } - return (!match || (line[0] == '+'))?1:0; + return (!match || strprefix(line, "+"))?1:0; } /* list_match - match an item against a list of tokens with exceptions */ @@ -225,7 +225,7 @@ static bool user_match (char *tok, const char *string) if (host != NULL) { return user_match(tok, string) && from_match(host, myhostname()); #if HAVE_INNETGR - } else if (tok[0] == '@') { /* netgroup */ + } else if (strprefix(tok, "@")) { /* netgroup */ return (netgroup_match (tok + 1, NULL, string)); #endif } else if (string_match (tok, string)) { /* ALL or exact match */ @@ -298,13 +298,13 @@ static bool from_match (char *tok, const char *string) * if it matches the head of the string. */ #if HAVE_INNETGR - if (tok[0] == '@') { /* netgroup */ + if (strprefix(tok, "@")) { /* netgroup */ return (netgroup_match (tok + 1, string, NULL)); } else #endif if (string_match (tok, string)) { /* ALL or exact match */ return true; - } else if (tok[0] == '.') { /* domain: match last fields */ + } else if (strprefix(tok, ".")) { /* domain: match last fields */ size_t str_len, tok_len; str_len = strlen (string); diff --git a/src/newgrp.c b/src/newgrp.c index 8b3844cc3..94340bd1c 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -510,7 +510,7 @@ int main (int argc, char **argv) * Do the command line for "newgrp". It's just making sure * there aren't any flags and getting the new group name. */ - if ((argc > 0) && (argv[0][0] == '-')) { + if ((argc > 0) && strprefix(argv[0], "-")) { usage (); goto failure; } else if (argv[0] != NULL) { diff --git a/src/passwd.c b/src/passwd.c index c545550fd..df3394a04 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -35,6 +35,7 @@ #include "string/memset/memzero.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/xstrdup.h" #include "time/day_to_str.h" @@ -402,7 +403,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp) * changed. Passwords which have been inactive too long cannot be * changed. */ - if ( (sp->sp_pwdp[0] == '!') + if ( strprefix(sp->sp_pwdp, "!") || (exp_status > 1) || ( (sp->sp_max >= 0) && (sp->sp_min > sp->sp_max))) { @@ -439,7 +440,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp) static /*@observer@*/const char *pw_status (const char *pass) { - if (*pass == '*' || *pass == '!') { + if (strprefix(pass, "*") || strprefix(pass, "!")) { return "L"; } if (streq(pass, "")) { @@ -520,7 +521,7 @@ static char *update_crypt_pw (char *cp) if (dflg) strcpy(cp, ""); - if (uflg && *cp == '!') { + if (uflg && strprefix(cp, "!")) { if (cp[1] == '\0') { (void) fprintf (stderr, _("%s: unlocking the password would result in a passwordless account.\n" diff --git a/src/pwck.c b/src/pwck.c index b485a5a87..6272839fd 100644 --- a/src/pwck.c +++ b/src/pwck.c @@ -29,6 +29,7 @@ #include "shadowlog.h" #include "sssd.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #ifdef WITH_TCB #include "tcbfuncs.h" #endif /* WITH_TCB */ @@ -383,7 +384,7 @@ static void check_pw_file (bool *errors, bool *changed) * If this is a NIS line, skip it. You can't "know" what NIS * is going to do without directly asking NIS ... */ - if (('+' == pfe->line[0]) || ('-' == pfe->line[0])) { + if (strprefix(pfe->line, "+") || strprefix(pfe->line, "-")) { continue; } @@ -708,7 +709,7 @@ static void check_spw_file (bool *errors, bool *changed) * If this is a NIS line, skip it. You can't "know" what NIS * is going to do without directly asking NIS ... */ - if (('+' == spe->line[0]) || ('-' == spe->line[0])) { + if (strprefix(spe->line, "+") || strprefix(spe->line, "-")) { continue; } diff --git a/src/su.c b/src/su.c index 970ff1b3c..0902c49a6 100644 --- a/src/su.c +++ b/src/su.c @@ -62,6 +62,7 @@ #include "string/sprintf/snprintf.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/xstrdup.h" @@ -714,7 +715,7 @@ static /*@only@*/struct passwd * do_check_perms (void) * the shell specified in /etc/passwd (not the one specified with * --shell, which will be the one executed in the chroot later). */ - if ('*' == pw->pw_shell[0]) { /* subsystem root required */ + if (strprefix(pw->pw_shell, "*")) { /* subsystem root required */ subsystem (pw); /* change to the subsystem root */ endpwent (); /* close the old password databases */ endspent (); @@ -915,7 +916,7 @@ static void set_environment (struct passwd *pw) #ifndef USE_PAM cp = getdef_str ("ENV_TZ"); if (NULL != cp) { - addenv (('/' == *cp) ? tz (cp) : cp, NULL); + addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL); } /* diff --git a/src/suauth.c b/src/suauth.c index cd2f87b65..2a5affb24 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -20,6 +20,7 @@ #include "defines.h" #include "prototypes.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -87,7 +88,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root) stpcpy(stprspn(temp, " \t"), ""); p = stpspn(temp, " \t"); - if (*p == '#' || streq(p, "")) + if (strprefix(p, "#") || streq(p, "")) continue; to_users = strsep(&p, ":"); diff --git a/src/sulogin.c b/src/sulogin.c index 655a583ab..e2e491aec 100644 --- a/src/sulogin.c +++ b/src/sulogin.c @@ -28,6 +28,7 @@ #include "exitcodes.h" #include "shadowlog.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" @@ -117,7 +118,7 @@ main(int argc, char *argv[]) #ifndef USE_PAM env = getdef_str ("ENV_TZ"); if (NULL != env) { - addenv (('/' == *env) ? tz (env) : env, NULL); + addenv(strprefix(env, "/") ? tz(env) : env, NULL); } env = getdef_str ("ENV_HZ"); if (NULL != env) { diff --git a/src/useradd.c b/src/useradd.c index 84647ee11..258a6cbbb 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -69,6 +69,7 @@ #include "string/sprintf/xasprintf.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" #include "string/strtok/stpsep.h" @@ -2218,7 +2219,7 @@ static void create_home (void) */ for (cp = strtok(bhome, "/"); cp != NULL; cp = strtok(NULL, "/")) { /* Avoid turning a relative path into an absolute path. */ - if (bhome[0] == '/' || !streq(path, "")) + if (strprefix(bhome, "/") || !streq(path, "")) strcat(path, "/"); strcat(path, cp); diff --git a/src/usermod.c b/src/usermod.c index fc29729a9..b4cf2e705 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -65,6 +65,7 @@ #include "string/memset/memzero.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" #include "time/day_to_str.h" @@ -436,7 +437,7 @@ static char *new_pw_passwd (char *pw_pass) SYSLOG ((LOG_INFO, "lock user '%s' password", user_newname)); xasprintf(&buf, "!%s", pw_pass); pw_pass = buf; - } else if (Uflg && pw_pass[0] == '!') { + } else if (Uflg && strprefix(pw_pass, "!")) { if (pw_pass[1] == '\0') { fprintf (stderr, _("%s: unlocking the user's password would result in a passwordless account.\n"