libc+apps: Avoid calling endpwent() and endgrent() after every call to get{pw,gr}{nam,uid,gid}

This commit is contained in:
apio 2023-05-18 21:47:46 +02:00
parent 84bed3ceb3
commit 0dbfbe6395
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 15 additions and 24 deletions

View File

@ -40,6 +40,8 @@ Result<int> luna_main(int argc, char** argv)
}
gid = grp->gr_gid;
endgrent();
}
}
}
@ -73,6 +75,8 @@ Result<int> luna_main(int argc, char** argv)
gid = pw->pw_gid;
}
endpwent();
}
}

View File

@ -74,6 +74,7 @@ Result<int> luna_main(int argc, char** argv)
struct passwd* pw = getpwuid(getuid());
if (pw) { username = pw->pw_name; }
else { username = getenv("USER"); }
endpwent();
}
while (1)

View File

@ -78,6 +78,8 @@ Result<int> luna_main(int argc, char** argv)
return 1;
}
endpwent();
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
{
char* pass = getpass();

View File

@ -1,4 +1,5 @@
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <luna/Vector.h>
#include <stdio.h>
@ -20,6 +21,7 @@ extern "C"
{
f = fopen("/etc/group", "r");
if (!f) return nullptr;
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
}
while (true)
@ -82,14 +84,9 @@ extern "C"
while ((entry = getgrent()))
{
if (!strcmp(entry->gr_name, name))
{
endgrent();
return entry;
}
if (!strcmp(entry->gr_name, name)) { return entry; }
}
endgrent();
return entry;
}
@ -101,14 +98,9 @@ extern "C"
while ((entry = getgrent()))
{
if (entry->gr_gid == gid)
{
endgrent();
return entry;
}
if (entry->gr_gid == gid) { return entry; }
}
endgrent();
return entry;
}

View File

@ -1,3 +1,4 @@
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
@ -45,6 +46,7 @@ extern "C"
{
f = fopen("/etc/passwd", "r");
if (!f) return nullptr;
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
}
while (true)
@ -93,14 +95,9 @@ extern "C"
while ((entry = getpwent()))
{
if (!strcmp(entry->pw_name, name))
{
endpwent();
return entry;
}
if (!strcmp(entry->pw_name, name)) { return entry; }
}
endpwent();
return entry;
}
@ -112,14 +109,9 @@ extern "C"
while ((entry = getpwent()))
{
if (entry->pw_uid == uid)
{
endpwent();
return entry;
}
if (entry->pw_uid == uid) { return entry; }
}
endpwent();
return entry;
}