-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgethostname.m
53 lines (48 loc) · 1.35 KB
/
gethostname.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function hostName = gethostname
%GETHOSTNAME Get host name.
% HOSTNAME = GETHOSTNAME returns the name of the computer that MATLAB
% is running on. Function should work for both Linux and Windows.
%
% Markus Buehren
% Last modified: 20.08.2009
%
% See also GETUSERNAME.
persistent hostNamePersistent
if isempty(hostNamePersistent)
if ispc
hostName = getenv('COMPUTERNAME');
else
hostName = getenv('HOSTNAME');
end
if isempty(hostName)
% the environment variable above was not existing
if ispc
systemCall = 'hostname';
else
systemCall = 'uname -n';
end
if ispc
% The current directory may be a network-directory. This is not
% supported by Windows' cmd.exe, which results in a wrong host
% name. Therefore we return to a default-directory first.
currDir = cd;
cd('C:\');
end
[status, hostName] = system(systemCall);
if ispc
cd(currDir);
end
if status ~= 0
error('System call "%s" failed with return code %d.', systemCall, status);
end
hostName = hostName(1:end-1);
end
% environment variable and system call might result different, so only
% allow upper case letters
hostName = upper(hostName);
% save string for next function call
hostNamePersistent = hostName;
else
% return string computed before
hostName = hostNamePersistent;
end