aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-05-17 12:10:07 -0500
committerRob Landley <rob@landley.net>2016-05-17 12:10:07 -0500
commit6d50d4cbbf6ff34ed68dba61d30c434b57b23332 (patch)
tree68dfe801c6a0f09f51a54d72bf957998031b35e4 /lib/lib.c
parent713055b349b2e6adc7ada2640cd7a40b3d311fb6 (diff)
downloadtoybox-6d50d4cbbf6ff34ed68dba61d30c434b57b23332.tar.gz
Add bufgetpwuid. (Repeated calls to getpwuid() are really expensive.)
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 0df45bf9..98597b24 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1074,3 +1074,31 @@ int dev_makedev(int major, int minor)
{
return (minor&0xff)|((major&0xfff)<<8)|((minor&0xfff00)<<12);
}
+
+// Return cached passwd entries.
+struct passwd *bufgetpwuid(uid_t uid)
+{
+ struct pwuidbuf_list {
+ struct pwuidbuf_list *next;
+ struct passwd pw;
+ } *list;
+ struct passwd *temp;
+ static struct pwuidbuf_list *pwuidbuf;
+
+ for (list = pwuidbuf; list; list = list->next)
+ if (list->pw.pw_uid == uid) return &(list->pw);
+
+ list = xmalloc(512);
+ list->next = pwuidbuf;
+
+ errno = getpwuid_r(uid, &list->pw, sizeof(*list)+(char *)list,
+ 512-sizeof(*list), &temp);
+ if (!temp) {
+ free(list);
+
+ return 0;
+ }
+ pwuidbuf = list;
+
+ return &list->pw;
+}