diff options
Diffstat (limited to '')
-rw-r--r-- | lib/sgetgrent.c | 70 |
1 files changed, 24 insertions, 46 deletions
diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index ad4fcc8..6cde15c 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -14,6 +14,9 @@ #include <stdio.h> #include <sys/types.h> #include <grp.h> +#include <string.h> + +#include "alloc.h" #include "defines.h" #include "prototypes.h" @@ -25,19 +28,15 @@ * list() converts the comma-separated list of member names into * an array of character pointers. * - * WARNING: I profiled this once with and without strchr() calls - * and found that using a register variable and an explicit loop - * works best. For large /etc/group files, this is a major win. - * * FINALLY added dynamic allocation. Still need to fix sgetsgent(). * --marekm */ -static char **list (char *s) +static char ** +list(char *s) { - static char **members = 0; - static int size = 0; /* max members + 1 */ - int i; - char **rbuf; + static char **members = NULL; + static size_t size = 0; /* max members + 1 */ + size_t i; i = 0; for (;;) { @@ -45,40 +44,24 @@ static char **list (char *s) member name, or terminating NULL). */ if (i >= size) { size = i + 100; /* at least: i + 1 */ - if (members) { - rbuf = - realloc (members, size * sizeof (char *)); - } else { - /* for old (before ANSI C) implementations of - realloc() that don't handle NULL properly */ - rbuf = malloc (size * sizeof (char *)); - } - if (!rbuf) { - free (members); - members = 0; + members = REALLOCF(members, size, char *); + if (!members) { size = 0; - return (char **) 0; + return NULL; } - members = rbuf; } if (!s || s[0] == '\0') break; - members[i++] = s; - while (('\0' != *s) && (',' != *s)) { - s++; - } - if ('\0' != *s) { - *s++ = '\0'; - } + members[i++] = strsep(&s, ","); } - members[i] = (char *) 0; + members[i] = NULL; return members; } struct group *sgetgrent (const char *buf) { - static char *grpbuf = 0; + static char *grpbuf = NULL; static size_t size = 0; static char *grpfields[NFIELDS]; static struct group grent; @@ -90,10 +73,10 @@ struct group *sgetgrent (const char *buf) allocate a larger block */ free (grpbuf); size = strlen (buf) + 1000; /* at least: strlen(buf) + 1 */ - grpbuf = malloc (size); - if (!grpbuf) { + grpbuf = MALLOC(size, char); + if (grpbuf == NULL) { size = 0; - return 0; + return NULL; } } strcpy (grpbuf, buf); @@ -103,25 +86,20 @@ struct group *sgetgrent (const char *buf) *cp = '\0'; } - for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) { - grpfields[i] = cp; - cp = strchr (cp, ':'); - if (NULL != cp) { - *cp = '\0'; - cp++; - } - } + for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) + grpfields[i] = strsep(&cp, ":"); + if (i < (NFIELDS - 1) || *grpfields[2] == '\0' || cp != NULL) { - return (struct group *) 0; + return NULL; } grent.gr_name = grpfields[0]; grent.gr_passwd = grpfields[1]; - if (get_gid (grpfields[2], &grent.gr_gid) == 0) { - return (struct group *) 0; + if (get_gid(grpfields[2], &grent.gr_gid) == -1) { + return NULL; } grent.gr_mem = list (grpfields[3]); if (NULL == grent.gr_mem) { - return (struct group *) 0; /* out of memory */ + return NULL; /* out of memory */ } return &grent; |