logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 5c32c02e12a219c652677d799151ec225d30e55c
parent 350b3f01d7811fc9368fce7ad750066f95b28238
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 12 Mar 2022 02:54:21 +0100

bin/id: Add support for -u

Diffstat:

Mbin/id.c38++++++++++++++++++++++++++++++++++++--
Mtest-bin/id9+++++++++
2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/bin/id.c b/bin/id.c @@ -142,7 +142,7 @@ int main(int argc, char *argv[]) { int ret, c; - bool euid_flag = false; + bool u_flag = false; bool G_flag = false; bool g_flag = false; bool real_flag = false; @@ -159,7 +159,7 @@ main(int argc, char *argv[]) name_flag = true; break; case 'u': - euid_flag = true; + u_flag = true; break; case 'g': g_flag = true; @@ -209,6 +209,40 @@ main(int argc, char *argv[]) return 0; } + if(u_flag) + { + uid_t uid; + + if(real_flag) + { + uid = getuid(); + } + else + { + uid = geteuid(); + } + + if(!name_flag) + { + ret = printf("%u\n", uid); + } + else + { + struct passwd *pw = getpwuid(uid); + + assert(pw->pw_uid == uid); + + ret = printf("%s\n", pw->pw_name); + } + + if(ret < 0) + { + return 1; + } + + return 0; + } + // geteuid, getuid, getegid, getgid shall always be successful uid_t uid = getuid(); uid_t euid = geteuid(); diff --git a/test-bin/id b/test-bin/id @@ -35,6 +35,14 @@ group_body() { atf_check -o "inline:$(id -gnr)\n" ../bin/id -gnr } +atf_test_case user +user_body() { + atf_check -o "inline:$(id -u)\n" ../bin/id -u + atf_check -o "inline:$(id -ur)\n" ../bin/id -ur + atf_check -o "inline:$(id -un)\n" ../bin/id -un + atf_check -o "inline:$(id -unr)\n" ../bin/id -unr +} + atf_test_case noetc noetc_body() { bwrap_args="--bind / / --bind /var/empty /etc" @@ -69,6 +77,7 @@ atf_init_test_cases() { atf_add_test_case noargs atf_add_test_case names atf_add_test_case group + atf_add_test_case user atf_add_test_case noetc }