]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Simplify by calling strdup(3)
authorAlejandro Colomar <alx@kernel.org>
Sat, 7 Dec 2024 12:12:11 +0000 (13:12 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 30 May 2025 21:11:16 +0000 (16:11 -0500)
While at it, rename the function parameter.

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

index 2725e09ebaf5bf0d8ab5ab066304287fb0712161..86559b31a3fd54f526561ed7a61c8c33f4f30277 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #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");
 
        /*
index eeeed4b6f816e06327af62839e529ebd5abdc6fb..37608344d3a7883b17a6cc5649a51182f9706de2 100644 (file)
 
 #ident "$Id$"
 
-#include <stdio.h>
-#include <sys/types.h>
 #include <grp.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
 
 #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++)