From: Alejandro Colomar Date: Sat, 7 Dec 2024 12:12:11 +0000 (+0100) Subject: lib/: Simplify by calling strdup(3) X-Git-Tag: 4.18.0-rc1~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c7692f1dc20115b608a791f39dfb7299e84c8d47;p=thirdparty%2Fshadow.git lib/: Simplify by calling strdup(3) While at it, rename the function parameter. Signed-off-by: Alejandro Colomar --- diff --git a/lib/gshadow.c b/lib/gshadow.c index 2725e09eb..86559b31a 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -15,6 +15,7 @@ #include #include +#include #include #include "alloc/malloc.h" @@ -68,26 +69,19 @@ void endsgent (void) } /*@observer@*//*@null@*/struct sgrp * -sgetsgent(const char *string) +sgetsgent(const char *s) { static char *sgrbuf = NULL; - static size_t sgrbuflen = 0; char *fields[FIELDS]; char *cp; int i; - size_t len = strlen (string) + 1; - if (len > sgrbuflen) { - char *buf = REALLOC(sgrbuf, len, char); - if (NULL == buf) - return NULL; - - sgrbuf = buf; - sgrbuflen = len; - } + free(sgrbuf); + sgrbuf = strdup(s); + if (sgrbuf == NULL) + return NULL; - strcpy (sgrbuf, string); stpsep(sgrbuf, "\n"); /* diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index eeeed4b6f..37608344d 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -11,10 +11,11 @@ #ident "$Id$" -#include -#include #include +#include +#include #include +#include #include "alloc/malloc.h" #include "alloc/reallocf.h" @@ -64,27 +65,20 @@ list(char *s) } -struct group *sgetgrent (const char *buf) +struct group * +sgetgrent(const char *s) { static char *grpbuf = NULL; - static size_t size = 0; static char *grpfields[NFIELDS]; static struct group grent; int i; char *cp; - if (strlen (buf) + 1 > size) { - /* no need to use realloc() here - just free it and - allocate a larger block */ - free (grpbuf); - size = strlen (buf) + 1000; /* at least: strlen(buf) + 1 */ - grpbuf = MALLOC(size, char); - if (grpbuf == NULL) { - size = 0; - return NULL; - } - } - strcpy (grpbuf, buf); + free(grpbuf); + grpbuf = strdup(s); + if (grpbuf == NULL) + return NULL; + stpsep(grpbuf, "\n"); for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)