Skip to content

Commit 3ac8455

Browse files
committed
sys: consistent sprintf
1 parent 0c386e7 commit 3ac8455

20 files changed

+94
-97
lines changed

+stdlib/+python/hard_link_count.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
try
44
c = double(py.os.stat(p).st_nlink);
55
catch e
6-
warning(e.identifier, "hard_link_count(%s) failed: %s", p, e.message);
7-
c = 0;
6+
c = [];
87
end
98

109
end

+stdlib/+sys/cpu_load.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function L = cpu_load()
1+
function [L, cmd, m] = cpu_load()
22

33
L = NaN;
44

@@ -12,7 +12,6 @@
1212

1313
[status, m] = system(cmd);
1414
if status ~= 0
15-
warning("stdlib:cpu_load:OSError", "Failed to get CPU load");
1615
return
1716
end
1817

+stdlib/+sys/create_symlink.m

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11

2-
function ok = create_symlink(target, link)
2+
function [ok, cmd, m] = create_symlink(target, link)
33

44
ok = false;
5+
m = '';
56

6-
if ~stdlib.exists(target) || stdlib.strempty(link) || stdlib.exists(link)
7-
return
8-
end
9-
10-
if ispc
11-
cmd = sprintf('pwsh -c "New-Item -ItemType SymbolicLink -Path "%s" -Target "%s""', link, target);
7+
if ispc()
8+
cmd = sprintf('pwsh -c "New-Item -ItemType SymbolicLink -Path ''%s'' -Target ''%s''"', link, target);
129
else
1310
cmd = sprintf('ln -s "%s" "%s"', target, link);
1411
end
1512

16-
% suppress output text on powershell
17-
[stat, ~] = system(cmd);
18-
19-
ok = stat == 0;
13+
if stdlib.exists(target) && strlength(link) && ~stdlib.exists(link)
14+
[stat, m] = system(cmd);
15+
ok = stat == 0;
16+
end
2017

2118
end

+stdlib/+sys/disk_available.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
function t = disk_available(p)
1+
function [t, cmd] = disk_available(p)
22

33
t = uint64([]);
44

5-
if ~stdlib.exists(p), return, end
6-
75
if ispc()
86
dl = extractBefore(stdlib.absolute(p), 2);
97
cmd = "pwsh -c (Get-Volume -DriveLetter " + dl + ").SizeRemaining";
@@ -13,9 +11,11 @@
1311
cmd = sprintf('df -B1 "%s" | awk ''NR==2 {print $4}''', p);
1412
end
1513

16-
[s, t] = system(cmd);
17-
if s == 0
18-
t = uint64(str2double(t));
14+
if stdlib.exists(p)
15+
[s, t] = system(cmd);
16+
if s == 0
17+
t = uint64(str2double(t));
18+
end
1919
end
2020

2121
end

+stdlib/+sys/disk_capacity.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
function t = disk_capacity(p)
1+
function [t, cmd] = disk_capacity(p)
22

33
t = uint64([]);
44

5-
if ~stdlib.exists(p), return, end
6-
75
if ispc()
86
dl = extractBefore(stdlib.absolute(p), 2);
9-
cmd = "pwsh -c (Get-Volume -DriveLetter " + dl + ").Size";
7+
cmd = sprintf('pwsh -c "(Get-Volume -DriveLetter ''%s'').Size"', dl);
108
elseif ismac()
119
cmd = sprintf('df -k "%s" | awk ''NR==2 {print $2*1024}''', p);
1210
else
1311
cmd = sprintf('df -B1 "%s" | awk ''NR==2 {print $2}''', p);
1412
end
1513

16-
[s, t] = system(cmd);
17-
if s == 0
18-
t = str2double(t);
14+
if stdlib.exists(p)
15+
[s, t] = system(cmd);
16+
if s == 0
17+
t = str2double(t);
18+
end
1919
end
2020

2121
t = uint64(t);

+stdlib/+sys/file_checksum.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
%% SYS.FILE_CHECKSUM compute checksum of file
2-
function hash = file_checksum(file, hash_method)
2+
function [hash, cmd] = file_checksum(file, hash_method)
33

44
switch lower(hash_method)
55
case {"sha-256", "sha256"}
66
if ismac()
7-
cmd = "shasum --algorithm 256 --binary " + file;
7+
cmd = sprintf('shasum --algorithm 256 --binary "%s"', file);
88
elseif ispc()
9-
cmd = "CertUtil -hashfile " + file + " SHA256";
9+
cmd = sprintf('CertUtil -hashfile "%s" SHA256', file);
1010
else
11-
cmd = "sha256sum --binary " + file;
11+
cmd = sprintf('sha256sum --binary "%s"', file);
1212
end
1313
case "md5"
1414
if ismac()
15-
cmd = "md5 -r " + file;
15+
cmd = sprintf('md5 -r "%s"', file);
1616
elseif ispc()
17-
cmd = "CertUtil -hashfile " + file + " MD5";
17+
cmd = sprintf('CertUtil -hashfile "%s" MD5', file);
1818
else
19-
cmd = "md5sum " + file;
19+
cmd = sprintf('md5sum "%s"', file);
2020
end
2121
otherwise, error('unhandled hash method %s', hash_method)
2222
end

+stdlib/+sys/filesystem_type.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
function t = filesystem_type(p)
1+
function [t, cmd] = filesystem_type(p)
22

33
t = '';
44

5-
if ~stdlib.exists(p), return, end
6-
75
if ispc()
86
dl = extractBefore(stdlib.absolute(p), 2);
9-
cmd = sprintf('pwsh -c (Get-Volume -DriveLetter "%s").FileSystem', dl);
7+
cmd = sprintf('pwsh -c "(Get-Volume -DriveLetter ''%s'').FileSystem"', dl);
108
elseif ismac()
119
cmd = sprintf('df -aHY "%s" | awk ''NR==2 {print $2}''', p);
1210
else
1311
cmd = sprintf('df --output=fstype "%s" | tail -n 1', p);
1412
end
1513

16-
[s, t] = system(cmd);
17-
if s == 0
18-
t = strip(t);
14+
if stdlib.exists(p)
15+
[s, t] = system(cmd);
16+
if s == 0
17+
t = strip(t);
18+
end
1919
end
2020

2121
end

+stdlib/+sys/get_hostname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function n = get_hostname()
1+
function [n, cmd] = get_hostname()
22

33
cmd = 'hostname';
44
[s, n] = system(cmd);

+stdlib/+sys/get_owner.m

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
function o = get_owner(p)
1+
function [o, cmd] = get_owner(p)
22

33
o = "";
4-
if ~stdlib.exists(p)
5-
return
6-
end
74

85
if ispc()
96
cmd = sprintf('pwsh -c "if($x=Get-Acl -Path ''%s'') {$x.Owner}"', p);
@@ -13,9 +10,11 @@
1310
cmd = sprintf('stat -c %%U "%s"', p);
1411
end
1512

16-
[s, m] = system(cmd);
17-
if s == 0
18-
o = string(strip(m));
13+
if stdlib.exists(p)
14+
[s, m] = system(cmd);
15+
if s == 0
16+
o = string(strip(m));
17+
end
1918
end
2019

2120
end

+stdlib/+sys/get_username.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function n = get_username()
1+
function [n, cmd] = get_username()
22

33
if ispc()
44
cmd = 'whoami';

0 commit comments

Comments
 (0)