Skip to content

Commit 173b93f

Browse files
lib/, src/: Use strcspn(3) instead of its pattern
Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent 794f7f4 commit 173b93f

File tree

8 files changed

+20
-28
lines changed

8 files changed

+20
-28
lines changed

lib/getdef.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "string/sprintf/xaprintf.h"
3434
#include "string/strcmp/strcaseeq.h"
3535
#include "string/strcmp/streq.h"
36-
#include "string/strcmp/strprefix.h"
3736
#include "string/strspn/stpspn.h"
3837
#include "string/strspn/stprspn.h"
3938
#include "string/strtok/stpsep.h"
@@ -566,7 +565,7 @@ static void def_load (void)
566565
* Break the line into two fields.
567566
*/
568567
name = stpspn(buf, " \t"); /* first nonwhite */
569-
if (streq(name, "") || strprefix(name, "#"))
568+
if (!strcspn(name, "#"))
570569
continue; /* comment or empty */
571570

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

lib/limits.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ static int setup_user_limits (const char *uname)
369369
* FIXME: A better (smarter) checking should be done
370370
*/
371371
while (fgets (buf, 1024, fil) != NULL) {
372-
if (strprefix(buf, "#") || strprefix(buf, "\n")) {
372+
if (!strcspn(buf, "#\n"))
373373
continue;
374-
}
374+
375375
MEMZERO(tempbuf);
376376
/* a valid line should have a username, then spaces,
377377
* then limits

lib/nss.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "string/sprintf/snprintf.h"
1818
#include "string/strcmp/strcaseprefix.h"
1919
#include "string/strcmp/streq.h"
20-
#include "string/strcmp/strprefix.h"
2120
#include "string/strspn/stpspn.h"
2221
#include "string/strtok/stpsep.h"
2322

@@ -81,7 +80,7 @@ nss_init(const char *nsswitch_path) {
8180
}
8281
p = NULL;
8382
while (getline(&line, &len, nssfp) != -1) {
84-
if (strprefix(line, "#"))
83+
if (!strcspn(line, "#"))
8584
continue;
8685
if (strlen(line) < 8)
8786
continue;

lib/setupenv.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
#ident "$Id$"
1717

18-
#include <assert.h>
18+
#include <ctype.h>
19+
#include <stdio.h>
20+
#include <string.h>
1921
#include <sys/types.h>
2022
#include <sys/stat.h>
21-
#include <stdio.h>
22-
#include <ctype.h>
2323

2424
#include "prototypes.h"
2525
#include "defines.h"
@@ -28,11 +28,12 @@
2828
#include "shadowlog.h"
2929
#include "string/sprintf/xaprintf.h"
3030
#include "string/strcmp/streq.h"
31-
#include "string/strcmp/strprefix.h"
3231
#include "string/strdup/xstrdup.h"
3332
#include "string/strspn/stpspn.h"
3433
#include "string/strtok/stpsep.h"
3534

35+
#include <assert.h>
36+
3637

3738
#ifndef USE_PAM
3839
static void
@@ -62,9 +63,9 @@ static void read_env_file (const char *filename)
6263
cp = buf;
6364
/* ignore whitespace and comments */
6465
cp = stpspn(cp, " \t");
65-
if (streq(cp, "") || strprefix(cp, "#")) {
66+
if (!strcspn(cp, "#"))
6667
continue;
67-
}
68+
6869
/*
6970
* ignore lines which don't follow the name=value format
7071
* (for example, the "export NAME" shell commands)

src/login_nopam.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ login_access(const char *user, const char *from)
111111
TABLE, lineno));
112112
continue;
113113
}
114-
if (strprefix(line, "#")) {
115-
continue; /* comment line */
116-
}
114+
if (!strcspn(line, "#"))
115+
continue; /* comment or empty */
116+
117117
stpcpy(stprspn(line, " \t"), "");
118118
if (streq(line, "")) { /* skip blank lines */
119119
continue;

src/suauth.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "defines.h"
2121
#include "prototypes.h"
2222
#include "string/strcmp/streq.h"
23-
#include "string/strcmp/strprefix.h"
2423
#include "string/strspn/stpspn.h"
2524
#include "string/strspn/stprspn.h"
2625
#include "string/strtok/stpsep.h"
@@ -88,7 +87,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root)
8887
stpcpy(stprspn(temp, " \t"), "");
8988

9089
p = stpspn(temp, " \t");
91-
if (strprefix(p, "#") || streq(p, ""))
90+
if (!strcspn(p, "#"))
9291
continue;
9392

9493
to_users = strsep(&p, ":");

src/useradd.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,16 +1371,13 @@ static void process_flags (int argc, char **argv, struct option_flags *flags)
13711371
break;
13721372
case 's':
13731373
if ( ( !VALID (optarg) )
1374-
|| ( !streq(optarg, "")
1375-
&& ('/' != optarg[0])
1376-
&& ('*' != optarg[0]) )) {
1374+
|| strcspn(optarg, "/*")) {
13771375
fprintf (stderr,
13781376
_("%s: invalid shell '%s'\n"),
13791377
Prog, optarg);
13801378
exit (E_BAD_ARG);
13811379
}
1382-
if (!streq(optarg, "")
1383-
&& '*' != optarg[0]
1380+
if (strcspn(optarg, "*")
13841381
&& !streq(optarg, "/sbin/nologin")
13851382
&& !streq(optarg, "/usr/sbin/nologin")
13861383
&& ( stat(optarg, &st) != 0

src/usermod.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ process_flags(int argc, char **argv, struct option_flags *flags)
10831083
}
10841084
dflg = true;
10851085
user_newhome = optarg;
1086-
if ((user_newhome[0] != '/') && !streq(user_newhome, "")) {
1086+
if (strcspn(user_newhome, "/")) {
10871087
fprintf (stderr,
10881088
_("%s: homedir must be an absolute path\n"),
10891089
Prog);
@@ -1176,16 +1176,13 @@ process_flags(int argc, char **argv, struct option_flags *flags)
11761176
break;
11771177
case 's':
11781178
if ( ( !VALID (optarg) )
1179-
|| ( !streq(optarg, "")
1180-
&& ('/' != optarg[0])
1181-
&& ('*' != optarg[0]) )) {
1179+
|| strcspn(optarg, "/*")) {
11821180
fprintf (stderr,
11831181
_("%s: invalid shell '%s'\n"),
11841182
Prog, optarg);
11851183
exit (E_BAD_ARG);
11861184
}
1187-
if (!streq(optarg, "")
1188-
&& '*' != optarg[0]
1185+
if (strcspn(optarg, "*")
11891186
&& !streq(optarg, "/sbin/nologin")
11901187
&& !streq(optarg, "/usr/sbin/nologin")
11911188
&& ( stat(optarg, &st) != 0

0 commit comments

Comments
 (0)