From 964df6ed6ed650a7972799f2e13345746daa9a4d Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 12 May 2024 23:44:35 +0200 Subject: [PATCH] lib/, src/: Use strchrnul(3) instead of its pattern In the files where #include is missing, add it, and sort the includes. Signed-off-by: Alejandro Colomar --- lib/commonio.c | 23 ++++++++++------------- lib/console.c | 9 ++++++--- lib/getdef.c | 12 +++++++----- lib/gshadow.c | 11 ++--------- lib/hushed.c | 10 ++++++---- lib/port.c | 9 ++++++--- lib/sgetgrent.c | 6 +----- lib/sgetspent.c | 6 +----- lib/shadow.c | 24 ++++++++---------------- lib/ttytype.c | 12 ++++++------ lib/tz.c | 3 +-- src/useradd.c | 5 +---- 12 files changed, 55 insertions(+), 75 deletions(-) diff --git a/lib/commonio.c b/lib/commonio.c index 1d0ba8909..11e4f3c0f 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -11,28 +11,29 @@ #ident "$Id$" -#include "defines.h" #include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include #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 #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) { diff --git a/lib/console.c b/lib/console.c index 20e388ba5..fed315c3b 100644 --- a/lib/console.c +++ b/lib/console.c @@ -9,14 +9,18 @@ */ #include -#include "defines.h" + #include +#include + +#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; diff --git a/lib/getdef.c b/lib/getdef.c index 4b3dc5bb7..347a7f524 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -11,20 +11,22 @@ #ident "$Id$" -#include "prototypes.h" -#include "defines.h" +#include +#include #include #include #include -#include -#include +#include + #ifdef USE_ECONF #include #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. diff --git a/lib/gshadow.c b/lib/gshadow.c index 3c71bea4c..95b9cc73b 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -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; diff --git a/lib/hushed.c b/lib/hushed.c index 627a0595b..a0fc38f1b 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -12,12 +12,14 @@ #ident "$Id$" -#include -#include #include +#include +#include +#include + #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); } diff --git a/lib/port.c b/lib/port.c index 60ff8989e..d70957d97 100644 --- a/lib/port.c +++ b/lib/port.c @@ -11,12 +11,15 @@ #ident "$Id$" -#include #include #include +#include +#include + #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++) { diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index 26ddf5c2a..b6db47116 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -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, ":"); diff --git a/lib/sgetspent.c b/lib/sgetspent.c index bd2ef8b8d..1d4b6f105 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -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 diff --git a/lib/shadow.c b/lib/shadow.c index ab8f5d2a5..9b2a17df1 100644 --- a/lib/shadow.c +++ b/lib/shadow.c @@ -14,12 +14,13 @@ #ident "$Id$" -#include -#include "prototypes.h" -#include "defines.h" #include +#include +#include #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; diff --git a/lib/ttytype.c b/lib/ttytype.c index a9fb0e860..b07b68624 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -12,9 +12,13 @@ #ident "$Id$" #include -#include "prototypes.h" +#include + #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)) { diff --git a/lib/tz.c b/lib/tz.c index 83b295c75..c22268bf6 100644 --- 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) { diff --git a/src/useradd.c b/src/useradd.c index 3f0726849..ae28d87c9 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -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) { -- 2.47.3