feat(passwd): getpw*/getgr*/setpwent/endpwent family

This commit is contained in:
2026-09-05 23:25:24 -04:00
parent 9b576757cf
commit 523045b5ae
6 changed files with 1549 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
#ifndef VLIBC_GRP_H
#define VLIBC_GRP_H
/*
* vlibc — <grp.h>.
*
* The group database (todo 37). struct group mirrors /etc/group, whose
* records are name:passwd:gid:member,member,... — the member list is a
* comma-separated tail, empty when the group has no members. Everything
* here is POSIX.1-2008 base (Level 1).
*
* The non-reentrant forms getgrnam/getgrgid/getgrent return a pointer to a
* static structure whose contents — including the strings and the member
* array it points at — are overwritten by the next call to any of the
* three. getgrnam/getgrgid open and scan the whole file per call; getgrent
* walks a cursor that setgrent rewinds and endgrent closes.
*
* The _r forms behave like their <pwd.h> counterparts: the entry is written
* into caller storage (the gr_mem pointer array and the member name strings
* both live inside the supplied buffer) and the return value is 0 on
* success, ERANGE when the buffer is too small, or 0 with *result NULL when
* no entry matches. errno is never touched: the error number IS the return
* value.
*/
#include <vlibc/features.h>
#include <stddef.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/* One group database entry (/etc/group record). */
struct group
{
char *gr_name; /* group name */
char *gr_passwd; /* encrypted password; usually empty */
gid_t gr_gid; /* numeric group id */
char **gr_mem; /* NULL-terminated member-name array */
};
/*
* Look up the first entry whose name matches; NULL when absent. The result
* points into shared static storage valid only until the next grp call.
*/
struct group *
getgrnam(const char *name);
/*
* Look up the first entry whose gid matches; NULL when absent. Result
* storage is shared with getgrnam/getgrent as described above.
*/
struct group *
getgrgid(gid_t gid);
/*
* Return the next entry from the group stream, opening it on the first
* call; NULL at end of file. The stream cursor advances per call.
*/
struct group *
getgrent(void);
/* Rewind the group stream to its first entry. */
void
setgrent(void);
/* Close the group stream; a later getgrent reopens from the start. */
void
endgrent(void);
int
getgrnam_r(const char *name, struct group *gr, char *buf, size_t bufsize, struct group **result);
int
getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t bufsize, struct group **result);
int
getgrent_r(struct group *gr, char *buf, size_t bufsize, struct group **result);
#if VLIBC_LEVEL_GE(2)
/* Level 2 (muslmimic): XSI. */
#include <stdio.h> /* FILE */
/*
* Return the next entry read from the given open stream, or NULL at end of
* file or on a malformed/unopenable record; the entry is parsed as an
* /etc/group record. Result storage is shared with getgrnam/getgrgid/
* getgrent as described above, so the stream must not be closed until the
* caller has copied the result. XSI.
*/
struct group *
fgetgrent(FILE *stream);
#endif /* VLIBC_LEVEL_GE(2) */
#ifdef __cplusplus
}
#endif
#endif /* VLIBC_GRP_H */
+90
View File
@@ -0,0 +1,90 @@
#ifndef VLIBC_PWD_H
#define VLIBC_PWD_H
/*
* vlibc — <pwd.h>.
*
* The password database (todo 37). struct passwd mirrors /etc/passwd, whose
* records are name:passwd:uid:gid:gecos:dir:shell — seven ':'-separated
* fields. Everything here is POSIX.1-2008 base (Level 1).
*
* The non-reentrant forms getpwnam/getpwuid/getpwent return a pointer to a
* static structure whose contents — including the strings it points at —
* are overwritten by the next call to any of the three, so the result must
* be copied out before the next database call. getpwnam/getpwuid open and
* scan the whole file per call; getpwent walks a cursor that setpwent
* rewinds and endpwent closes.
*
* The getpwnam_r/getpwuid_r/getpwent_r forms write the entry into
* caller-supplied storage (struct, char buffer of bufsize bytes) and
* return 0 on success with *result pointing at the filled struct, or an
* error number as the return value with *result left NULL: ERANGE when the
* buffer is too small for the entry, 0 with *result NULL when no entry
* matches (or the stream is exhausted). errno is never touched by any _r
* function: the error number IS the return value.
*/
#include <vlibc/features.h>
#include <stddef.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/* One password database entry (/etc/passwd record). */
struct passwd
{
char *pw_name; /* user's login name */
char *pw_passwd; /* encrypted password; often "x" with shadow files */
uid_t pw_uid; /* numeric user id */
gid_t pw_gid; /* numeric group id */
char *pw_gecos; /* user information (comment) field */
char *pw_dir; /* home directory */
char *pw_shell; /* login shell */
};
/*
* Look up the first entry whose name matches; NULL when absent. The result
* points into shared static storage valid only until the next pwd call.
*/
struct passwd *
getpwnam(const char *name);
/*
* Look up the first entry whose uid matches; NULL when absent. Result
* storage is shared with getpwnam/getpwent as described above.
*/
struct passwd *
getpwuid(uid_t uid);
/*
* Return the next entry from the password stream, opening it on the first
* call; NULL at end of file. The stream cursor advances per call.
*/
struct passwd *
getpwent(void);
/* Rewind the password stream to its first entry. */
void
setpwent(void);
/* Close the password stream; a later getpwent reopens from the start. */
void
endpwent(void);
int
getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t bufsize, struct passwd **result);
int
getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t bufsize, struct passwd **result);
int
getpwent_r(struct passwd *pw, char *buf, size_t bufsize, struct passwd **result);
#ifdef __cplusplus
}
#endif
#endif /* VLIBC_PWD_H */
+15
View File
@@ -544,6 +544,21 @@ getsubopt(char **optionp, char *const *tokens, char **valuep);
#endif /* VLIBC_LEVEL_GE(2) */
#if VLIBC_LEVEL_GE(2)
/*
* Resolve path to an absolute, NUL-terminated canonical pathname with no
* symbolic links, "." or ".." components (todo 38). resolved_path may be
* NULL, in which case the canonical path is returned in a malloc'd
* buffer the caller must free; otherwise it must hold at least PATH_MAX
* bytes. Returns resolved_path (or the malloc'd buffer), or NULL with
* errno set. XSI.
*/
char *
realpath(const char *restrict path, char *restrict resolved_path);
#endif /* VLIBC_LEVEL_GE(2) */
#ifdef __cplusplus
}
#endif