-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_linux.go
60 lines (48 loc) · 961 Bytes
/
dl_linux.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
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 += ".so"
}
dirs := make([]string, 0, 100)
dirs = append(dirs, getPaths("LD_LIBRARY_PATH")...)
dirs = append(dirs, "/lib", "/usr/lib")
ok := false
for _, dir := range dirs {
filepath.Walk(dir, func(p string, f os.FileInfo, e error) error {
if !ok && f != nil && !f.IsDir() && (f.Mode() & 0600) != 0 && 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]
}