github.com/primecitizens/pcz/std@v0.2.1/core/sys/os_darwin.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  //go:build !noos && darwin
     5  
     6  package sys
     7  
     8  import (
     9  	"unsafe"
    10  
    11  	stdptr "github.com/primecitizens/pcz/std/builtin/ptr"
    12  	stdstring "github.com/primecitizens/pcz/std/builtin/string"
    13  	"github.com/primecitizens/pcz/std/core/arch"
    14  )
    15  
    16  var (
    17  	//go:linkname executablePath
    18  	executablePath string
    19  )
    20  
    21  func ExecutablePath() string { return executablePath }
    22  
    23  func init() {
    24  	base := unsafe.SliceData(args)
    25  	if base == nil { // no args
    26  		return
    27  	}
    28  
    29  	// base is the envv
    30  	base = stdptr.Add(base, arch.PtrSize*uintptr(len(args)+1 /* NULL sep */))
    31  	n := 0
    32  	for p := base; *p != nil; p = stdptr.Add(p, arch.PtrSize) {
    33  		n++
    34  	}
    35  	envs = unsafe.Slice(base, n)
    36  	// envs[n] = nil
    37  	// envs[n+1] = "executable_path="
    38  	executablePath = stdstring.FromByteArray(*stdptr.Add(base, arch.PtrSize*uintptr(n+1)))
    39  
    40  	// strip "executable_path=" prefix if available, it's added after OS X 10.11.
    41  	const prefix = "executable_path="
    42  	if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
    43  		executablePath = executablePath[len(prefix):]
    44  	}
    45  }