From 729949bd6f673679e76a109a3fa824493f11a12c Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 7 Dec 2024 02:02:07 +0100 Subject: [PATCH] lib/: Use xastrsep2ls() instead of its pattern Signed-off-by: Alejandro Colomar --- lib/gshadow.c | 13 +++++-------- lib/sgetgrent.c | 33 +++++++++++---------------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/lib/gshadow.c b/lib/gshadow.c index 7c069666f..b764c1dbb 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -20,13 +20,12 @@ #include "alloc/malloc.h" #include "alloc/realloc.h" -#include "alloc/x/xmalloc.h" #include "defines.h" #include "prototypes.h" -#include "string/strchr/strchrcnt.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" #include "string/strtok/strsep2arr.h" +#include "string/strtok/xastrsep2ls.h" static /*@null@*/FILE *shadow; @@ -37,14 +36,12 @@ static /*@null@*/char ** build_list(char *s) { char **l; - size_t i; + size_t n; - l = XMALLOC(strchrcnt(s, ',') + 2, char *); + l = xastrsep2ls(s, ",", &n); - for (i = 0; s != NULL && !streq(s, ""); i++) - l[i] = strsep(&s, ","); - - l[i] = NULL; + if (streq(l[n-1], "")) + l[n-1] = NULL; return l; } diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index 981f43958..eb8709181 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -17,13 +17,13 @@ #include #include "alloc/malloc.h" -#include "alloc/reallocf.h" #include "atoi/getnum.h" #include "defines.h" #include "prototypes.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" #include "string/strtok/strsep2arr.h" +#include "string/strtok/astrsep2ls.h" /* @@ -39,29 +39,18 @@ static char ** list(char *s) { static char **members = NULL; - static size_t size = 0; /* max members + 1 */ - size_t i; + + size_t n; free(members); - members = NULL; - - i = 0; - for (;;) { - /* check if there is room for another pointer (to a group - member name, or terminating NULL). */ - if (i >= size) { - size = i + 100; /* at least: i + 1 */ - members = REALLOCF(members, size, char *); - if (!members) { - size = 0; - return NULL; - } - } - if (!s || streq(s, "")) - break; - members[i++] = strsep(&s, ","); - } - members[i] = NULL; + + members = astrsep2ls(s, ",", &n); + if (members == NULL) + return NULL; + + if (streq(members[n-1], "")) + members[n-1] = NULL; + return members; }