]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use strchrnul(3) instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Sun, 12 May 2024 21:44:35 +0000 (23:44 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 2 Jul 2024 02:40:11 +0000 (21:40 -0500)
In the files where #include <string.h> is missing, add it, and sort the
includes.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
12 files changed:
lib/commonio.c
lib/console.c
lib/getdef.c
lib/gshadow.c
lib/hushed.c
lib/port.c
lib/sgetgrent.c
lib/sgetspent.c
lib/shadow.c
lib/ttytype.c
lib/tz.c
src/useradd.c

index 1d0ba8909cc54fc31076285947dfbe848e4ba966..11e4f3c0ff8e18303503acd94178d9d6b52080dc 100644 (file)
 
 #ident "$Id$"
 
-#include "defines.h"
 #include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
 #include <sys/stat.h>
 #include <stdlib.h>
-#include <limits.h>
 #include <utime.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdio.h>
-#include <signal.h>
 
 #include "alloc.h"
 #include "atoi/getnum.h"
+#include "commonio.h"
+#include "defines.h"
 #include "memzero.h"
 #include "nscd.h"
-#include "sssd.h"
 #ifdef WITH_TCB
 #include <tcb.h>
 #endif                         /* WITH_TCB */
 #include "prototypes.h"
-#include "commonio.h"
 #include "shadowlog_internal.h"
+#include "sssd.h"
 #include "string/sprintf/snprintf.h"
 
 
@@ -639,7 +640,6 @@ int commonio_open (struct commonio_db *db, int mode)
                goto cleanup_errno;
 
        while (db->ops->fgets (buf, buflen, db->fp) == buf) {
-               char                   *cp;
                struct commonio_entry  *p;
 
                while (   (strrchr (buf, '\n') == NULL)
@@ -658,10 +658,7 @@ int commonio_open (struct commonio_db *db, int mode)
                                goto cleanup_buf;
                        }
                }
-               cp = strrchr (buf, '\n');
-               if (NULL != cp) {
-                       *cp = '\0';
-               }
+               *strchrnul(buf, '\n') = '\0';
 
                line = strdup (buf);
                if (NULL == line) {
index 20e388ba5be1f7407655cb03b3d17f083ed1b48a..fed315c3b1ea007c280658331611abfed4763311 100644 (file)
@@ -9,14 +9,18 @@
  */
 
 #include <config.h>
-#include "defines.h"
+
 #include <stdio.h>
+#include <string.h>
+
+#include "defines.h"
 #include "getdef.h"
 #include "prototypes.h"
 #include "string/strcpy/strtcpy.h"
 
 #ident "$Id$"
 
+
 /*
  * This is now rather generic function which decides if "tty" is listed
  * under "cfgin" in config (directly or indirectly). Fallback to default if
@@ -72,8 +76,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def)
         */
 
        while (fgets (buf, sizeof (buf), fp) != NULL) {
-               /* Remove optional trailing '\n'. */
-               buf[strcspn (buf, "\n")] = '\0';
+               *strchrnul(buf, '\n') = '\0';
                if (strcmp (buf, tty) == 0) {
                        (void) fclose (fp);
                        return true;
index 4b3dc5bb73e3b1e83cee3739223a014bd4772a99..347a7f524b8360216902b1142f087f3d62f0956e 100644 (file)
 
 #ident "$Id$"
 
-#include "prototypes.h"
-#include "defines.h"
+#include <ctype.h>
+#include <errno.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
-#include <errno.h>
+#include <string.h>
+
 #ifdef USE_ECONF
 #include <libeconf.h>
 #endif
 
 #include "alloc.h"
 #include "atoi/str2i.h"
+#include "defines.h"
 #include "getdef.h"
+#include "prototypes.h"
 #include "shadowlog_internal.h"
 #include "string/sprintf/xasprintf.h"
 
@@ -579,7 +581,7 @@ static void def_load (void)
 
                *s++ = '\0';
                value = s + strspn (s, " \"\t");        /* next nonwhite */
-               *(value + strcspn (value, "\"")) = '\0';
+               *strchrnul(value, '"') = '\0';
 
                /*
                 * Store the value in def_table.
index 3c71bea4cad9f6caed7621ebc86934f408fa64af..95b9cc73b4955d2874a5966a078b8f1d4a50ecaa 100644 (file)
@@ -93,11 +93,7 @@ void endsgent (void)
        }
 
        strcpy (sgrbuf, string);
-
-       cp = strrchr (sgrbuf, '\n');
-       if (NULL != cp) {
-               *cp = '\0';
-       }
+       *strchrnul(sgrbuf, '\n') = '\0';
 
        /*
         * There should be exactly 4 colon separated fields.  Find
@@ -178,10 +174,7 @@ void endsgent (void)
                                return NULL;
                        }
                }
-               cp = strrchr (buf, '\n');
-               if (NULL != cp) {
-                       *cp = '\0';
-               }
+               *strchrnul(buf, '\n') = '\0';
                return (sgetsgent (buf));
        }
        return NULL;
index 627a0595b3cf7e8cc2e45d3592b871d2e83d693d..a0fc38f1b9d178c1de6da697427709cde420fa39 100644 (file)
 
 #ident "$Id$"
 
-#include <sys/types.h>
-#include <stdio.h>
 #include <pwd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+
 #include "defines.h"
-#include "prototypes.h"
 #include "getdef.h"
+#include "prototypes.h"
 #include "string/sprintf/snprintf.h"
 
 
@@ -70,7 +72,7 @@ bool hushed (const char *username)
                return false;
        }
        for (found = false; !found && (fgets (buf, sizeof buf, fp) == buf);) {
-               buf[strcspn (buf, "\n")] = '\0';
+               *strchrnul(buf, '\n') = '\0';
                found = (strcmp (buf, pw->pw_shell) == 0) ||
                        (strcmp (buf, pw->pw_name) == 0);
        }
index 60ff8989e58c93cf0fe437405f39a83b93cbe861..d70957d97deb701d454d3d60e453f85db289f282 100644 (file)
 
 #ident "$Id$"
 
-#include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
 #include "defines.h"
-#include "prototypes.h"
 #include "port.h"
+#include "prototypes.h"
+
 
 static FILE *ports;
 
@@ -149,7 +152,7 @@ again:
         * TTY devices.
         */
 
-       buf[strcspn (buf, "\n")] = 0;
+       *strchrnul(buf, '\n') = '\0';
 
        port.pt_names = ttys;
        for (cp = buf, j = 0; j < PORT_TTY; j++) {
index 26ddf5c2a83bff91fd3cdd0a819dbbd99a7c0be5..b6db471166c6f6d536aa435d09d1df80d4379422 100644 (file)
@@ -82,11 +82,7 @@ struct group *sgetgrent (const char *buf)
                }
        }
        strcpy (grpbuf, buf);
-
-       cp = strrchr (grpbuf, '\n');
-       if (NULL != cp) {
-               *cp = '\0';
-       }
+       *strchrnul(grpbuf, '\n') = '\0';
 
        for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)
                grpfields[i] = strsep(&cp, ":");
index bd2ef8b8de97e776d9c669fd90548ea08ace55dc..1d4b6f105bb1235e043eabe9350f56d43b898bb8 100644 (file)
@@ -53,11 +53,7 @@ sgetspent(const char *string)
                return NULL;    /* fail if too long */
        }
        strcpy (spwbuf, string);
-
-       cp = strrchr (spwbuf, '\n');
-       if (NULL != cp) {
-               *cp = '\0';
-       }
+       *strchrnul(spwbuf, '\n') = '\0';
 
        /*
         * Tokenize the string into colon separated fields.  Allow up to
index ab8f5d2a5c58671fd6a52e051fbd908b31754a77..9b2a17df125ef9514c1afd108219524ace8ccfff 100644 (file)
 
 #ident "$Id$"
 
-#include <sys/types.h>
-#include "prototypes.h"
-#include "defines.h"
 #include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
 
 #include "atoi/str2i.h"
+#include "defines.h"
+#include "prototypes.h"
 
 
 static FILE *shadow;
@@ -75,10 +76,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (strlen (string) >= sizeof spwbuf)
                return 0;
        strcpy (spwbuf, string);
-
-       cp = strrchr (spwbuf, '\n');
-       if (NULL != cp)
-               *cp = '\0';
+       *strchrnul(spwbuf, '\n') = '\0';
 
        /*
         * Tokenize the string into colon separated fields.  Allow up to
@@ -87,9 +85,7 @@ static struct spwd *my_sgetspent (const char *string)
 
        for (cp = spwbuf, i = 0; *cp && i < FIELDS; i++) {
                fields[i] = cp;
-               while (*cp && *cp != ':')
-                       cp++;
-
+               cp = strchrnul(cp, ':');
                if (*cp)
                        *cp++ = '\0';
        }
@@ -229,8 +225,7 @@ static struct spwd *my_sgetspent (const char *string)
 
 struct spwd *fgetspent (FILE * fp)
 {
-       char buf[BUFSIZ];
-       char *cp;
+       char  buf[BUFSIZ];
 
        if (NULL == fp) {
                return (0);
@@ -238,10 +233,7 @@ struct spwd *fgetspent (FILE * fp)
 
        if (fgets (buf, sizeof buf, fp) != NULL)
        {
-               cp = strchr (buf, '\n');
-               if (NULL != cp) {
-                       *cp = '\0';
-               }
+               *strchrnul(buf, '\n') = '\0';
                return my_sgetspent (buf);
        }
        return 0;
index a9fb0e860631300cbbb87e12d34bd56f06e4826d..b07b686245e03c42e955f6e8ecae73a41962431a 100644 (file)
 #ident "$Id$"
 
 #include <stdio.h>
-#include "prototypes.h"
+#include <string.h>
+
 #include "defines.h"
 #include "getdef.h"
+#include "prototypes.h"
+
+
 /*
  * ttytype - set ttytype from port to terminal type mapping database
  */
@@ -23,7 +27,6 @@ void ttytype (const char *line)
        FILE *fp;
        char buf[BUFSIZ];
        const char *typefile;
-       char *cp;
        char type[1024] = "";
        char port[1024];
 
@@ -46,10 +49,7 @@ void ttytype (const char *line)
                        continue;
                }
 
-               cp = strchr (buf, '\n');
-               if (NULL != cp) {
-                       *cp = '\0';
-               }
+               *strchrnul(buf, '\n') = '\0';
 
                if (   (sscanf (buf, "%1023s %1023s", type, port) == 2)
                    && (strcmp (line, port) == 0)) {
index 83b295c7591d26dd047c83f23a394e1b850be627..c22268bf62e0efe32ab349c42de43f45dbf0520f 100644 (file)
--- a/lib/tz.c
+++ b/lib/tz.c
@@ -42,8 +42,7 @@
 
                strcpy (tzbuf, def_tz);
        } else {
-               /* Remove optional trailing '\n'. */
-               tzbuf[strcspn (tzbuf, "\n")] = '\0';
+               *strchrnul(tzbuf, '\n') = '\0';
        }
 
        if (NULL != fp) {
index 3f07268496b62463ff861685b8d0f14c77d990b5..ae28d87c9b2cc26d62952c70ace5370633296ffd 100644 (file)
@@ -360,10 +360,7 @@ static void get_defaults (void)
         * values are used, everything else can be ignored.
         */
        while (fgets (buf, sizeof buf, fp) == buf) {
-               cp = strrchr (buf, '\n');
-               if (NULL != cp) {
-                       *cp = '\0';
-               }
+               *strchrnul(buf, '\n') = '\0';
 
                cp = strchr (buf, '=');
                if (NULL == cp) {