aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c28
-rw-r--r--lib/lib.h1
-rw-r--r--toys/posix/find.c4
-rw-r--r--toys/posix/ls.c12
-rw-r--r--toys/posix/ps.c2
5 files changed, 37 insertions, 10 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 98597b24..66814a45 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1102,3 +1102,31 @@ struct passwd *bufgetpwuid(uid_t uid)
return &list->pw;
}
+
+// Return cached passwd entries.
+struct group *bufgetgrgid(gid_t gid)
+{
+ struct grgidbuf_list {
+ struct grgidbuf_list *next;
+ struct group gr;
+ } *list;
+ struct group *temp;
+ static struct grgidbuf_list *grgidbuf;
+
+ for (list = grgidbuf; list; list = list->next)
+ if (list->gr.gr_gid == gid) return &(list->gr);
+
+ list = xmalloc(512);
+ list->next = grgidbuf;
+
+ errno = getgrgid_r(gid, &list->gr, sizeof(*list)+(char *)list,
+ 512-sizeof(*list), &temp);
+ if (!temp) {
+ free(list);
+
+ return 0;
+ }
+ grgidbuf = list;
+
+ return &list->gr;
+}
diff --git a/lib/lib.h b/lib/lib.h
index b1f4ca11..f545a794 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -206,6 +206,7 @@ int dev_minor(int dev);
int dev_major(int dev);
int dev_makedev(int major, int minor);
struct passwd *bufgetpwuid(uid_t uid);
+struct group *bufgetgrgid(gid_t gid);
#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 3b27225d..189773c3 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -303,9 +303,9 @@ static int do_find(struct dirtree *new)
if (check) do_print(new, s[5] ? 0 : '\n');
} else if (!strcmp(s, "nouser")) {
- if (check) if (getpwuid(new->st.st_uid)) test = 0;
+ if (check) if (bufgetpwuid(new->st.st_uid)) test = 0;
} else if (!strcmp(s, "nogroup")) {
- if (check) if (getgrgid(new->st.st_gid)) test = 0;
+ if (check) if (bufgetgrgid(new->st.st_gid)) test = 0;
} else if (!strcmp(s, "prune")) {
if (check && S_ISDIR(new->st.st_mode) && !TT.depth) recurse = 0;
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 2ebc062a..3ad28da4 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -123,7 +123,7 @@ static char endtype(struct stat *st)
static char *getusername(uid_t uid)
{
- struct passwd *pw = getpwuid(uid);
+ struct passwd *pw = bufgetpwuid(uid);
sprintf(TT.uid_buf, "%u", (unsigned)uid);
return pw ? pw->pw_name : TT.uid_buf;
@@ -131,7 +131,7 @@ static char *getusername(uid_t uid)
static char *getgroupname(gid_t gid)
{
- struct group *gr = getgrgid(gid);
+ struct group *gr = bufgetgrgid(gid);
sprintf(TT.gid_buf, "%u", (unsigned)gid);
return gr ? gr->gr_name : TT.gid_buf;
@@ -334,12 +334,10 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Do preprocessing (Dirtree didn't populate, so callback wasn't called.)
for (;dt; dt = dt->next) filter(dt);
if (flags == (FLAG_1|FLAG_f)) return;
- } else {
- // Read directory contents. We dup() the fd because this will close it.
- // This reads/saves contents to display later, except for in "ls -1f" mode.
- dirtree_recurse(indir, filter, dup(dirfd),
+ // Read directory contents. We dup() the fd because this will close it.
+ // This reads/saves contents to display later, except for in "ls -1f" mode.
+ } else dirtree_recurse(indir, filter, dup(dirfd),
DIRTREE_SYMFOLLOW*!!(flags&FLAG_L));
- }
// Copy linked list to array and sort it. Directories go in array because
// we visit them in sorted order too. (The nested loops let us measure and
diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 445228fd..301c1f36 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -449,7 +449,7 @@ static char *string_field(struct carveup *tb, struct strawberry *field)
sprintf(out, "%lld", ll);
if (sl&64) {
if (which > PS_RUSER) {
- struct group *gr = getgrgid(ll);
+ struct group *gr = bufgetgrgid(ll);
if (gr) out = gr->gr_name;
} else {