Skip to content

Instantly share code, notes, and snippets.

@rootVIII
Last active June 26, 2020 15:41
Show Gist options
  • Save rootVIII/791e8aa9d75f275bd3755422ca85a582 to your computer and use it in GitHub Desktop.
Save rootVIII/791e8aa9d75f275bd3755422ca85a582 to your computer and use it in GitHub Desktop.
Golang realpath with CGO (Linux)
package main
/*
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
func currentDir() []byte {
return []byte{0x2e, 0x2f}
}
func rpath() string {
relativePath := currentDir()
limit := make([]byte, C.PATH_MAX)
slink := make([]byte, len(relativePath)+1)
copy(slink, relativePath)
C.realpath(
(*C.char)(unsafe.Pointer(&slink[0])),
(*C.char)(unsafe.Pointer(&limit[0])))
return C.GoString((*C.char)(unsafe.Pointer(&limit[0])))
}
func main() {
fmt.Println(rpath())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment