blob: 48a660104fb4d0174810ec3c3e451d841a0ecd1b [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// Copyright 2015 CoreOS, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// +build linux
16
17package dlopen
18
19// #include <string.h>
20// #include <stdlib.h>
21//
22// int
23// my_strlen(void *f, const char *s)
24// {
25// size_t (*strlen)(const char *);
26//
27// strlen = (size_t (*)(const char *))f;
28// return strlen(s);
29// }
30import "C"
31
32import (
33 "fmt"
34 "unsafe"
35)
36
37func strlen(libs []string, s string) (int, error) {
38 h, err := GetHandle(libs)
39 if err != nil {
40 return -1, fmt.Errorf(`couldn't get a handle to the library: %v`, err)
41 }
42 defer h.Close()
43
44 f := "strlen"
45 cs := C.CString(s)
46 defer C.free(unsafe.Pointer(cs))
47
48 strlen, err := h.GetSymbolPointer(f)
49 if err != nil {
50 return -1, fmt.Errorf(`couldn't get symbol %q: %v`, f, err)
51 }
52
53 len := C.my_strlen(strlen, cs)
54
55 return int(len), nil
56}