From 70e2f2b1fba3837483f9e597669c4ebf8104f25c Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 7 Dec 2024 00:51:14 +0100 Subject: [PATCH] lib/: Use strsep2arr() instead of its pattern Signed-off-by: Alejandro Colomar --- lib/sgetspent.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/sgetspent.c b/lib/sgetspent.c index 8a19e8d82..2c620aef6 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -25,8 +25,10 @@ #include "defines.h" #include "prototypes.h" #include "shadowlog_internal.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" #define FIELDS 9 @@ -43,8 +45,7 @@ sgetspent(const char *s) static struct spwd spwd; char *fields[FIELDS]; - char *cp; - int i; + size_t i; free(dup); dup = strdup(s); @@ -53,18 +54,10 @@ sgetspent(const char *s) stpsep(dup, "\n"); - /* - * Tokenize the string into colon separated fields. Allow up to - * FIELDS different fields. - */ - - for (cp = dup, i = 0; cp != NULL && i < FIELDS; i++) - fields[i] = strsep(&cp, ":"); - - if (i == (FIELDS - 1)) + i = strsep2arr(dup, ":", countof(fields), fields); + if (i == countof(fields) - 1) fields[i++] = ""; - - if (cp != NULL || (i != FIELDS && i != OFIELDS)) + if (i != countof(fields) && i != OFIELDS) return NULL; /* -- 2.47.3