summaryrefslogtreecommitdiff
path: root/src/passwd/getpw_a.c
diff options
context:
space:
mode:
authorJosiah Worcester <josiahw@gmail.com>2015-02-10 18:32:55 -0600
committerRich Felker <dalias@aerifal.cx>2015-02-10 22:57:02 -0500
commit700e08993c3f6a808773d56424aa7e633da13e2e (patch)
tree752558943d55b652eb61469e818ac1dceaad6238 /src/passwd/getpw_a.c
parent74e334dcd177b585c64ddafa732a3dc9e3f6b5ec (diff)
downloadmusl-700e08993c3f6a808773d56424aa7e633da13e2e.tar.gz
refactor passwd file access code
this allows getpwnam and getpwuid to share code with the _r versions in preparation for alternate backend support.
Diffstat (limited to 'src/passwd/getpw_a.c')
-rw-r--r--src/passwd/getpw_a.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/passwd/getpw_a.c b/src/passwd/getpw_a.c
new file mode 100644
index 00000000..21efc5ca
--- /dev/null
+++ b/src/passwd/getpw_a.c
@@ -0,0 +1,31 @@
+#include "pwf.h"
+#include <pthread.h>
+
+int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res)
+{
+ FILE *f;
+ int cs;
+ int rv = 0;
+
+ *res = 0;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
+ f = fopen("/etc/passwd", "rbe");
+ if (!f) {
+ rv = errno;
+ goto done;
+ }
+
+ while (!(rv = __getpwent_a(f, pw, buf, size, res)) && *res) {
+ if (name && !strcmp(name, (*res)->pw_name)
+ || !name && (*res)->pw_uid == uid)
+ break;
+ }
+ fclose(f);
+
+done:
+ pthread_setcancelstate(cs, 0);
+ if (rv) errno = rv;
+ return rv;
+}