-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoikwinwhich.erl
168 lines (140 loc) · 4.18 KB
/
aoikwinwhich.erl
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
%%
uniq(Item_s) ->
lists:foldl(
fun(Item, Acc) ->
ItemExists = lists:member(Item, Acc),
if ItemExists ->
Acc
;true ->
lists:append(Acc, [Item])
end
end, [], Item_s).
find_executale(Prog) ->
%% 8f1kRCu
EnvPATHENV = os:getenv("PATHEXT"),
%%% can be false
%% 6qhHTHF
%% split into a list of extensions
Ext_s =
if EnvPATHENV == false ->
[]
;true ->
string:tokens(EnvPATHENV, ";")
end,
%% 2pGJrMW
%% strip
Ext_s2 = lists:map(fun(X) -> string:strip(X) end, Ext_s),
%% 2gqeHHl
%% remove empty
Ext_s3 = lists:filter(fun(X) -> X =/= "" end, Ext_s2),
%% 2zdGM8W
%% convert to lowercase
Ext_s4 = lists:map(fun(X) -> string:to_lower(X) end, Ext_s3),
%% 2fT8aRB
%% uniquify
Ext_s5 = gb_sets:to_list(gb_sets:from_list(Ext_s4)),
%% 4ysaQVN
EnvPATH = os:getenv("PATH"),
%%% can be false
%% 6mPI0lg
Dir_s =
if EnvPATH == false ->
[]
;true ->
string:tokens(EnvPATH, ";")
end,
%% 5rT49zI
%% insert empty dir path to the beginning
%%
%% Empty dir handles the case that |prog| is a path, either relative or
%% absolute. See code 7rO7NIN.
Dir_s2 = ["" | Dir_s],
%% 2klTv20
%% uniquify
Dir_s3 = uniq(Dir_s2),
%%
ProgLc = string:to_lower(Prog),
ProgHasExt = lists:any(fun(Ext) -> lists:suffix(Ext, ProgLc) end, Ext_s5),
%% 6bFwhbv
Exe_path_s_res = lists:foldl(
fun(Dir, Acc) ->
%% 7rO7NIN
%% synthesize a path with the dir and prog
Path =
if Dir == "" ->
Prog
;true ->
string:join([Dir, "\\" ,Prog], "")
end,
%% 6kZa5cq
%% assume the path has extension, check if it is an executable
PathExists = filelib:is_regular(Path),
if ProgHasExt andalso PathExists ->
Exe_path_s = [Path]
;true ->
Exe_path_s = []
end,
%% 2sJhhEV
%% assume the path has no extension
Exe_path_s2 = [
%% 6k9X6GP
%% synthesize a new path with the path and the executable extension
string:concat(Path, Ext) ||
Ext <- Ext_s5,
%% 6kabzQg
%% check if it is an executable
filelib:is_regular(string:concat(Path, Ext))
],
%% New Acc result
lists:append([Acc, Exe_path_s, Exe_path_s2])
end, [], Dir_s3),
%% 8swW6Av
%% uniquify
Exe_path_s_res2 = uniq(Exe_path_s_res),
%%
Exe_path_s_res2.
println(Str) ->
io:format("~s~n", [Str]).
main(Args) ->
%% 9mlJlKg
if length(Args) =/= 1 ->
%% 7rOUXFo
%% print program usage
println("Usage: aoikwinwhich PROG"),
println(""),
println("#/ PROG can be either name or path"),
println("aoikwinwhich notepad.exe"),
println("aoikwinwhich C:\\Windows\\notepad.exe"),
println(""),
println("#/ PROG can be either absolute or relative"),
println("aoikwinwhich C:\\Windows\\notepad.exe"),
println("aoikwinwhich Windows\\notepad.exe"),
println(""),
println("#/ PROG can be either with or without extension"),
println("aoikwinwhich notepad.exe"),
println("aoikwinwhich notepad"),
println("aoikwinwhich C:\\Windows\\notepad.exe"),
println("aoikwinwhich C:\\Windows\\notepad"),
%% 3nqHnP7
noop
;true ->
%% 9m5B08H
%% get name or path of a program from cmd arg
Prog = lists:nth(1, Args),
%% 8ulvPXM
%% find executables
Path_s = find_executale(Prog),
%% 5fWrcaF
%% has found none, exit
if length(Path_s) == 0 ->
%% 3uswpx0
noop
;true ->
%% 9xPCWuS
%% has found some, output
Txt = string:join(Path_s, "\n"),
println(Txt),
%% 4s1yY1b
noop
end
end.