]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use STRSEP2ARR() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Fri, 6 Dec 2024 20:21:24 +0000 (21:21 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Sat, 7 Jun 2025 14:52:03 +0000 (16:52 +0200)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/gshadow.c
lib/sgetgrent.c
lib/sgetpwent.c
lib/subordinateio.c
src/newusers.c

index 9e9e251002323221c314b45fdc528c764afcffcf..5e7e3b14103a7a7c64f5245a0589dc713b852efe 100644 (file)
 #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];
index 8d675a3fed19cd88d4dd445889c8a52b3f4e760a..8737cebb6e341ab1f780fd553e40f416c1fe1467 100644 (file)
 #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) {
index 7ece4dccbae34b93a4551d35c369b0e7ff645bec..370fd2150a0119ad183f6bee64d215a3779b2b52 100644 (file)
 #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], ""))
index 90372476755aaf84b09803b38bff106a7e986751..27f888b933aa71aa2e07b9acec1004b15ecb21ce 100644 (file)
@@ -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], ""))
index deb0891259e733f742e5817e411df99a8d586b60..38cbdddb88ebdb203ad96fe73228c9ae9bea09c4 100644 (file)
@@ -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);