lib/, src/: Use strsep(3) instead of strtok(3)
strsep(3) is stateless, and so is easier to reason about.
It also has a slight difference: strtok(3) jumps over empty fields,
while strsep(3) respects them as empty fields. In most of the cases
where we were using strtok(3), it makes more sense to respect empty
fields, and this commit probably silently fixes a few bugs.
In other cases (most notably filesystem paths), contiguous delimiters
("//") should be collapsed, so strtok(3) still makes more sense there.
This commit doesn't replace such strtok(3) calls.
While at this, remove some useless variables used by these calls, and
reduce the scope of others.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Serge Hallyn
parent
bdb5e2b79f
commit
90afe61003
+9
-12
@@ -45,11 +45,9 @@ static int isgrp (const char *, const char *);
|
||||
static int lines = 0;
|
||||
|
||||
|
||||
int check_su_auth (const char *actual_id,
|
||||
const char *wanted_id,
|
||||
bool su_to_root)
|
||||
int
|
||||
check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root)
|
||||
{
|
||||
const char field[] = ":";
|
||||
FILE *authfile_fd;
|
||||
char temp[1024];
|
||||
char *to_users;
|
||||
@@ -91,10 +89,10 @@ int check_su_auth (const char *actual_id,
|
||||
if (*p == '#' || *p == '\0')
|
||||
continue;
|
||||
|
||||
if (!(to_users = strtok(p, field))
|
||||
|| !(from_users = strtok (NULL, field))
|
||||
|| !(action = strtok (NULL, field))
|
||||
|| strtok (NULL, field)) {
|
||||
to_users = strsep(&p, ":");
|
||||
from_users = strsep(&p, ":");
|
||||
action = strsep(&p, ":");
|
||||
if (action == NULL || p != NULL) {
|
||||
SYSLOG ((LOG_ERR,
|
||||
"%s, line %d. Bad number of fields.\n",
|
||||
SUAUTHFILE, lines));
|
||||
@@ -138,15 +136,14 @@ int check_su_auth (const char *actual_id,
|
||||
return NOACTION;
|
||||
}
|
||||
|
||||
static int applies (const char *single, char *list)
|
||||
static int
|
||||
applies(const char *single, char *list)
|
||||
{
|
||||
const char split[] = ", ";
|
||||
char *tok;
|
||||
|
||||
int state = 0;
|
||||
|
||||
for (tok = strtok (list, split); tok != NULL;
|
||||
tok = strtok (NULL, split)) {
|
||||
while (NULL != (tok = strsep(&list, ", "))) {
|
||||
|
||||
if (streq(tok, "ALL")) {
|
||||
if (state != 0) {
|
||||
|
||||
Reference in New Issue
Block a user