-
Notifications
You must be signed in to change notification settings - Fork 0
/
myls.m
executable file
·110 lines (99 loc) · 2.43 KB
/
myls.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function F = myls(D)
% F = myls(D) returns a cell array of strings containing the files
% in directory D.
%
% 2008-10-29 Dan Ellis [email protected]
%%%%% Check for network mode
OVERNET = 0;
if length(D) > 6
if strcmp(lower(D(1:7)),'http://') == 1 ...
| strcmp(lower(D(1:6)),'ftp://')
% mp3info not available over network
OVERNET = 1;
end
end
if ~OVERNET
try
S = ls('-1',D);
catch me
S = '';
end
F = tokenize(S,char(10));
else
% Network mode - experimental
% Will only work on Unix, with curl installed etc.
% Break up the request string into root and pattern
[p,n,e] = fileparts(D);
p = [p,'/'];
n = [n,e];
if length(n) == 0; n = '*'; end
% Convert glob pattern to grep pattern
% "." becomes "\."
n = stringsub(n,'.','\.');
% "*" becomes ".*"
n = stringsub(n,'*','.*');
n = ['^',n,'$'];
r = mysystem(['curl -s ',p,...
' | grep href | sed -e ''s/.*href=\"\([^\"]*\)\".*/\1/'' | grep ''',...
n,'''']);
% break into cell array on line feeds
F = tokenize(r,char(10));
% prepend the root URL in each case
% If returned URL is complete, keep it all
% If it begins with "/", just keep the host we got it from
% else, prepend the whole root URL
ix = find(p == '/');
% find the 3rd one (first two are from http://)
machpart = p(1:ix(3)-1);
for i = 1:length(F)
f = F{i};
if f(1) == '/'
F{i} = [machpart,f];
elseif strcmp(f(1:7),'http://') == 0
F{i} = [p,f];
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function T = stringsub(S,F,R)
% Replace all instances of substring F in string S with substring R.
% Return as T.
ix = strfind(S,F);
T = '';
b = 1;
for i = ix
T = [T,S(b:(i-1)),R];
b = i + length(F);
end
T = [T,S(b:end)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function w = mysystem(cmd)
% Run system command; report error; return lines
[s,w] = system(cmd);
if s ~= 0
error(['unable to execute ',cmd,' (',w,')']);
end
% Debug
%disp([cmd,' -> ','*',w,'*']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function a = tokenize(s,t)
% Break space-separated string into cell array of strings
% 2004-09-18 [email protected]
if nargin < 2; t = ' '; end
a = [];
p = 1;
n = 1;
l = length(s);
nss = findstr([s(p:end),t],t);
for ns = nss
% Skip initial spaces (tokens)
if ns == p
p = p+1;
else
if p <= l
a{n} = s(p:(ns-1));
n = n+1;
p = ns+1;
end
end
end