Last active
June 26, 2020 15:41
-
-
Save rootVIII/791e8aa9d75f275bd3755422ca85a582 to your computer and use it in GitHub Desktop.
Golang realpath with CGO (Linux)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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