]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/port.c: getportent(): Use equivalent code to parse equally-formatted fields
authorAlejandro Colomar <alx@kernel.org>
Tue, 2 Jul 2024 13:05:21 +0000 (15:05 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 6 Jul 2024 12:44:02 +0000 (07:44 -0500)
The tty names field and the user names field have the same formatting:
a CSV terminated by a ':'.  Thus, we can --and should-- use the same
exact code for parsing both.

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

index 32bb0802531b324d2694ceeb02444a7ba7b8fd15..5cf34ddc973e996291193a306ddf7642d3bbb8a2 100644 (file)
@@ -154,16 +154,16 @@ next:
        for (cp = buf, j = 0; j < PORT_TTY; j++) {
                port.pt_names[j] = cp;
                cp = strpbrk(cp, ":,");
-               if (':' == *cp)         /* end of tty name list */
+               if (':' == *cp)
                        break;
-               if (',' == *cp)         /* end of current tty name */
+               if (',' == *cp)
                        stpcpy(cp++, "");
        }
+       port.pt_names[j] = NULL;
        if (':' != *cp)
                goto next;
 
        stpcpy(cp++, "");
-       port.pt_names[j] = NULL;
 
        /*
         * Get the list of user names.  It is the second colon
@@ -175,21 +175,16 @@ next:
        if (strchr(cp, ':') == NULL)
                goto next;
 
-       if (':' != *cp) {
-               port.pt_users = users;
-               port.pt_users[0] = cp;
-
-               for (j = 1; ':' != *cp; cp++) {
-                       if ((',' == *cp) && (j < PORT_IDS)) {
-                               stpcpy(cp++, "");
-                               port.pt_users[j] = cp;
-                               j++;
-                       }
-               }
-               port.pt_users[j] = 0;
-       } else {
-               port.pt_users = 0;
+       port.pt_users = users;
+       for (j = 0; j < PORT_IDS; j++) {
+               port.pt_users[j] = cp;
+               cp = strpbrk(cp, ":,");
+               if (':' == *cp)
+                       break;
+               if (',' == *cp)
+                       stpcpy(cp++, "");
        }
+       port.pt_users[j] = NULL;
        if (':' != *cp)
                goto next;
 
@@ -333,15 +328,11 @@ static struct port *getttyuser (const char *tty, const char *user)
        setportent ();
 
        while ((port = getportent ()) != NULL) {
-               if (NULL == port->pt_users)
-                       continue;
-
                for (i = 0; NULL != port->pt_names[i]; i++) {
                        if (portcmp (port->pt_names[i], tty) == 0) {
                                break;
                        }
                }
-
                if (port->pt_names[i] == 0) {
                        continue;
                }