// Copyright 2009,2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Darwin system calls.
// This file is compiled as ordinary Go code,
// but it is also input to mksyscall,
// which parses the //sys lines and generates system call stubs.
// Note that sometimes we use a lowercase //sys name and wrap
// it in our own nicer implementation, either here or in
// syscall_bsd.go or syscall_unix.go.

package syscall

import 

type SockaddrDatalink struct {
	Len    uint8
	Family uint8
	Index  uint16
	Type   uint8
	Nlen   uint8
	Alen   uint8
	Slen   uint8
	Data   [12]int8
	raw    RawSockaddrDatalink
}

// Translate "kern.hostname" to []_C_int{0,1,2,3}.
func ( string) ( []_C_int,  error) {
	const  = unsafe.Sizeof([0])

	// NOTE(rsc): It seems strange to set the buffer to have
	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
	// as the size. I don't know why the +2 is here, but the
	// kernel uses +2 for its own implementation of this function.
	// I am scared that if we don't include the +2 here, the kernel
	// will silently write 2 words farther than we specify
	// and we'll get memory corruption.
	var  [CTL_MAXNAME + 2]_C_int
	 := uintptr(CTL_MAXNAME) * 

	 := (*byte)(unsafe.Pointer(&[0]))
	,  := ByteSliceFromString()
	if  != nil {
		return nil, 
	}

	// Magic sysctl: "setting" 0.3 to a string name
	// lets you read back the array of integers form.
	if  = sysctl([]_C_int{0, 3}, , &, &[0], uintptr(len()));  != nil {
		return nil, 
	}
	return [0 : /], nil
}

func ( []byte) (uint64, bool) {
	return readInt(, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
}

func ( []byte) (uint64, bool) {
	return readInt(, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
}

func ( []byte) (uint64, bool) {
	return readInt(, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
}

func ( int) ( error) { return ptrace(PT_ATTACH, , 0, 0) }
func ( int) ( error) { return ptrace(PT_DETACH, , 0, 0) }

const (
	attrBitMapCount = 5
	attrCmnModtime  = 0x00000400
	attrCmnAcctime  = 0x00001000
)

type attrList struct {
	bitmapCount uint16
	_           uint16
	CommonAttr  uint32
	VolAttr     uint32
	DirAttr     uint32
	FileAttr    uint32
	Forkattr    uint32
}

//sysnb pipe(p *[2]int32) (err error)

func ( []int) ( error) {
	if len() != 2 {
		return EINVAL
	}
	var  [2]int32
	 = pipe(&)
	[0] = int([0])
	[1] = int([1])
	return
}

func ( []Statfs_t,  int) ( int,  error) {
	var  unsafe.Pointer
	var  uintptr
	if len() > 0 {
		 = unsafe.Pointer(&[0])
		 = unsafe.Sizeof(Statfs_t{}) * uintptr(len())
	}
	, ,  := syscall(funcPC(libc_getfsstat_trampoline), uintptr(), , uintptr())
	 = int()
	if  != 0 {
		 = 
	}
	return
}

func ()

//go:linkname libc_getfsstat libc_getfsstat
//go:cgo_import_dynamic libc_getfsstat getfsstat "/usr/lib/libSystem.B.dylib"

func ( string,  []Timespec) error {
	,  := BytePtrFromString()
	if  != nil {
		return 
	}

	var  attrList
	.bitmapCount = attrBitMapCount
	.CommonAttr = attrCmnModtime | attrCmnAcctime

	// order is mtime, atime: the opposite of Chtimes
	 := [2]Timespec{[1], [0]}
	const  = 0
	, ,  := syscall6(
		funcPC(libc_setattrlist_trampoline),
		uintptr(unsafe.Pointer()),
		uintptr(unsafe.Pointer(&)),
		uintptr(unsafe.Pointer(&)),
		uintptr(unsafe.Sizeof()),
		uintptr(),
		0,
	)
	if  != 0 {
		return 
	}
	return nil
}

func ()

//go:linkname libc_setattrlist libc_setattrlist
//go:cgo_import_dynamic libc_setattrlist setattrlist "/usr/lib/libSystem.B.dylib"

func ( int,  string,  *[2]Timespec,  int) error {
	// Darwin doesn't support SYS_UTIMENSAT
	return ENOSYS
}

/*
 * Wrapped
 */

//sys	kill(pid int, signum int, posix int) (err error)

func ( int,  Signal) ( error) { return kill(, int(), 1) }

/*
 * Exposed directly
 */
//sys	Access(path string, mode uint32) (err error)
//sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
//sys	Chdir(path string) (err error)
//sys	Chflags(path string, flags int) (err error)
//sys	Chmod(path string, mode uint32) (err error)
//sys	Chown(path string, uid int, gid int) (err error)
//sys	Chroot(path string) (err error)
//sys	Close(fd int) (err error)
//sys	closedir(dir uintptr) (err error)
//sys	Dup(fd int) (nfd int, err error)
//sys	Dup2(from int, to int) (err error)
//sys	Exchangedata(path1 string, path2 string, options int) (err error)
//sys	Fchdir(fd int) (err error)
//sys	Fchflags(fd int, flags int) (err error)
//sys	Fchmod(fd int, mode uint32) (err error)
//sys	Fchown(fd int, uid int, gid int) (err error)
//sys	Flock(fd int, how int) (err error)
//sys	Fpathconf(fd int, name int) (val int, err error)
//sys	Fsync(fd int) (err error)
//  Fsync is not called for os.File.Sync(). Please see internal/poll/fd_fsync_darwin.go
//sys	Ftruncate(fd int, length int64) (err error)
//sys	Getdtablesize() (size int)
//sysnb	Getegid() (egid int)
//sysnb	Geteuid() (uid int)
//sysnb	Getgid() (gid int)
//sysnb	Getpgid(pid int) (pgid int, err error)
//sysnb	Getpgrp() (pgrp int)
//sysnb	Getpid() (pid int)
//sysnb	Getppid() (ppid int)
//sys	Getpriority(which int, who int) (prio int, err error)
//sysnb	Getrlimit(which int, lim *Rlimit) (err error)
//sysnb	Getrusage(who int, rusage *Rusage) (err error)
//sysnb	Getsid(pid int) (sid int, err error)
//sysnb	Getuid() (uid int)
//sysnb	Issetugid() (tainted bool)
//sys	Kqueue() (fd int, err error)
//sys	Lchown(path string, uid int, gid int) (err error)
//sys	Link(path string, link string) (err error)
//sys	Listen(s int, backlog int) (err error)
//sys	Mkdir(path string, mode uint32) (err error)
//sys	Mkfifo(path string, mode uint32) (err error)
//sys	Mknod(path string, mode uint32, dev int) (err error)
//sys	Mlock(b []byte) (err error)
//sys	Mlockall(flags int) (err error)
//sys	Mprotect(b []byte, prot int) (err error)
//sys	Munlock(b []byte) (err error)
//sys	Munlockall() (err error)
//sys	Open(path string, mode int, perm uint32) (fd int, err error)
//sys	Pathconf(path string, name int) (val int, err error)
//sys	Pread(fd int, p []byte, offset int64) (n int, err error)
//sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
//sys	read(fd int, p []byte) (n int, err error)
//sys	readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
//sys	Readlink(path string, buf []byte) (n int, err error)
//sys	Rename(from string, to string) (err error)
//sys	Revoke(path string) (err error)
//sys	Rmdir(path string) (err error)
//sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_lseek
//sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
//sys	Setegid(egid int) (err error)
//sysnb	Seteuid(euid int) (err error)
//sysnb	Setgid(gid int) (err error)
//sys	Setlogin(name string) (err error)
//sysnb	Setpgid(pid int, pgid int) (err error)
//sys	Setpriority(which int, who int, prio int) (err error)
//sys	Setprivexec(flag int) (err error)
//sysnb	Setregid(rgid int, egid int) (err error)
//sysnb	Setreuid(ruid int, euid int) (err error)
//sysnb	Setrlimit(which int, lim *Rlimit) (err error)
//sysnb	Setsid() (pid int, err error)
//sysnb	Settimeofday(tp *Timeval) (err error)
//sysnb	Setuid(uid int) (err error)
//sys	Symlink(path string, link string) (err error)
//sys	Sync() (err error)
//sys	Truncate(path string, length int64) (err error)
//sys	Umask(newmask int) (oldmask int)
//sys	Undelete(path string) (err error)
//sys	Unlink(path string) (err error)
//sys	Unmount(path string, flags int) (err error)
//sys	write(fd int, p []byte) (n int, err error)
//sys	writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
//sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
//sys   munmap(addr uintptr, length uintptr) (err error)
//sysnb fork() (pid int, err error)
//sysnb ioctl(fd int, req int, arg int) (err error)
//sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
//sysnb execve(path *byte, argv **byte, envp **byte) (err error)
//sysnb exit(res int) (err error)
//sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
//sys	fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
//sys   unlinkat(fd int, path string, flags int) (err error)
//sys   openat(fd int, path string, flags int, perm uint32) (fdret int, err error)
//sys	getcwd(buf []byte) (n int, err error)

func () {
	execveDarwin = execve
}

func ( int) ( uintptr,  error) {
	, ,  := syscallPtr(funcPC(libc_fdopendir_trampoline), uintptr(), 0, 0)
	 = uintptr()
	if  != 0 {
		 = errnoErr()
	}
	return
}

func ()

//go:linkname libc_fdopendir libc_fdopendir
//go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"

func ( int,  *byte,  int) ( int,  error) {
	, ,  := syscall(funcPC(libc_read_trampoline), uintptr(), uintptr(unsafe.Pointer()), uintptr())
	 = int()
	if  != 0 {
		 = errnoErr()
	}
	return
}

func ( int,  *byte,  int) ( int,  error) {
	, ,  := syscall(funcPC(libc_write_trampoline), uintptr(), uintptr(unsafe.Pointer()), uintptr())
	 = int()
	if  != 0 {
		 = errnoErr()
	}
	return
}

func ( int,  []byte,  *uintptr) ( int,  error) {
	// Simulate Getdirentries using fdopendir/readdir_r/closedir.
	// We store the number of entries to skip in the seek
	// offset of fd. See issue #31368.
	// It's not the full required semantics, but should handle the case
	// of calling Getdirentries or ReadDirent repeatedly.
	// It won't handle assigning the results of lseek to *basep, or handle
	// the directory being edited underfoot.
	,  := Seek(, 0, 1 /* SEEK_CUR */)
	if  != nil {
		return 0, 
	}

	// We need to duplicate the incoming file descriptor
	// because the caller expects to retain control of it, but
	// fdopendir expects to take control of its argument.
	// Just Dup'ing the file descriptor is not enough, as the
	// result shares underlying state. Use openat to make a really
	// new file descriptor referring to the same directory.
	,  := openat(, ".", O_RDONLY, 0)
	if  != nil {
		return 0, 
	}
	,  := fdopendir()
	if  != nil {
		Close()
		return 0, 
	}
	defer closedir()

	var  int64
	for {
		var  Dirent
		var  *Dirent
		 := readdir_r(, &, &)
		if  != 0 {
			return , errnoErr()
		}
		if  == nil {
			break
		}
		if  > 0 {
			--
			++
			continue
		}
		 := int(.Reclen)
		if  > len() {
			// Not enough room. Return for now.
			// The counter will let us know where we should start up again.
			// Note: this strategy for suspending in the middle and
			// restarting is O(n^2) in the length of the directory. Oh well.
			break
		}
		// Copy entry into return buffer.
		 := struct {
			 unsafe.Pointer
			 int
			 int
		}{: unsafe.Pointer(&), : , : }
		copy(, *(*[]byte)(unsafe.Pointer(&)))
		 = [:]
		 += 
		++
	}
	// Set the seek offset of the input fd to record
	// how many files we've already returned.
	_,  = Seek(, , 0 /* SEEK_SET */)
	if  != nil {
		return , 
	}

	return , nil
}

// Implemented in the runtime package (runtime/sys_darwin.go)
func (, , ,  uintptr) (,  uintptr,  Errno)
func (, , , , , ,  uintptr) (,  uintptr,  Errno)
func (, , , , , ,  uintptr) (,  uintptr,  Errno)
func (, , ,  uintptr) (,  uintptr,  Errno)
func (, , , , , ,  uintptr) (,  uintptr,  Errno)
func (, , ,  uintptr) (,  uintptr,  Errno)

// Find the entry point for f. See comments in runtime/proc.go for the
// function of the same name.
//go:nosplit
func ( func()) uintptr {
	return **(**uintptr)(unsafe.Pointer(&))
}