Skip to content

Commit 0951fc7

Browse files
committed
lint / docs
1 parent 4afd3a2 commit 0951fc7

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

+stdlib/file_size.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
%%% Inputs
44
% * p: path to file
55
%%% Outputs
6-
% * s: size in bytes, or empty if file does not exist
6+
% * s: size in bytes; NaN if file does not exist
77

88
function s = file_size(p)
99
arguments

+stdlib/is_executable_binary.m

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,71 @@
1-
function y = is_executable_binary(filename)
21
% IS_EXECUTABLE_BINARY Check if a file is an executable binary by examining magic numbers
32
%
43
% NOTE: on Unix-like operating systems, often times what users run thinking
54
% it's a "program" is actually a shell script invoking a binary with
65
% options.
76
% fullfile(matlabroot, 'bin/matlab')
87
% is a shell script and thus is FALSE for this function.
8+
%
9+
% Inputs:
10+
% * filename: path to file
11+
% Outputs:
12+
% * y: true if the file is an executable binary, false otherwise
13+
%
14+
% This function checks the magic number of the file to determine if it is
15+
% an executable binary. It supports Windows (PE), macOS (Mach-O), and Linux
16+
% (ELF) formats.
17+
18+
function y = is_executable_binary(filename)
19+
arguments
20+
filename {mustBeTextScalar}
21+
end
922

1023
y = false;
1124

1225
fid = fopen(filename, 'rb');
13-
if fid < 0, return, end
26+
if fid < 0
27+
return
28+
end
1429

1530
N = 4;
16-
if ispc(), N = 2; end
31+
if ispc()
32+
N = 2;
33+
end
1734

1835
magic = fread(fid, N, 'uint8').';
1936
fclose(fid);
20-
if numel(magic) < N, return, end
37+
38+
if numel(magic) == N
39+
y = check_magic(magic);
40+
end
41+
42+
end
43+
44+
45+
function y = check_magic(magic)
2146

2247
if ispc()
23-
y = isequal(magic(1:2), [0x4d, 0x5a]);
24-
% Check for PE magic number (MZ)
48+
y = isequal(magic(1:2), [0x4d, 0x5a]);
49+
% Check for PE magic number (MZ)
2550
elseif ismac()
26-
% Check for Mach-O magic number
27-
feedface = [0xFE, 0xED, 0xFA, 0xCE];
28-
feedfacf = [0xFE, 0xED, 0xFA, 0xCF];
29-
cafebabe = [0xCA, 0xFE, 0xBA, 0xBE];
30-
cafebabf = [0xCA, 0xFE, 0xBA, 0xBF];
31-
for a = {feedface, fliplr(feedface), ...
32-
feedfacf, fliplr(feedfacf), ...
33-
cafebabe, fliplr(cafebabe), ...
34-
cafebabf, fliplr(cafebabf)}
35-
36-
y = isequal(magic, a{1});
37-
if y, return, end
51+
% Check for Mach-O magic number
52+
feedface = [0xFE, 0xED, 0xFA, 0xCE];
53+
feedfacf = [0xFE, 0xED, 0xFA, 0xCF];
54+
cafebabe = [0xCA, 0xFE, 0xBA, 0xBE];
55+
cafebabf = [0xCA, 0xFE, 0xBA, 0xBF];
56+
for a = {feedface, fliplr(feedface), ...
57+
feedfacf, fliplr(feedfacf), ...
58+
cafebabe, fliplr(cafebabe), ...
59+
cafebabf, fliplr(cafebabf)}
60+
61+
y = isequal(magic, a{1});
62+
if y
63+
return
3864
end
65+
end
3966
else
40-
% Check for ELF magic number (0x7f followed by 'ELF')
41-
y = isequal(magic, [0x7f, 0x45, 0x4c, 0x46]);
67+
% Check for ELF magic number (0x7f followed by 'ELF')
68+
y = isequal(magic, [0x7f, 0x45, 0x4c, 0x46]);
4269
end
4370

4471
end

0 commit comments

Comments
 (0)