Skip to content

Commit 309cc8f

Browse files
lib/, src/: Use strprefix() instead of its pattern
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 0b8ffba commit 309cc8f

22 files changed

+60
-40
lines changed

contrib/adduser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#include <syslog.h>
120120

121121
#include "string/strcmp/streq.h"
122+
#include "string/strcmp/strprefix.h"
122123

123124

124125
#define IMMEDIATE_CHANGE /* Expire newly created password, must be changed
@@ -389,7 +390,7 @@ main (void)
389390
fflush (stdout);
390391
safeget (foo, sizeof (foo));
391392

392-
done = bad = correct = (foo[0] == 'y' || foo[0] == 'Y');
393+
done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y"));
393394

394395
if (bad != 1)
395396
printf ("\nUser [%s] not added\n", usrname);

lib/commonio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "string/memset/memzero.h"
3838
#include "string/sprintf/snprintf.h"
3939
#include "string/strcmp/streq.h"
40+
#include "string/strcmp/strprefix.h"
4041
#include "string/strtok/stpsep.h"
4142

4243

@@ -524,7 +525,7 @@ static void add_one_entry (struct commonio_db *db,
524525

525526
static bool name_is_nis (const char *name)
526527
{
527-
return (('+' == name[0]) || ('-' == name[0]));
528+
return strprefix(name, "+") || strprefix(name, "-");
528529
}
529530

530531

lib/encrypt.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "prototypes.h"
1818
#include "defines.h"
1919
#include "shadowlog_internal.h"
20+
#include "string/strcmp/strprefix.h"
21+
2022

2123
/*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt)
2224
{
@@ -35,7 +37,7 @@
3537

3638
/* Some crypt() do not return NULL if the algorithm is not
3739
* supported, and return a DES encrypted password. */
38-
if ((NULL != salt) && (salt[0] == '$') && (strlen (cp) <= 13))
40+
if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13))
3941
{
4042
/*@observer@*/const char *method;
4143
switch (salt[1])

lib/getdef.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "string/strchr/stpspn.h"
3434
#include "string/strchr/strrspn.h"
3535
#include "string/strcmp/streq.h"
36+
#include "string/strcmp/strprefix.h"
3637
#include "string/strtok/stpsep.h"
3738

3839

@@ -567,7 +568,7 @@ static void def_load (void)
567568
* Break the line into two fields.
568569
*/
569570
name = stpspn(buf, " \t"); /* first nonwhite */
570-
if (streq(name, "") || *name == '#')
571+
if (streq(name, "") || strprefix(name, "#"))
571572
continue; /* comment or empty */
572573

573574
s = stpsep(name, " \t"); /* next field */

lib/limits.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int setrlimit_value (unsigned int resource,
6363
/* The "-" is special, not belonging to a strange negative limit.
6464
* It is infinity, in a controlled way.
6565
*/
66-
if ('-' == value[0]) {
66+
if (strprefix(value, "-")) {
6767
limit = RLIM_INFINITY;
6868

6969
} else {
@@ -371,7 +371,7 @@ static int setup_user_limits (const char *uname)
371371
* FIXME: A better (smarter) checking should be done
372372
*/
373373
while (fgets (buf, 1024, fil) != NULL) {
374-
if (('#' == buf[0]) || ('\n' == buf[0])) {
374+
if (strprefix(buf, "#") || strprefix(buf, "\n")) {
375375
continue;
376376
}
377377
MEMZERO(tempbuf);
@@ -402,7 +402,7 @@ static int setup_user_limits (const char *uname)
402402
break;
403403
} else if (streq(name, "*")) {
404404
strcpy (deflimits, tempbuf);
405-
} else if (name[0] == '@') {
405+
} else if (strprefix(name, "@")) {
406406
/* If the user is in the group, the group
407407
* limits apply unless later a line for
408408
* the specific user is found.

lib/nss.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "string/sprintf/snprintf.h"
1818
#include "string/strchr/stpspn.h"
1919
#include "string/strcmp/streq.h"
20+
#include "string/strcmp/strprefix.h"
2021
#include "string/strtok/stpsep.h"
2122

2223

@@ -79,7 +80,7 @@ nss_init(const char *nsswitch_path) {
7980
}
8081
p = NULL;
8182
while (getline(&line, &len, nssfp) != -1) {
82-
if (line[0] == '#')
83+
if (strprefix(line, "#"))
8384
continue;
8485
if (strlen(line) < 8)
8586
continue;

lib/port.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "port.h"
2121
#include "prototypes.h"
2222
#include "string/strcmp/streq.h"
23+
#include "string/strcmp/strprefix.h"
2324
#include "string/strtok/stpsep.h"
2425

2526

@@ -140,7 +141,7 @@ getportent(void)
140141
errno = saveerr;
141142
return NULL;
142143
}
143-
if ('#' == buf[0])
144+
if (strprefix(buf, "#"))
144145
goto next;
145146

146147
stpsep(buf, "\n");

lib/setupenv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "string/sprintf/xasprintf.h"
3030
#include "string/strchr/stpspn.h"
3131
#include "string/strcmp/streq.h"
32+
#include "string/strcmp/strprefix.h"
3233
#include "string/strdup/xstrdup.h"
3334
#include "string/strtok/stpsep.h"
3435

@@ -61,7 +62,7 @@ static void read_env_file (const char *filename)
6162
cp = buf;
6263
/* ignore whitespace and comments */
6364
cp = stpspn(cp, " \t");
64-
if (streq(cp, "") || ('#' == *cp)) {
65+
if (streq(cp, "") || strprefix(cp, "#")) {
6566
continue;
6667
}
6768
/*

lib/ttytype.c

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

2324

@@ -47,7 +48,7 @@ void ttytype (const char *line)
4748
return;
4849
}
4950
while (fgets (buf, sizeof buf, fp) == buf) {
50-
if (buf[0] == '#') {
51+
if (strprefix(buf, "#")) {
5152
continue;
5253
}
5354

lib/yesno.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include <stdbool.h>
1515
#include <stdio.h>
1616
#include <stdlib.h>
17+
1718
#include "prototypes.h"
19+
#include "string/strcmp/strprefix.h"
1820

1921

2022
/*
@@ -76,10 +78,9 @@ yes_or_no(bool read_only)
7678
static int
7779
rpmatch(const char *response)
7880
{
79-
if (response[0] == 'y' || response[0] == 'Y')
81+
if (strprefix(response, "y") || strprefix(response, "Y"))
8082
return 1;
81-
82-
if (response[0] == 'n' || response[0] == 'n')
83+
if (strprefix(response, "n") || strprefix(response, "N"))
8384
return 0;
8485

8586
return -1;

src/check_subid_range.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
#include "atoi/getnum.h"
1616
#include "atoi/str2i/str2u.h"
1717
#include "defines.h"
18-
#include "prototypes.h"
19-
#include "subordinateio.h"
2018
#include "idmapping.h"
19+
#include "prototypes.h"
2120
#include "shadowlog.h"
21+
#include "string/strcmp/strprefix.h"
22+
#include "subordinateio.h"
2223

2324

2425
static const char Prog[] = "check_subid_range";
@@ -42,7 +43,7 @@ main(int argc, char **argv)
4243
exit(1);
4344
if (str2ul(&count, argv[4]) == -1)
4445
exit(1);
45-
if (argv[2][0] == 'u') {
46+
if (strprefix(argv[2], "u")) {
4647
if (have_sub_uids(owner, start, count))
4748
exit(0);
4849
exit(1);

src/grpck.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "shadowlog.h"
2626
#include "sssd.h"
2727
#include "string/strcmp/streq.h"
28+
#include "string/strcmp/strprefix.h"
2829

2930
#ifdef SHADOWGRP
3031
#include "sgroupio.h"
@@ -470,7 +471,7 @@ static void check_grp_file (int *errors, bool *changed)
470471
* Skip all NIS entries.
471472
*/
472473

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

src/login.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "string/memset/memzero.h"
4141
#include "string/sprintf/snprintf.h"
4242
#include "string/strcmp/streq.h"
43+
#include "string/strcmp/strprefix.h"
4344
#include "string/strcpy/strtcpy.h"
4445
#include "string/strdup/xstrdup.h"
4546
#include "string/strftime.h"
@@ -267,7 +268,7 @@ static void process_flags (int argc, char *const *argv)
267268
* clever telnet, and getty holes.
268269
*/
269270
for (arg = 1; arg < argc; arg++) {
270-
if (argv[arg][0] == '-' && strlen (argv[arg]) > 2) {
271+
if (strprefix(argv[arg], "-") && strlen(argv[arg]) > 2) {
271272
usage ();
272273
}
273274
if (streq(argv[arg], "--")) {
@@ -349,7 +350,7 @@ static void init_env (void)
349350
else {
350351
cp = getdef_str ("ENV_TZ");
351352
if (NULL != cp) {
352-
addenv (('/' == *cp) ? tz (cp) : cp, NULL);
353+
addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL);
353354
}
354355
}
355356
#endif /* !USE_PAM */
@@ -860,8 +861,8 @@ int main (int argc, char **argv)
860861
* login, even if they have been
861862
* "pre-authenticated."
862863
*/
863-
if ( ('!' == user_passwd[0])
864-
|| ('*' == user_passwd[0])) {
864+
if ( strprefix(user_passwd, "!")
865+
|| strprefix(user_passwd, "*")) {
865866
failed = true;
866867
}
867868

@@ -1019,7 +1020,7 @@ int main (int argc, char **argv)
10191020
addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */
10201021
}
10211022

1022-
if (pwd->pw_shell[0] == '*') { /* subsystem root */
1023+
if (strprefix(pwd->pw_shell, "*")) { /* subsystem root */
10231024
pwd->pw_shell++; /* skip the '*' */
10241025
subsystem (pwd); /* figure out what to execute */
10251026
subroot = true; /* say I was here again */

src/login_nopam.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ login_access(const char *user, const char *from)
108108
TABLE, lineno));
109109
continue;
110110
}
111-
if (line[0] == '#') {
111+
if (strprefix(line, "#")) {
112112
continue; /* comment line */
113113
}
114114
stpcpy(strrspn(line, " \t"), "");
@@ -139,7 +139,7 @@ login_access(const char *user, const char *from)
139139
int err = errno;
140140
SYSLOG ((LOG_ERR, "cannot open %s: %s", TABLE, strerror (err)));
141141
}
142-
return (!match || (line[0] == '+'))?1:0;
142+
return (!match || strprefix(line, "+"))?1:0;
143143
}
144144

145145
/* list_match - match an item against a list of tokens with exceptions */
@@ -228,7 +228,7 @@ static bool user_match (const char *tok, const char *string)
228228
if (host != NULL) {
229229
return user_match(tok, string) && from_match(host, myhostname());
230230
#if HAVE_INNETGR
231-
} else if (tok[0] == '@') { /* netgroup */
231+
} else if (strprefix(tok, "@")) { /* netgroup */
232232
return (netgroup_match (tok + 1, NULL, string));
233233
#endif
234234
} else if (string_match (tok, string)) { /* ALL or exact match */
@@ -301,13 +301,13 @@ static bool from_match (const char *tok, const char *string)
301301
* if it matches the head of the string.
302302
*/
303303
#if HAVE_INNETGR
304-
if (tok[0] == '@') { /* netgroup */
304+
if (strprefix(tok, "@")) { /* netgroup */
305305
return (netgroup_match (tok + 1, string, NULL));
306306
} else
307307
#endif
308308
if (string_match (tok, string)) { /* ALL or exact match */
309309
return true;
310-
} else if (tok[0] == '.') { /* domain: match last fields */
310+
} else if (strprefix(tok, ".")) { /* domain: match last fields */
311311
size_t str_len, tok_len;
312312

313313
str_len = strlen (string);

src/newgrp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ int main (int argc, char **argv)
519519
* Do the command line for "newgrp". It's just making sure
520520
* there aren't any flags and getting the new group name.
521521
*/
522-
if ((argc > 0) && (argv[0][0] == '-')) {
522+
if ((argc > 0) && strprefix(argv[0], "-")) {
523523
usage ();
524524
goto failure;
525525
} else if (argv[0] != NULL) {

src/passwd.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "string/memset/memzero.h"
3636
#include "string/sprintf/xasprintf.h"
3737
#include "string/strcmp/streq.h"
38+
#include "string/strcmp/strprefix.h"
3839
#include "string/strcpy/strtcpy.h"
3940
#include "string/strdup/xstrdup.h"
4041
#include "time/day_to_str.h"
@@ -401,7 +402,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp)
401402
* changed. Passwords which have been inactive too long cannot be
402403
* changed.
403404
*/
404-
if ( (sp->sp_pwdp[0] == '!')
405+
if ( strprefix(sp->sp_pwdp, "!")
405406
|| (exp_status > 1)
406407
|| ( (sp->sp_max >= 0)
407408
&& (sp->sp_min > sp->sp_max))) {
@@ -438,7 +439,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp)
438439

439440
static /*@observer@*/const char *pw_status (const char *pass)
440441
{
441-
if (*pass == '*' || *pass == '!') {
442+
if (strprefix(pass, "*") || strprefix(pass, "!")) {
442443
return "L";
443444
}
444445
if (streq(pass, "")) {
@@ -519,7 +520,7 @@ static char *update_crypt_pw (char *cp)
519520
if (dflg)
520521
strcpy(cp, "");
521522

522-
if (uflg && *cp == '!') {
523+
if (uflg && strprefix(cp, "!")) {
523524
if (cp[1] == '\0') {
524525
(void) fprintf (stderr,
525526
_("%s: unlocking the password would result in a passwordless account.\n"

src/pwck.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "shadowlog.h"
3030
#include "sssd.h"
3131
#include "string/strcmp/streq.h"
32+
#include "string/strcmp/strprefix.h"
3233
#ifdef WITH_TCB
3334
#include "tcbfuncs.h"
3435
#endif /* WITH_TCB */
@@ -383,7 +384,7 @@ static void check_pw_file (int *errors, bool *changed)
383384
* If this is a NIS line, skip it. You can't "know" what NIS
384385
* is going to do without directly asking NIS ...
385386
*/
386-
if (('+' == pfe->line[0]) || ('-' == pfe->line[0])) {
387+
if (strprefix(pfe->line, "+") || strprefix(pfe->line, "-")) {
387388
continue;
388389
}
389390

@@ -708,7 +709,7 @@ static void check_spw_file (int *errors, bool *changed)
708709
* If this is a NIS line, skip it. You can't "know" what NIS
709710
* is going to do without directly asking NIS ...
710711
*/
711-
if (('+' == spe->line[0]) || ('-' == spe->line[0])) {
712+
if (strprefix(spe->line, "+") || strprefix(spe->line, "-")) {
712713
continue;
713714
}
714715

0 commit comments

Comments
 (0)