-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_darwin.go
74 lines (60 loc) · 1.17 KB
/
dl_darwin.go
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
package dl
// #cgo CFLAGS: -W -Wall -Wno-unused-parameter -O3
// #cgo LDFLAGS: -ldl
import "C"
import (
"os"
"path/filepath"
"strings"
)
func find(name string) (path string, err error) {
if strings.ContainsRune(name, '/') {
path = name
return
}
if len(filepath.Ext(name)) == 0 {
name += ".dylib"
}
p1 := getPaths("LD_LIBRARY_PATH")
p2 := getPaths("DYLD_LIBRARY_PATH")
p3 := getPaths("DYLD_FALLBACK_LIBRARY_PATH")
if len(p3) == 0 {
p3 = []string{
filepath.Join(os.Getenv("HOME"), "lib"),
"/usr/local/lib",
"/usr/lib",
}
}
dirs := make([]string, 0, 100)
dirs = append(dirs, p1...)
dirs = append(dirs, p2...)
dirs = append(dirs, ".")
dirs = append(dirs, p3...)
ok := false
for _, dir := range dirs {
filepath.Walk(dir, func(p string, f os.FileInfo, e error) error {
if !ok && f != nil && !f.IsDir() && strings.HasPrefix(f.Name(), name) {
path, ok = p, true
}
return nil
})
if ok {
break
}
}
if !ok {
err = os.ErrNotExist
}
return
}
func getPaths(env string) []string {
paths := strings.Split(os.Getenv(env), ":")
i := 0
for _, p := range paths {
if len(p) != 0 {
paths[i] = p
i++
}
}
return paths[:i]
}