-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Theophanes
authored and
Daniel Theophanes
committed
May 5, 2012
0 parents
commit d29bca0
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package osext | ||
|
||
// Returns the full path of the running executable | ||
// as reported by the system. Includes the executable | ||
// image name. | ||
func GetExePath() (exePath string, err error) { | ||
return getExePath() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package osext | ||
|
||
import "errors" | ||
|
||
// _NSGetExecutablePath() | ||
func getExePath() (exePath string, err error) { | ||
return "", errors.New("Unimplemented") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package osext | ||
|
||
// FreeBSD: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1 | ||
// BSD with procfs: readlink /proc/curproc/file | ||
func getExePath() (exePath string, err error) { | ||
return os.Readlink(`/proc/curproc/file`) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package osext | ||
|
||
import "os" | ||
|
||
// readlink /proc/self/exe | ||
func getExePath() (exePath string, err error) { | ||
return os.Readlink(`/proc/self/exe`) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package osext | ||
|
||
import "testing" | ||
|
||
func TestGetExePath(t *testing.T) { | ||
p, err := GetExePath() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("IMG EXE PATH: %s", p) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package osext | ||
|
||
import ( | ||
"unsafe" | ||
"utf16" | ||
) | ||
|
||
var ( | ||
kernel = syscall.MustLoadDLL("kernel32.dll") | ||
getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW") | ||
) | ||
|
||
// GetModuleFileName() with hModule = NULL | ||
func getExePath() (exePath string, err error) { | ||
return getModuleFileName() | ||
} | ||
|
||
func getModuleFileName() (string, error) { | ||
var n uint32 | ||
b := make([]uint16, syscall.MAX_PATH) | ||
size := uint32(len(b)) | ||
|
||
r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size)) | ||
n = uint32(r0) | ||
if n == 0 { | ||
return "", e1 | ||
} | ||
return string(utf16.Decode(b[0:n])), nil | ||
} |