Browse Source

lib/pwdutils: add xgetgrnam

Signed-off-by: Matthew Harm Bekkema <id@mbekkema.name>
pull/1005/head
Matthew Harm Bekkema 2 years ago
parent
commit
dc96ca29d8
  1. 2
      include/pwdutils.h
  2. 32
      lib/pwdutils.c

2
include/pwdutils.h

@ -3,8 +3,10 @@
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
extern struct passwd *xgetpwnam(const char *username, char **pwdbuf);
extern struct group *xgetgrnam(const char *groupname, char **grpbuf);
extern struct passwd *xgetpwuid(uid_t uid, char **pwdbuf);
extern char *xgetlogin(void);

32
lib/pwdutils.c

@ -36,6 +36,38 @@ failed:
return NULL;
}
/* Returns allocated group and allocated grpbuf to store group strings
* fields. In case of error returns NULL and set errno, for unknown group set
* errno to EINVAL
*/
struct group *xgetgrnam(const char *groupname, char **grpbuf)
{
struct group *grp = NULL, *res = NULL;
int rc;
if (!grpbuf || !groupname)
return NULL;
*grpbuf = xmalloc(UL_GETPW_BUFSIZ);
grp = xcalloc(1, sizeof(struct group));
errno = 0;
rc = getgrnam_r(groupname, grp, *grpbuf, UL_GETPW_BUFSIZ, &res);
if (rc != 0) {
errno = rc;
goto failed;
}
if (!res) {
errno = EINVAL;
goto failed;
}
return grp;
failed:
free(grp);
free(*grpbuf);
return NULL;
}
struct passwd *xgetpwuid(uid_t uid, char **pwdbuf)
{
struct passwd *pwd = NULL, *res = NULL;

Loading…
Cancel
Save