ladybird/Userland/id.cpp

42 lines
1.1 KiB
C++
Raw Normal View History

#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <alloca.h>
2018-10-22 12:06:22 +00:00
int main(int argc, char** argv)
2018-10-22 12:06:22 +00:00
{
(void) argc;
(void) argv;
2018-10-22 12:06:22 +00:00
uid_t uid = getuid();
gid_t gid = getgid();
struct passwd* pw = getpwuid(uid);
2018-11-06 21:27:51 +00:00
struct group* gr = getgrgid(gid);
printf("uid=%u(%s), gid=%u(%s)", uid, pw ? pw->pw_name : "n/a", gid, gr ? gr->gr_name : "n/a", getpid());
int extra_gid_count = getgroups(0, nullptr);
if (extra_gid_count) {
auto* extra_gids = (gid_t*)alloca(extra_gid_count * sizeof(gid_t));
int rc = getgroups(extra_gid_count, extra_gids);
if (rc < 0) {
perror("\ngetgroups");
return 1;
}
printf(",");
for (int g = 0; g < extra_gid_count; ++g) {
auto* gr = getgrgid(extra_gids[g]);
if (gr)
printf("%u(%s)", extra_gids[g], gr->gr_name);
else
printf("%u", extra_gids[g]);
if (g != extra_gid_count - 1)
printf(",");
}
}
printf("\n");
2018-10-22 12:06:22 +00:00
return 0;
}