aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c28
-rw-r--r--lib/lib.h1
-rw-r--r--toys/posix/ps.c2
3 files changed, 30 insertions, 1 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;
+}
diff --git a/lib/lib.h b/lib/lib.h
index aafa9693..b1f4ca11 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -205,6 +205,7 @@ char *strnstr(char *line, char *str);
int dev_minor(int dev);
int dev_major(int dev);
int dev_makedev(int major, int minor);
+struct passwd *bufgetpwuid(uid_t uid);
#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units
diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 9557b64d..445228fd 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -453,7 +453,7 @@ static char *string_field(struct carveup *tb, struct strawberry *field)
if (gr) out = gr->gr_name;
} else {
- struct passwd *pw = getpwuid(ll);
+ struct passwd *pw = bufgetpwuid(ll);
if (pw) out = pw->pw_name;
}