Skip to content

Commit 6defce0

Browse files
committed
add get_uid()
1 parent afd641d commit 6defce0

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

+stdlib/+dotnet/get_uid.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function u = get_uid()
2+
3+
if ispc()
4+
u = char(System.Security.Principal.WindowsIdentity.GetCurrent().User.Value);
5+
else
6+
u = '';
7+
end
8+
9+
end

+stdlib/+perl/get_uid.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function u = get_uid()
2+
3+
u = [];
4+
if ispc()
5+
return
6+
end
7+
8+
cmd = sprintf('"%s" -e "print $<"', stdlib.perl_exe());
9+
10+
[s, r] = system(cmd);
11+
if s == 0
12+
u = str2double(r);
13+
end
14+
15+
end

+stdlib/+python/get_uid.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function u = get_uid()
2+
3+
if isunix()
4+
u = double(py.os.geteuid());
5+
else
6+
u = [];
7+
end
8+
9+
end

+stdlib/Backend.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
switch functionName
6060
case {'create_symlink', 'ram_total', 'read_symlink'}
6161
if stdlib.dotnet_api() < 6, continue, end
62-
case {'get_owner', 'is_admin'}
62+
case {'get_owner', 'get_uid', 'is_admin'}
6363
if isunix(), continue, end
6464
end
6565
case 'java'
@@ -71,6 +71,11 @@
7171
case {'device', 'hard_link_count' , 'inode', 'is_admin'}
7272
if ispc(), continue, end
7373
end
74+
case 'perl'
75+
switch functionName
76+
case {'get_uid'}
77+
if ispc(), continue, end
78+
end
7479
case 'python'
7580
if ~any(ismember(self.backends, 'python'))
7681
continue
@@ -79,7 +84,7 @@
7984
switch functionName
8085
case {'filesystem_type', 'is_removable', 'ram_total', 'ram_free'}
8186
if ~stdlib.python.has_psutil(); continue, end
82-
case {'cpu_load', 'get_owner'}
87+
case {'cpu_load', 'get_owner', 'get_uid'}
8388
if ispc(), continue, end
8489
case 'is_admin'
8590
if ispc() || stdlib.matlabOlderThan('R2024a'), continue, end

+stdlib/get_uid.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%% GET_UID tell UID (numeric) of current user
2+
%
3+
%% Outputs
4+
% * n: UID of current user
5+
% * b: backend used
6+
7+
function [n, b] = get_uid(backend)
8+
arguments
9+
backend (1,:) string = ["dotnet", "python", "perl"]
10+
end
11+
12+
o = stdlib.Backend(mfilename(), backend);
13+
n = o.func();
14+
15+
b = o.backend;
16+
17+
end

test/TestSys.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
B_os_version
1313
B_hostname
1414
B_username
15+
B_get_uid
1516
end
1617

1718
methods (TestParameterDefinition, Static)
18-
function [B_cpu_arch, B_cpu_load, B_ram_free, B_ram_total, B_is_admin, B_os_version, B_hostname, B_username] = setupBackends()
19+
function [B_cpu_arch, B_cpu_load, B_ram_free, B_ram_total, B_is_admin, B_os_version, B_hostname, B_username, B_get_uid] = setupBackends()
1920
B_cpu_arch = init_backend("cpu_arch");
2021
B_cpu_load = init_backend("cpu_load");
2122
B_ram_free = init_backend("ram_free");
@@ -24,6 +25,7 @@
2425
B_os_version = init_backend("os_version");
2526
B_hostname = init_backend("hostname");
2627
B_username = init_backend("get_username");
28+
B_get_uid = init_backend('get_uid');
2729
end
2830
end
2931

@@ -69,6 +71,17 @@ function test_hostname(tc, B_hostname)
6971
end
7072

7173

74+
function test_get_uid(tc, B_get_uid)
75+
u = stdlib.get_uid(B_get_uid);
76+
if ispc()
77+
tc.verifyClass(u, 'char')
78+
tc.verifyGreaterThan(strlength(u), 0)
79+
else
80+
tc.verifyNotEmpty(u)
81+
end
82+
end
83+
84+
7285
function test_username(tc, B_username)
7386
u = stdlib.get_username(B_username);
7487
tc.verifyClass(u, 'char')

0 commit comments

Comments
 (0)