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

package libc // import "modernc.org/libc"

import (
	
	

	
	
	
	
	
)

// int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
func ( *TLS,  int32, ,  uintptr) int32 {
	var ,  uintptr
	if  != 0 {
		 := int(unsafe.Sizeof(signal.X__sigaction{}))
		 = .Alloc()
		defer .Free()
		(*signal.X__sigaction)(unsafe.Pointer()).F__sigaction_u.F__sa_handler = (*signal.Sigaction)(unsafe.Pointer()).F__sigaction_u.F__sa_handler
		(*signal.X__sigaction)(unsafe.Pointer()).Fsa_flags = (*signal.Sigaction)(unsafe.Pointer()).Fsa_flags
		Xmemcpy(, +unsafe.Offsetof(signal.X__sigaction{}.Fsa_mask), +unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(signal.Sigset_t(0))))
	}
	if  != 0 {
		panic(todo(""))
	}

	if , ,  := unix.Syscall6(unix.SYS_SIGACTION, uintptr(), , , unsafe.Sizeof(signal.Sigset_t(0)), 0, 0);  != 0 {
		.setErrno()
		return -1
	}

	if  != 0 {
		panic(todo(""))
	}

	return 0
}

// int fcntl(int fd, int cmd, ... /* arg */ );
func ( *TLS, ,  int32,  uintptr) ( int32) {
	var  error
	var  uintptr
	var  int
	switch  {
	case fcntl.F_GETLK, fcntl.F_SETLK:
		 = *(*uintptr)(unsafe.Pointer())
		 = unix.FcntlFlock(uintptr(), int(), (*unix.Flock_t)(unsafe.Pointer()))
	case fcntl.F_GETFL, fcntl.F_FULLFSYNC:
		,  = unix.FcntlInt(uintptr(), int(), 0)
		 = int32()
	case fcntl.F_SETFD, fcntl.F_SETFL:
		 := *(*int32)(unsafe.Pointer())
		_,  = unix.FcntlInt(uintptr(), int(), int())
	default:
		panic(todo("%v: %v %v", origin(1), , ))
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: fd %v cmd %v p %#x: %v FAIL", origin(1), fcntlCmdStr(), , , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %d %s %#x: ok", origin(1), , fcntlCmdStr(), )
	}
	return 
}

// int lstat(const char *pathname, struct stat *statbuf);
func ( *TLS, ,  uintptr) int32 {
	if  := unix.Lstat(GoString(), (*unix.Stat_t)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 0
}

// int stat(const char *pathname, struct stat *statbuf);
func ( *TLS, ,  uintptr) int32 {
	if  := unix.Stat(GoString(), (*unix.Stat_t)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 0
}

// int fstatfs(int fd, struct statfs *buf);
func ( *TLS,  int32,  uintptr) int32 {
	if  := unix.Fstatfs(int(), (*unix.Statfs_t)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: %v: %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %v: ok", origin(1), )
	}
	return 0
}

// int statfs(const char *path, struct statfs *buf);
func ( *TLS,  uintptr,  uintptr) int32 {
	if  := unix.Statfs(GoString(), (*unix.Statfs_t)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 0
}

// int fstat(int fd, struct stat *statbuf);
func ( *TLS,  int32,  uintptr) int32 {
	if  := unix.Fstat(int(), (*unix.Stat_t)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: fd %d: %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: fd %d: ok", origin(1), )
	}
	return 0
}

// off64_t lseek64(int fd, off64_t offset, int whence);
func ( *TLS,  int32,  types.Off_t,  int32) types.Off_t {
	,  := unix.Seek(int(), int64(), int())
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Off_t()
}

// int utime(const char *filename, const struct utimbuf *times);
func ( *TLS, ,  uintptr) int32 {
	var  []unix.Timeval
	if  != 0 {
		 = make([]unix.Timeval, 2)
		[0].Sec = (*utime.Utimbuf)(unsafe.Pointer()).Factime
		[1].Sec = (*utime.Utimbuf)(unsafe.Pointer()).Fmodtime
	}
	if  := unix.Utimes(GoString(), );  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// unsigned int alarm(unsigned int seconds);
func ( *TLS,  uint32) uint32 {
	panic(todo(""))
	// n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
	// if err != 0 {
	// 	panic(todo(""))
	// }

	// return uint32(n)
}

// time_t time(time_t *tloc);
func ( *TLS,  uintptr) types.Time_t {
	panic(todo(""))
	// n := time.Now().UTC().Unix()
	// if tloc != 0 {
	// 	*(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
	// }
	// return types.Time_t(n)
}

// // int getrlimit(int resource, struct rlimit *rlim);
// func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
// 	if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
// 		t.setErrno(err)
// 		return -1
// 	}
//
// 	return 0
// }

// int mkdir(const char *path, mode_t mode);
func ( *TLS,  uintptr,  types.Mode_t) int32 {
	if  := unix.Mkdir(GoString(), uint32());  != nil {
		if dmesgs {
			dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 0
}

// int symlink(const char *target, const char *linkpath);
func ( *TLS, ,  uintptr) int32 {
	if  := unix.Symlink(GoString(), GoString());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int chmod(const char *pathname, mode_t mode)
func ( *TLS,  uintptr,  types.Mode_t) int32 {
	if  := unix.Chmod(GoString(), uint32());  != nil {
		if dmesgs {
			dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q %#o: ok", origin(1), GoString(), )
	}
	return 0
}

// int utimes(const char *filename, const struct timeval times[2]);
func ( *TLS, ,  uintptr) int32 {
	var  []unix.Timeval
	if  != 0 {
		 = make([]unix.Timeval, 2)
		[0] = *(*unix.Timeval)(unsafe.Pointer())
		[1] = *(*unix.Timeval)(unsafe.Pointer( + unsafe.Sizeof(unix.Timeval{})))
	}
	if  := unix.Utimes(GoString(), );  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int unlink(const char *pathname);
func ( *TLS,  uintptr) int32 {
	if  := unix.Unlink(GoString());  != nil {
		if dmesgs {
			dmesg("%v: %q: %v", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int access(const char *pathname, int mode);
func ( *TLS,  uintptr,  int32) int32 {
	if  := unix.Access(GoString(), uint32());  != nil {
		if dmesgs {
			dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q %#o: ok", origin(1), GoString(), )
	}
	return 0
}

// int rename(const char *oldpath, const char *newpath);
func ( *TLS, ,  uintptr) int32 {
	if  := unix.Rename(GoString(), GoString());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int mknod(const char *pathname, mode_t mode, dev_t dev);
func ( *TLS,  uintptr,  types.Mode_t,  types.Dev_t) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int chown(const char *pathname, uid_t owner, gid_t group);
func ( *TLS,  uintptr,  types.Uid_t,  types.Gid_t) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int link(const char *oldpath, const char *newpath);
func ( *TLS, ,  uintptr) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int dup2(int oldfd, int newfd);
func ( *TLS, ,  int32) int32 {
	panic(todo(""))
	// n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return int32(n)
}

// ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
func ( *TLS, ,  uintptr,  types.Size_t) types.Ssize_t {
	var  int
	var  error
	switch {
	case  == 0 ||  == 0:
		,  = unix.Readlink(GoString(), nil)
	default:
		,  = unix.Readlink(GoString(), (*RawMem)(unsafe.Pointer())[::])
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok")
	}
	return types.Ssize_t()
}

// FILE *fopen64(const char *pathname, const char *mode);
func ( *TLS, ,  uintptr) uintptr {
	 := strings.ReplaceAll(GoString(), "b", "")
	var  int
	switch  {
	case "r":
		 = fcntl.O_RDONLY
	case "r+":
		 = fcntl.O_RDWR
	case "w":
		 = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_TRUNC
	case "w+":
		 = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_TRUNC
	case "a":
		 = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_APPEND
	case "a+":
		 = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_APPEND
	default:
		panic()
	}
	,  := unix.Open(GoString(), int(), 0666)
	if  != nil {
		if dmesgs {
			dmesg("%v: %q %q: %v FAIL", origin(1), GoString(), GoString(), )
		}
		.setErrno()
		return 0
	}

	if dmesgs {
		dmesg("%v: %q %q: fd %v", origin(1), GoString(), GoString(), )
	}
	if  := newFile(, int32());  != 0 {
		return 
	}

	panic("OOM")
}