Skip to content

Commit

Permalink
lib/, src/: Use strprefix() instead of its pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Dec 10, 2024
1 parent b6b5a3b commit 7130fc6
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 40 deletions.
3 changes: 2 additions & 1 deletion contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include <syslog.h>

#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


#define IMMEDIATE_CHANGE /* Expire newly created password, must be changed
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -524,7 +525,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, "-");
}


Expand Down
4 changes: 3 additions & 1 deletion lib/encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "string/strchr/stpspn.h"
#include "string/strchr/strrspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -567,7 +568,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 */
Expand Down
6 changes: 3 additions & 3 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "string/sprintf/snprintf.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -140,7 +141,7 @@ getportent(void)
errno = saveerr;
return NULL;
}
if ('#' == buf[0])
if (strprefix(buf, "#"))
goto next;

stpsep(buf, "\n");
Expand Down
3 changes: 2 additions & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "string/sprintf/xasprintf.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strdup/xstrdup.h"
#include "string/strtok/stpsep.h"

Expand Down Expand Up @@ -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;
}
/*
Expand Down
3 changes: 2 additions & 1 deletion lib/ttytype.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -47,7 +48,7 @@ void ttytype (const char *line)
return;
}
while (fgets (buf, sizeof buf, fp) == buf) {
if (buf[0] == '#') {
if (strprefix(buf, "#")) {
continue;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/yesno.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "prototypes.h"
#include "string/strcmp/strprefix.h"


/*
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/check_subid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -42,7 +43,7 @@ main(int argc, char **argv)
exit(1);
if (str2ul(&count, argv[4]) == -1)
exit(1);
if (argv[2][0] == 'u') {
if (strprefix(argv[2], "u")) {
if (have_sub_uids(owner, start, count))
exit(0);
exit(1);
Expand Down
3 changes: 2 additions & 1 deletion src/grpck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -470,7 +471,7 @@ static void check_grp_file (int *errors, bool *changed)
* Skip all NIS entries.
*/

if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
if (strprefix(gre->line, "+") || strprefix(gre->line, "-")) {
continue;
}

Expand Down
11 changes: 6 additions & 5 deletions src/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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"
Expand Down Expand Up @@ -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], "--")) {
Expand Down Expand Up @@ -349,7 +350,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 */
Expand Down Expand Up @@ -860,8 +861,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;
}

Expand Down Expand Up @@ -1019,7 +1020,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 */
Expand Down
10 changes: 5 additions & 5 deletions src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ login_access(const char *user, const char *from)
TABLE, lineno));
continue;
}
if (line[0] == '#') {
if (strprefix(line, "#")) {
continue; /* comment line */
}
stpcpy(strrspn(line, " \t"), "");
Expand Down Expand Up @@ -139,7 +139,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 */
Expand Down Expand Up @@ -228,7 +228,7 @@ static bool user_match (const 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 */
Expand Down Expand Up @@ -301,13 +301,13 @@ static bool from_match (const 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);
Expand Down
2 changes: 1 addition & 1 deletion src/newgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,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) {
Expand Down
7 changes: 4 additions & 3 deletions src/passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -401,7 +402,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))) {
Expand Down Expand Up @@ -438,7 +439,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, "")) {
Expand Down Expand Up @@ -519,7 +520,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"
Expand Down
5 changes: 3 additions & 2 deletions src/pwck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -383,7 +384,7 @@ static void check_pw_file (int *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;
}

Expand Down Expand Up @@ -708,7 +709,7 @@ static void check_spw_file (int *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;
}

Expand Down
Loading

0 comments on commit 7130fc6

Please sign in to comment.