I am unable to return the char pointer from the getrpath() function. I know that the C function is being called in the below code because a printf can print the realpath. I can’t seem to get the char* back into the main() function though.
Any advice? Thank you.
package main
/*
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
char* getrpath(char *symlink) {
char *actualpath = malloc(PATH_MAX);
return realpath(symlink, actualpath)
}
*/
import "C"
import (
"fmt"
"os"
"path"
"unsafe"
)
func getArgv() string {
return fmt.Sprintf("./%s", path.Base(os.Args[0]))
}
func main() {
slink := C.CString(getArgv())
defer C.free(unsafe.Pointer(slink))
ret := C.getrpath(slink)
fmt.Println(C.GoString(ret))
}