summaryrefslogtreecommitdiff
path: root/libgcompat
diff options
context:
space:
mode:
Diffstat (limited to 'libgcompat')
-rw-r--r--libgcompat/pwd.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libgcompat/pwd.c b/libgcompat/pwd.c
new file mode 100644
index 0000000..2e09aea
--- /dev/null
+++ b/libgcompat/pwd.c
@@ -0,0 +1,55 @@
+/* some musl versions incorrectly mark fgetpwent() as a GNU extension */
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <errno.h>
+#include <pwd.h>
+#include <string.h>
+
+
+int getpwent_r(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp) {
+ struct passwd *pwd;
+
+ if (pwbufp == NULL || pwbuf == NULL)
+ return ERANGE;
+
+ if (buflen < 1)
+ return ERANGE;
+
+ if (buf != NULL)
+ *buf = '\0';
+
+ if ((pwd = getpwent()) == NULL) {
+ *pwbufp = NULL;
+ return ENOENT;
+ }
+
+ memcpy(pwbuf, pwd, sizeof(*pwd));
+ *pwbufp = pwbuf;
+
+ return 0;
+}
+
+
+int fgetpwent_r(FILE *filp, struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp) {
+ struct passwd *pwd;
+
+ if (pwbufp == NULL || pwbuf == NULL)
+ return ERANGE;
+
+ if (buflen < 1)
+ return ERANGE;
+
+ if (buf != NULL)
+ *buf = '\0';
+
+ if ((pwd = fgetpwent(filp)) == NULL) {
+ *pwbufp = NULL;
+ return ENOENT;
+ }
+
+ memcpy(pwbuf, pwd, sizeof(*pwd));
+ *pwbufp = pwbuf;
+
+ return 0;
+}