From 81821d3ec82fcf66322ea10ee42e97b1e348fdb4 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 6 Dec 2024 21:21:24 +0100 Subject: [PATCH] lib/: Use STRSEP2ARR() instead of its pattern Signed-off-by: Alejandro Colomar --- lib/gshadow.c | 22 +++------------------- lib/sgetgrent.c | 15 ++++++--------- lib/sgetpwent.c | 27 +++++---------------------- lib/subordinateio.c | 18 +++--------------- src/newusers.c | 15 ++------------- 5 files changed, 19 insertions(+), 78 deletions(-) diff --git a/lib/gshadow.c b/lib/gshadow.c index 9e9e25100..5e7e3b141 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -26,13 +26,12 @@ #include "string/strchr/strchrcnt.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" static /*@null@*/FILE *shadow; static struct sgrp sgroup = {}; -#define FIELDS 4 - static /*@null@*/char ** build_list(char *s) @@ -73,9 +72,7 @@ sgetsgent(const char *s) { static char *dup = NULL; - char *fields[FIELDS]; - char *cp; - int i; + char *fields[4]; free(dup); dup = strdup(s); @@ -84,20 +81,7 @@ sgetsgent(const char *s) stpsep(dup, "\n"); - /* - * There should be exactly 4 colon separated fields. Find - * all 4 of them and save the starting addresses in fields[]. - */ - - for (cp = dup, i = 0; (i < FIELDS) && (NULL != cp); i++) - fields[i] = strsep(&cp, ":"); - - /* - * If there was an extra field somehow, or perhaps not enough, - * the line is invalid. - */ - - if (NULL != cp || i != FIELDS) + if (STRSEP2ARR(dup, ":", fields) == -1) return NULL; sgroup.sg_namp = fields[0]; diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index 8d675a3fe..8737cebb6 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -24,10 +24,9 @@ #include "prototypes.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" -#define NFIELDS 4 - /* * list - turn a comma-separated string into an array of (char *)'s * @@ -71,9 +70,7 @@ sgetgrent(const char *s) static char *dup = NULL; static struct group grent; - int i; - char *cp; - char *fields[NFIELDS]; + char *fields[4]; free(dup); dup = strdup(s); @@ -82,12 +79,12 @@ sgetgrent(const char *s) stpsep(dup, "\n"); - for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++) - fields[i] = strsep(&cp, ":"); + if (STRSEP2ARR(dup, ":", fields) == -1) + return NULL; - if (i < NFIELDS || streq(fields[2], "") || cp != NULL) { + if (streq(fields[2], "")) return NULL; - } + grent.gr_name = fields[0]; grent.gr_passwd = fields[1]; if (get_gid(fields[2], &grent.gr_gid) == -1) { diff --git a/lib/sgetpwent.c b/lib/sgetpwent.c index 7ece4dccb..370fd2150 100644 --- a/lib/sgetpwent.c +++ b/lib/sgetpwent.c @@ -23,10 +23,9 @@ #include "shadowlog_internal.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" -#define NFIELDS 7 - /* * sgetpwent - convert a string to a (struct passwd) * @@ -45,9 +44,7 @@ sgetpwent(const char *s) static char *dup = NULL; static struct passwd pwent; - int i; - char *cp; - char *fields[NFIELDS]; + char *fields[7]; free(dup); dup = strdup(s); @@ -56,26 +53,12 @@ sgetpwent(const char *s) stpsep(dup, "\n"); - /* - * Save a pointer to the start of each colon separated - * field. The fields are converted into NUL terminated strings. - */ - - for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++) - fields[i] = strsep(&cp, ":"); - - /* something at the end, columns over shot */ - if ( cp != NULL ) { - return( NULL ); - } + if (STRSEP2ARR(dup, ":", fields) == -1) + return NULL; /* - * There must be exactly NFIELDS colon separated fields or - * the entry is invalid. Also, the UID and GID must be non-blank. + * The UID and GID must be non-blank. */ - - if (i != NFIELDS) - return NULL; if (streq(fields[2], "")) return NULL; if (streq(fields[3], "")) diff --git a/lib/subordinateio.c b/lib/subordinateio.c index 903724767..27f888b93 100644 --- a/lib/subordinateio.c +++ b/lib/subordinateio.c @@ -25,6 +25,7 @@ #include "string/ctype/strisascii/strisdigit.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" +#include "string/strtok/strsep2arr.h" #define ID_SIZE 31 @@ -85,8 +86,6 @@ subordinate_parse(const char *line) { static struct subordinate_range range; static char rangebuf[1024]; - int i; - char *cp; char *fields[SUBID_NFIELDS]; /* @@ -97,20 +96,9 @@ subordinate_parse(const char *line) return NULL; /* fail if too long */ strcpy (rangebuf, line); - /* - * Save a pointer to the start of each colon separated - * field. The fields are converted into NUL terminated strings. - */ - - for (cp = rangebuf, i = 0; (i < SUBID_NFIELDS) && (NULL != cp); i++) - fields[i] = strsep(&cp, ":"); - - /* - * There must be exactly SUBID_NFIELDS colon separated fields or - * the entry is invalid. Also, fields must be non-blank. - */ - if (i != SUBID_NFIELDS) + if (STRSEP2ARR(rangebuf, ":", fields) == -1) return NULL; + if (streq(fields[0], "")) return NULL; if (streq(fields[1], "")) diff --git a/src/newusers.c b/src/newusers.c index deb089125..38cbdddb8 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -58,6 +58,7 @@ #include "string/strcmp/streq.h" #include "string/strdup/xstrdup.h" #include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" /* @@ -1060,8 +1061,6 @@ int main (int argc, char **argv) { char buf[BUFSIZ]; char *fields[7]; - int nfields; - char *cp; const struct passwd *pw; struct passwd newpw; intmax_t line = 0; @@ -1119,17 +1118,7 @@ int main (int argc, char **argv) fail_exit (EXIT_FAILURE); } - /* - * Break the string into fields and screw around with them. - * There MUST be 7 colon separated fields, although the - * values aren't that particular. - */ - for (cp = buf, nfields = 0; nfields < 7; nfields++) { - fields[nfields] = strsep(&cp, ":"); - if (cp == NULL) - break; - } - if (nfields != 6) { + if (STRSEP2ARR(buf, ":", fields) == -1) { fprintf (stderr, _("%s: line %jd: invalid line\n"), Prog, line); fail_exit (EXIT_FAILURE); -- 2.47.2