]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
src/chfn.c: Use strsep(3) and strcpy(3) instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Fri, 18 Jul 2025 22:11:11 +0000 (00:11 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 9 Aug 2025 23:00:36 +0000 (18:00 -0500)
This wrapper was very weird, and it's simpler to open-code the calls to
strsep(3) and strcpy(3) instead.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/chfn.c

index 9d7a2343fd67de1117a89a2cc379e6a5abc4c5f3..6834366c0be2e2910e1e3ba77b2893828122dccc 100644 (file)
@@ -37,7 +37,6 @@
 #include "string/strcmp/streq.h"
 #include "string/strcpy/strtcpy.h"
 #include "string/strdup/xstrdup.h"
-#include "string/strtok/stpsep.h"
 
 
 /*
@@ -67,7 +66,6 @@ NORETURN static void fail_exit (int code);
 NORETURN static void usage (int status);
 static bool may_change_field (int);
 static void new_fields (void);
-static char *copy_field (char *, char *, char *);
 static void process_flags (int argc, char **argv);
 static void check_perms (const struct passwd *pw);
 static void update_gecos (const char *user, char *gecos);
@@ -206,27 +204,6 @@ static void new_fields (void)
        }
 }
 
-/*
- * copy_field - get the next field from the gecos field
- *
- * copy_field copies the next field from the gecos field, returning a
- * pointer to the field which follows, or NULL if there are no more fields.
- *
- *     in - the current GECOS field
- *     out - where to copy the field to
- */
-static char *copy_field(char *in, char *out)
-{
-       char  *next;
-
-       next = stpsep(in, ",");
-
-       if (out != NULL)
-               strcpy(out, in);
-
-       return next;
-}
-
 /*
  * process_flags - parse the command line options
  *
@@ -494,45 +471,32 @@ static void update_gecos (const char *user, char *gecos)
  */
 static void get_old_fields (const char *gecos)
 {
-       char *cp;               /* temporary character pointer       */
-       char old_gecos[BUFSIZ]; /* buffer for old GECOS fields       */
+       char        *p;
+       char        old_gecos[BUFSIZ];
+       const char  *f;
 
        STRTCPY(old_gecos, gecos);
+       p = old_gecos;
 
-       /*
-        * Now get the full name. It is the first comma separated field in
-        * the GECOS field.
-        */
-       cp = copy_field(old_gecos, fflg ? NULL : fullnm);
+       f = strsep(&p, ",");
+       if (!fflg)
+               strcpy(fullnm, f);
 
-       /*
-        * Now get the room number. It is the next comma separated field,
-        * if there is indeed one.
-        */
-       if (NULL != cp) {
-               cp = copy_field(cp, rflg ? NULL : roomno);
-       }
+       f = strsep(&p, ",");
+       if (!rflg && f != NULL)
+               strcpy(roomno, f);
 
-       /*
-        * Now get the work phone number. It is the third field.
-        */
-       if (NULL != cp) {
-               cp = copy_field(cp, wflg ? NULL : workph);
-       }
+       f = strsep(&p, ",");
+       if (!wflg && f != NULL)
+               strcpy(workph, f);
 
-       /*
-        * Now get the home phone number. It is the fourth field.
-        */
-       if (NULL != cp) {
-               cp = copy_field(cp, hflg ? NULL : homeph);
-       }
+       f = strsep(&p, ",");
+       if (!hflg && f != NULL)
+               strcpy(homeph, f);
 
-       /*
-        * Anything left over is "slop".
-        */
-       if ((NULL != cp) && !oflg) {
-               strcpy(slop, cp);
-       }
+       /* Anything left over is "slop".  */
+       if (!oflg && p != NULL)
+               strcpy(slop, p);
 }
 
 /*