---@meta unix

--------------------------------------------------------------------------------
-- lunix C module
--------------------------------------------------------------------------------

---@alias unix_fn fun(...:any):any

---@class unix_sockaddr*
local unix_sockaddr = {}

---@class unix_sigset*
local unix_sigset = {}

---@class unix_sighandler*
local unix_sighandler = {}

---@class unix_regex*
local unix_regex = {}

---@class unix_addrinfo*
---@field family integer
---@field socktype integer
---@field protocol integer
---@field addr unix_sockaddr*
---@field canonname? string
---@field port? integer|string

---@class unix_ifaddr*
---@field name string
---@field flags integer
---@field addr? unix_sockaddr*
---@field netmask? unix_sockaddr*
---@field broadaddr? unix_sockaddr*
---@field dstaddr? unix_sockaddr*
---@field data? any
---@field family? integer
---@field prefixlen? integer

---@class unix_group*
---@field name string
---@field passwd? string
---@field gid integer

---@class unix_passwd*
---@field name string
---@field passwd? string
---@field uid integer
---@field gid integer
---@field dir? string
---@field shell? string
---@field gecos? string

---@class unix_dir_entry*
---@field name string
---@field ino integer
---@field type integer

---@class unix_dir*
local unix_dir = {}

--- Read a directory entry.
---@return unix_dir_entry*? entry directory entry
function unix_dir:read() end

--- Returns an iterator of directory entries.
---@return fun(dir:unix_dir*):unix_dir_entry*? iter iterative function
---@return unix_dir* dir directory object
function unix_dir:files() end

--- Resets directory read position.
function unix_dir:rewind() end

--- Close the directory.
---@return boolean? ok true on success
function unix_dir:close() end

---@class unix_flock*
---@field type integer
---@field whence integer
---@field start integer
---@field len integer
---@field pid integer

---@class unix_pollfd*
---@field fd integer
---@field events integer
---@field revents? integer

---@class unix_sigaction*
---@field handler unix_sighandler*
---@field mask? unix_sigset*
---@field flags? integer

---@class unix_stat*
---@field dev integer
---@field ino integer
---@field mode integer
---@field nlink integer
---@field uid integer
---@field gid integer
---@field rdev integer
---@field size integer
---@field atime integer
---@field mtime integer
---@field ctime integer
---@field blksize integer
---@field blocks integer

---@class unix_uname*
---@field sysname string
---@field nodename string
---@field release string
---@field version string
---@field machine string

---@class unix_rusage*
---@field utime number
---@field stime number

---@class unix*
---@field environ table<string,string>
---@field SIG_DFL unix_sighandler*
---@field SIG_ERR unix_sighandler*
---@field SIG_IGN unix_sighandler*
---@field accept unix_fn
---@field access unix_fn
---@field alarm unix_fn
---@field arc4random unix_fn
---@field arc4random_buf unix_fn
---@field arc4random_stir unix_fn
---@field arc4random_uniform unix_fn
---@field bind unix_fn
---@field bitand unix_fn
---@field bitor unix_fn
---@field chdir unix_fn
---@field chmod unix_fn
---@field chown unix_fn
---@field chroot unix_fn
---@field clearerr unix_fn
---@field clock_gettime unix_fn
---@field close unix_fn
---@field closedir unix_fn
---@field closelog unix_fn
---@field compl unix_fn
---@field connect unix_fn
---@field dup unix_fn
---@field dup2 unix_fn
---@field dup3 unix_fn
---@field execve unix_fn
---@field execl unix_fn
---@field execlp unix_fn
---@field execvp unix_fn
---@field _exit unix_fn
---@field exit unix_fn
---@field faccessat unix_fn
---@field fchmod unix_fn
---@field fchown unix_fn
---@field fcntl unix_fn
---@field fdatasync unix_fn
---@field fdopen unix_fn
---@field fdopendir unix_fn
---@field fdup unix_fn
---@field feof unix_fn
---@field ferror unix_fn
---@field fgetc unix_fn
---@field fileno unix_fn
---@field flockfile unix_fn
---@field fnmatch unix_fn
---@field fstat fun(fd:integer):unix_stat*?
---@field fstatat unix_fn
---@field fsync unix_fn
---@field ftrylockfile unix_fn
---@field funlockfile unix_fn
---@field fopen unix_fn
---@field fopenat unix_fn
---@field fpathconf unix_fn
---@field fpipe unix_fn
---@field fork unix_fn
---@field gai_strerror unix_fn
---@field getaddrinfo fun(host?:string, service?:string|integer, hints?:table):unix_addrinfo[]?
---@field getc unix_fn
---@field getcwd fun():string?
---@field getegid fun():integer
---@field geteuid fun():integer
---@field getenv fun(name:string):string?
---@field getmode unix_fn
---@field getgid fun():integer
---@field getgrnam fun(name:string):unix_group?
---@field getgrgid fun(gid:integer):unix_group?
---@field getgroups fun():integer[]?
---@field gethostname fun():string?
---@field getifaddrs fun():unix_ifaddr[]?
---@field getnameinfo unix_fn
---@field getopt unix_fn
---@field getpeername unix_fn
---@field getpgid unix_fn
---@field getpgrp fun():integer
---@field getpid fun():integer
---@field getppid fun():integer
---@field getprogname fun():string?
---@field getpwnam fun(name:string):unix_passwd?
---@field getpwuid fun(uid:integer):unix_passwd?
---@field getrlimit unix_fn
---@field getrusage fun(who?:integer):unix_rusage?
---@field getsockname unix_fn
---@field gettimeofday fun():number
---@field getuid fun():integer
---@field grantpt unix_fn
---@field ioctl unix_fn
---@field isatty unix_fn
---@field issetugid unix_fn
---@field kill unix_fn
---@field lchown unix_fn
---@field link unix_fn
---@field listen unix_fn
---@field lockf unix_fn
---@field LOG_MASK fun(priority:integer):integer
---@field LOG_UPTO fun(priority:integer):integer
---@field lseek unix_fn
---@field lstat fun(path:string):unix_stat*?
---@field mkdir unix_fn
---@field mkdirat unix_fn
---@field mkfifo unix_fn
---@field mkfifoat unix_fn
---@field mkpath unix_fn
---@field open unix_fn
---@field openat unix_fn
---@field opendir fun(path:string):unix_dir?
---@field openlog unix_fn
---@field pathconf unix_fn
---@field pipe unix_fn
---@field poll unix_fn
---@field posix_fadvise unix_fn
---@field posix_fallocate unix_fn
---@field posix_openpt unix_fn
---@field posix_fopenpt unix_fn
---@field pread unix_fn
---@field ptsname unix_fn
---@field pwrite unix_fn
---@field raise unix_fn
---@field read unix_fn
---@field readdir fun(dir:unix_dir*):unix_dir_entry?
---@field readlink unix_fn
---@field readlinkat unix_fn
---@field realpath unix_fn
---@field recv unix_fn
---@field recvfrom unix_fn
---@field recvfromto unix_fn
---@field regcomp unix_fn
---@field regerror unix_fn
---@field regexec unix_fn
---@field regfree unix_fn
---@field rename unix_fn
---@field renameat unix_fn
---@field rewinddir unix_fn
---@field rmdir unix_fn
---@field S_ISBLK fun(mode:integer):boolean
---@field S_ISCHR fun(mode:integer):boolean
---@field S_ISDIR fun(mode:integer):boolean
---@field S_ISFIFO fun(mode:integer):boolean
---@field S_ISREG fun(mode:integer):boolean
---@field S_ISLNK fun(mode:integer):boolean
---@field S_ISSOCK fun(mode:integer):boolean
---@field shutdown unix_fn
---@field send unix_fn
---@field sendto unix_fn
---@field sendtofrom unix_fn
---@field setegid unix_fn
---@field setenv unix_fn
---@field seteuid unix_fn
---@field setgid unix_fn
---@field setgroups unix_fn
---@field setlocale unix_fn
---@field setlogmask unix_fn
---@field setpgid unix_fn
---@field setrlimit unix_fn
---@field setsockopt unix_fn
---@field setsid fun():integer?
---@field setuid unix_fn
---@field sigaction fun(signo:integer, action?:unix_sigaction*|nil, old?:boolean):boolean|unix_sigaction*
---@field sigfillset fun(set?:unix_sigset*):unix_sigset*
---@field sigemptyset fun(set?:unix_sigset*):unix_sigset*
---@field sigaddset fun(set:unix_sigset*, signo:integer):unix_sigset*
---@field sigdelset fun(set:unix_sigset*, signo:integer):unix_sigset*
---@field sigismember fun(set:unix_sigset*, signo:integer):boolean
---@field sigprocmask unix_fn
---@field sigtimedwait unix_fn
---@field sigwait unix_fn
---@field sleep unix_fn
---@field socket unix_fn
---@field socketpair unix_fn
---@field stat fun(path:string):unix_stat*?
---@field strerror fun(errno:integer):string
---@field strsignal fun(signo:integer):string?
---@field symlink unix_fn
---@field symlinkat unix_fn
---@field sysconf unix_fn
---@field syslog unix_fn
---@field tcgetpgrp unix_fn
---@field tcgetsid unix_fn
---@field tcsetpgrp unix_fn
---@field timegm unix_fn
---@field truncate unix_fn
---@field tzset unix_fn
---@field umask unix_fn
---@field uname fun():unix_uname?
---@field unlink unix_fn
---@field unlinkat unix_fn
---@field unlockpt unix_fn
---@field unsetenv unix_fn
---@field wait unix_fn
---@field waitpid unix_fn
---@field write unix_fn
---@field xor unix_fn
---@field AF_UNSPEC integer
---@field AF_INET6 integer
---@field SHUT_RD integer
---@field SHUT_RDWR integer
---@field SHUT_WR integer
---@field SO_ACCEPTCONN integer
---@field SO_BROADCAST integer
---@field SO_DEBUG integer
---@field SO_DONTROUTE integer
---@field SO_ERROR integer
---@field SO_KEEPALIVE integer
---@field SO_LINGER integer
---@field SO_NOSIGPIPE integer
---@field SO_OOBINLINE integer
---@field SO_RCVBUF integer
---@field SO_RCVLOWAT integer
---@field SO_RCVTIMEO integer
---@field SO_REUSEADDR integer
---@field SO_SNDBUF integer
---@field SO_SNDLOWAT integer
---@field SO_SNDTIMEO integer
---@field SO_TYPE integer
---@field SOMAXCONN integer
---@field SOL_SOCKET integer
---@field SOCK_DGRAM integer
---@field SOCK_RAW integer
---@field SOCK_SEQPACKET integer
---@field SOCK_CLOEXEC integer
---@field SOCK_NONBLOCK integer
---@field SOCK_NOSIGPIPE integer
---@field IPPROTO_IP integer
---@field IPPROTO_TCP integer
---@field IPPROTO_ICMP integer
---@field IPPROTO_RAW integer
---@field IP_PKTINFO integer
---@field IP_RECVDSTADDR integer
---@field IP_SENDSRCADDR integer
---@field IP_TTL integer
---@field IPV6_PKTINFO integer
---@field IPV6_RECVPKTINFO integer
---@field IPV6_V6ONLY integer
---@field AI_PASSIVE integer
---@field AI_NUMERICHOST integer
---@field AI_ADDRCONFIG integer
---@field AI_V4MAPPED integer
---@field AI_ALL integer
---@field EAI_AGAIN integer
---@field EAI_FAMILY integer
---@field EAI_NONAME integer
---@field EAI_SOCKTYPE integer
---@field EAI_OVERFLOW integer
---@field MAP_ANON integer
---@field MAP_ANONYMOUS integer
---@field MAP_FILE integer
---@field MAP_FIXED integer
---@field MAP_PRIVATE integer
---@field MAP_SHARED integer
---@field MCL_CURRENT integer
---@field MCL_FUTURE integer
---@field PROT_EXEC integer
---@field PROT_NONE integer
---@field PROT_READ integer
---@field PROT_WRITE integer
---@field MSG_EOR integer
---@field MSG_NOSIGNAL integer
---@field MSG_DONTWAIT integer
---@field MSG_OOB integer
---@field MSG_PEEK integer
---@field MSG_WAITALL integer
---@field NI_NOFQDN integer
---@field NI_NUMERICHOST integer
---@field NI_NAMEREQD integer
---@field NI_NUMERICSERV integer
---@field NI_NUMERICSCOPE integer
---@field NI_DGRAM integer
---@field MAXPATHLEN integer
---@field POLLERR integer
---@field POLLHUP integer
---@field POLLIN integer
---@field POLLOUT integer
---@field POLLNVAL integer
---@field POLLPRI integer
---@field POLLRDBAND integer
---@field POLLRDNORM integer
---@field POLLWRBAND integer
---@field POLLWRNORM integer
---@field EDOM integer
---@field E2BIG integer
---@field EACCES integer
---@field EADDRINUSE integer
---@field EADDRNOTAVAIL integer
---@field EAFNOSUPPORT integer
---@field EAGAIN integer
---@field EALREADY integer
---@field EBADF integer
---@field EBADMSG integer
---@field EBUSY integer
---@field ECANCELED integer
---@field ECHILD integer
---@field ECONNABORTED integer
---@field ECONNREFUSED integer
---@field ECONNRESET integer
---@field EDEADLK integer
---@field EDESTADDRREQ integer
---@field EDQUOT integer
---@field EEXIST integer
---@field EFAULT integer
---@field EFBIG integer
---@field EHOSTUNREACH integer
---@field EIDRM integer
---@field EINPROGRESS integer
---@field EINTR integer
---@field EINVAL integer
---@field EIO integer
---@field EISCONN integer
---@field EISDIR integer
---@field ELOOP integer
---@field EMFILE integer
---@field EMLINK integer
---@field EMSGSIZE integer
---@field EMULTIHOP integer
---@field ENAMETOOLONG integer
---@field ENETDOWN integer
---@field ENETRESET integer
---@field ENETUNREACH integer
---@field ENFILE integer
---@field ENOBUFS integer
---@field ENODATA integer
---@field ENODEV integer
---@field ENOENT integer
---@field ENOEXEC integer
---@field ENOLCK integer
---@field ENOLINK integer
---@field ENOMEM integer
---@field ENOMSG integer
---@field ENOPROTOOPT integer
---@field ENOSPC integer
---@field ENOSR integer
---@field ENOSTR integer
---@field ENOSYS integer
---@field ENOTCONN integer
---@field ENOTDIR integer
---@field ENOTEMPTY integer
---@field ENOTRECOVERABLE integer
---@field ENOTSOCK integer
---@field ENOTSUP integer
---@field ENOTTY integer
---@field ENXIO integer
---@field EOPNOTSUPP integer
---@field EOVERFLOW integer
---@field EOWNERDEAD integer
---@field EPERM integer
---@field EPIPE integer
---@field EPROTO integer
---@field EPROTONOSUPPORT integer
---@field EPROTOTYPE integer
---@field EROFS integer
---@field ESPIPE integer
---@field ESRCH integer
---@field ESTALE integer
---@field ETIME integer
---@field ETIMEDOUT integer
---@field ETXTBSY integer
---@field EWOULDBLOCK integer
---@field EXDEV integer
---@field FNM_NOMATCH integer
---@field FNM_PATHNAME integer
---@field FNM_PERIOD integer
---@field FNM_NOESCAPE integer
---@field IFF_UP integer
---@field IFF_BROADCAST integer
---@field IFF_DEBUG integer
---@field IFF_LOOPBACK integer
---@field IFF_POINTOPOINT integer
---@field IFF_NOTRAILERS integer
---@field IFF_RUNNING integer
---@field IFF_NOARP integer
---@field IFF_PROMISC integer
---@field IFF_SIMPLEX integer
---@field IFF_MULTICAST integer
---@field WCONTINUED integer
---@field WNOHANG integer
---@field WUNTRACED integer
---@field WSTOPPED integer
---@field REG_EXTENDED integer
---@field REG_ICASE integer
---@field REG_NOSUB integer
---@field REG_NEWLINE integer
---@field REG_NOTBOL integer
---@field REG_NOTEOL integer
---@field REG_BADBR integer
---@field REG_BADPAT integer
---@field REG_BADRPT integer
---@field REG_EBRACE integer
---@field REG_EBRACK integer
---@field REG_ECOLLATE integer
---@field REG_ECTYPE integer
---@field REG_EESCAPE integer
---@field REG_EPAREN integer
---@field REG_ERANGE integer
---@field REG_ESPACE integer
---@field REG_ESUBREG integer
---@field REG_NOMATCH integer
---@field RLIMIT_CORE integer
---@field RLIMIT_DATA integer
---@field RLIMIT_NOFILE integer
---@field RLIMIT_AS integer
---@field RUSAGE_CHILDREN integer
---@field SIGABRT integer
---@field SIGCHLD integer
---@field SIGHUP integer
---@field SIGKILL integer
---@field SIGSEGV integer
---@field SIGTSTP integer
---@field SIGUSR1 integer
---@field SIGURG integer
---@field NSIG integer
---@field SIG_BLOCK integer
---@field SA_NOCLDSTOP integer
---@field SA_RESTART integer
---@field SA_SIGINFO integer
---@field LOG_EMERG integer
---@field LOG_ALERT integer
---@field LOG_CRIT integer
---@field LOG_ERR integer
---@field LOG_WARNING integer
---@field LOG_NOTICE integer
---@field LOG_INFO integer
---@field LOG_DEBUG integer
---@field LOG_USER integer
---@field LOG_LOCAL0 integer
---@field LOG_LOCAL1 integer
---@field LOG_LOCAL2 integer
---@field LOG_LOCAL3 integer
---@field LOG_LOCAL4 integer
---@field LOG_LOCAL5 integer
---@field LOG_LOCAL6 integer
---@field LOG_LOCAL7 integer
---@field LOG_AUDIT integer
---@field LOG_AUTH integer
---@field LOG_AUTHPRIV integer
---@field LOG_CRON integer
---@field LOG_DAEMON integer
---@field LOG_FTP integer
---@field LOG_LPR integer
---@field LOG_MAIL integer
---@field LOG_NEWS integer
---@field LOG_UUCP integer
---@field LOG_PID integer
---@field LOG_CONS integer
---@field LOG_NDELAY integer
---@field LOG_ODELAY integer
---@field LOG_NOWAIT integer
---@field LOG_PERROR integer
---@field AT_EACCESS integer
---@field AT_EMPTY_PATH integer
---@field AT_FDCWD integer
---@field AT_NO_AUTOMOUNT integer
---@field AT_RECURSIVE integer
---@field AT_REMOVEDIR integer
---@field AT_SYMLINK_FOLLOW integer
---@field AT_SYMLINK_NOFOLLOW integer
---@field AT_SYMLINK_NOFOLLOW_ANY integer
---@field F_DUPFD integer
---@field F_DUPFD_CLOEXEC integer
---@field F_DUP2FD integer
---@field F_DUP2FD_CLOEXEC integer
---@field F_GETFD integer
---@field F_GETFL integer
---@field F_GETLK integer
---@field F_GETOWN integer
---@field F_GETPATH integer
---@field F_GETPATH_NOFIRMLINK integer
---@field F_GETNOSIGPIPE integer
---@field F_SETNOSIGPIPE integer
---@field F_CLOSEM integer
---@field F_MAXFD integer
---@field FD_CLOEXEC integer
---@field F_RDLCK integer
---@field SEEK_SET integer
---@field O_ACCMODE integer
---@field O_CREAT integer
---@field O_DIRECTORY integer
---@field O_EXCL integer
---@field O_NOCTTY integer
---@field O_NOFOLLOW integer
---@field O_TRUNC integer
---@field O_TMPFILE integer
---@field O_APPEND integer
---@field O_NONBLOCK integer
---@field O_NDELAY integer
---@field O_SYNC integer
---@field O_DSYNC integer
---@field O_RSYNC integer
---@field O_RDONLY integer
---@field O_EXEC integer
---@field O_SEARCH integer
---@field O_NOSIGPIPE integer
---@field POSIX_FADV_DONTNEED integer
---@field POSIX_FADV_NOREUSE integer
---@field POSIX_FADV_NORMAL integer
---@field POSIX_FADV_RANDOM integer
---@field POSIX_FADV_SEQUENTIAL integer
---@field POSIX_FADV_WILLNEED integer
---@field SIOCATMARK integer
---@field TIOCGSIZE integer
---@field TIOCSSIZE integer
---@field TIOCGWINSZ integer
---@field TIOCSWINSZ integer
---@field TIOCNOTTY integer
---@field TIOCSCTTY integer
---@field LC_ALL integer
---@field LC_MONETARY integer
---@field STDIN_FILENO integer
---@field STDERR_FILENO integer
---@field F_ULOCK integer
---@field F_TLOCK integer
---@field _PC_FILESIZEBITS integer
---@field _PC_NAME_MAX integer
---@field _PC_PATH_MAX integer
---@field _PC_PIPE_BUF integer
---@field _SC_LINE_MAX integer
---@field _SC_OPEN_MAX integer
---@field _SC_PAGE_SIZE integer
---@field R_OK integer
---@field W_OK integer
---@field X_OK integer
---@field F_OK integer
---@field RLIM_INFINITY number
---@field RLIM_SAVED_CUR number
---@field RLIM_SAVED_MAX number
---@type unix*
local M = {}

return M
