Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Platform/POSIX/Users.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include <unistd.h>
7
8#include "Stroika/Foundation/Execution/Exceptions.h"
10
11#include "Users.h"
12
13using namespace Stroika::Foundation;
14using namespace Stroika::Foundation::Execution;
16
19
20/*
21 ********************************************************************************
22 ************************ Platform::POSIX::UserName2UID *************************
23 ********************************************************************************
24 */
25uid_t Platform::POSIX::UserName2UID (const String& name)
26{
27 size_t bufsize = ::sysconf (_SC_GETPW_R_SIZE_MAX);
28 if (bufsize == -1) { /* Value was indeterminate */
29 bufsize = 16384; /* Should be more than enough */
30 }
31 StackBuffer<char> buf{Memory::eUninitialized, bufsize};
32
33 struct passwd pwd{};
34 struct passwd* result = nullptr;
35 int err = ::getpwnam_r (name.AsNarrowSDKString ().c_str (), &pwd, buf.data (), bufsize, &result);
36 if (err < 0) [[unlikely]] {
37 ThrowPOSIXErrNo (err);
38 }
39 if (result == nullptr) [[unlikely]] {
40 Execution::Throw (RuntimeErrorException{"No such username"sv});
41 }
42 return pwd.pw_uid;
43}
44
45/*
46 ********************************************************************************
47 ********************** Platform::POSIX::uid_t2UserName *************************
48 ********************************************************************************
49 */
50String Platform::POSIX::uid_t2UserName (uid_t uid)
51{
52 size_t bufsize = ::sysconf (_SC_GETPW_R_SIZE_MAX);
53 if (bufsize == -1) { /* Value was indeterminate */
54 bufsize = 16384; /* Should be more than enough */
55 }
56 StackBuffer<char> buf{Memory::eUninitialized, bufsize};
57
58 struct passwd pwd{};
59 struct passwd* result = nullptr;
60 int err = ::getpwuid_r (uid, &pwd, buf.data (), bufsize, &result);
61 if (err < 0) {
62 ThrowPOSIXErrNo (err);
63 }
64 if (result == nullptr) {
65 static const auto kException_ = RuntimeErrorException{"No such username"sv};
66 Execution::Throw (kException_);
67 }
68 return String::FromSDKString (pwd.pw_name);
69}
70
71/*
72 ********************************************************************************
73 ****************************** Platform::POSIX::GetUID *************************
74 ********************************************************************************
75 */
76uid_t Platform::POSIX::GetUID ()
77{
78 return ::getuid ();
79}
80
81/*
82 ********************************************************************************
83 ********************* Platform::POSIX::GetEffectiveUID *************************
84 ********************************************************************************
85 */
86uid_t Platform::POSIX::GetEffectiveUID ()
87{
88 return ::geteuid ();
89}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual string AsNarrowSDKString() const
Definition String.inl:830
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
void ThrowPOSIXErrNo(errno_t errNo=errno)
treats errNo as a POSIX errno value, and throws a SystemError (subclass of @std::system_error) except...