diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 6ccfd5fa..6c983d98 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -18,52 +18,148 @@
along with the Sortix C Library. If not, see .
unistd.h
- The header defines miscellaneous symbolic constants and types,
- and declares miscellaneous functions.
+ Standard symbolic constants and types.
*******************************************************************************/
-/* TODO: POSIX-1.2008 compliance is only partial */
-
-#ifndef _UNISTD_H
-#define _UNISTD_H 1
+#ifndef INCLUDE_UNISTD_H
+#define INCLUDE_UNISTD_H
#include
#include
#include <__/stdint.h>
-#if __USE_SORTIX
-#include
-#include
-#include
-#include
-__BEGIN_DECLS
-#ifndef __time_t_defined
-#define __time_t_defined
-typedef __time_t time_t;
-#endif
-__END_DECLS
-#include
-#include
-#endif
#include
#include
+#if __USE_SORTIX
+#include
+#include
+#include
+#endif
+
__BEGIN_DECLS
-/* Currently just say we support the newest POSIX. */
-/* TODO: Support the newest POSIX. */
-#define _POSIX_VERSION 200809L
-#define _POSIX2_VERSION 200809L
+/* If a POSIX revision was already decided by feature macros. */
+#if __USE_POSIX
+#define _POSIX_VERSION __USE_POSIX /* C bindings */
+#define _POSIX2_VERSION __USE_POSIX /* Shell utilities. */
-/* Currently just say we support the newest X/Open */
-/* TODO: Support the newest X/Open */
-#define _XOPEN_VERSION 700
+/* The native API is based on POSIX 2008. */
+#elif __USE_SORTIX
+#define _POSIX_VERSION 200809L /* C bindings */
+#define _POSIX2_VERSION 200809L /* Shell utilities. */
-/* TODO: _POSIX_*, _POSIX2_*, _XOPEN_* is missing here. */
+/* That's odd. This is a POSIX header, but the POSIX API is not visible. The
+ best option is probably to just say we are the 1990 POSIX standard, since it
+ is not protected by feature macros in this header. */
+#else
+#define _POSIX_VERSION 199009L /* C bindings */
+#define _POSIX2_VERSION 199209L /* Shell utilities. */
+#endif
-/* TODO: _POSIX_*, _POSIX2_* is missing here. */
+/* If an X/OPEN revision was already decided by feature macros.
+ TODO: Sortix refuses to implement stupid parts of the POSIX; the XSI option
+ is usually a great clue something is stupid. */
+#if 600 < __USE_XOPEN
+#define _XOPEN_VERSION __USE_XOPEN
+#elif 500 <= __USE_XOPEN
+#define _XOPEN_VERSION 500
+#else
+#define _XOPEN_VERSION 4
+#endif
+
+/* #define _POSIX_ADVISORY_INFO 200809L
+ TODO: Uncomment when posix_fadvise(), posix_fallocate(), posix_madvise(),
+ posix_memalign() has been added. */
+#define _POSIX_ASYNCHRONOUS_IO 200809L
+#define _POSIX_BARRIERS 200809L
+/* TODO: _POSIX_CHOWN_RESTRICTED - Decide when security policies are implemented. */
+#define _POSIX_CLOCK_SELECTION 200809L
+#define _POSIX_CPUTIME 200809L
+#define _POSIX_FSYNC 200809L
+#define _POSIX_IPV6 200809L
+#define _POSIX_JOB_CONTROL
+/*TODO: _POSIX_MEMLOCK - Research what this is. */
+/*TODO: _POSIX_MEMLOCK_RANGE - Research what this is. */
+#define _POSIX_MEMORY_PROTECTION 200809L
+/*TODO: _POSIX_MESSAGE_PASSING - Research what this is. */
+#define _POSIX_MONOTONIC_CLOCK 200809L
+#define _POSIX_NO_TRUNC 1
+/*TODO: _POSIX_PRIORITIZED_IO - Research what this is. */
+/*TODO: _POSIX_PRIORITY_SCHEDULING - Research what this is. */
+/*TODO: _POSIX_RAW_SOCKETS - Research what this is. */
+#define _POSIX_READER_WRITER_LOCKS 200809L
+#define _POSIX_REALTIME_SIGNALS 200809L
+/* #define _POSIX_REGEXP 1
+ TODO: Uncomment when regular expressions are implemented. */
+/* #define _POSIX_SAVED_IDS 1
+ TODO: Uncomment when saved ids are implemented. I forgot if they already are. */
+/* #define _POSIX_SEMAPHORES 200809L
+ TODO: Uncomment when named semaphores are implemented. */
+/*TODO: _POSIX_SHARED_MEMORY_OBJECTS - Research what this is. */
+#define _POSIX_SHELL 1
+/*TODO: _POSIX_SPAWN - Research what this is. */
+#define _POSIX_SPIN_LOCKS 200809L
+/*TODO: _POSIX_SPORADIC_SERVER - Research what this is. */
+/*TODO: _POSIX_SYNCHRONIZED_IO - Research what this is. */
+/*TODO: _POSIX_THREAD_ATTR_STACKADDR - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_ATTR_STACKSIZE - Research what this is, cooperate with libpthread. */
+#define _POSIX_THREAD_CPUTIME 200809L
+/*TODO: _POSIX_THREAD_PRIO_INHERIT - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_PRIO_PROTECT - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_PRIORITY_SCHEDULING - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_PROCESS_SHARED - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_ROBUST_PRIO_INHERIT - Research what this is, cooperate with libpthread. */
+/*TODO: _POSIX_THREAD_ROBUST_PRIO_PROTECT - Research what this is, cooperate with libpthread. */
+#define _POSIX_THREAD_SAFE_FUNCTIONS 200809L
+/*TODO: _POSIX_THREAD_SPORADIC_SERVER - Research what this is, cooperate with libpthread. */
+#define _POSIX_THREADS 200809L
+#define _POSIX_TIMEOUTS 200809L
+#define _POSIX_TIMERS 200809L
+/* TODO: _POSIX_TRACE (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_TRACE_EVENT_FILTER (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_TRACE_INHERIT (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_TRACE_LOG (Obsolescent) - Research what this is. */
+/* TODO: TYPED_MEMORY_OBJECTS - Research what this is. */
+/* TODO: _POSIX_V6_ILP32_OFF32 (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_V6_ILP32_OFFBIG (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_V6_LP64_OFF64 (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_V6_LPBIG_OFFBIG (Obsolescent) - Research what this is. */
+/* TODO: _POSIX_V7_ILP32_OFF32 - Research what this is. */
+/* TODO: _POSIX_V7_ILP32_OFFBIG - Research what this is. */
+/* TODO: _POSIX_V7_LP64_OFF64 - Research what this is. */
+/* TODO: _POSIX_V7_LPBIG_OFFBIG - Research what this is. */
+#define _POSIX2_C_BIND _POSIX2_VERSION
+#define _POSIX2_C_DEV _POSIX2_VERSION
+#define _POSIX2_CHAR_TERM 1
+/* TODO: _POSIX2_FORT_RUN - When fortran becomes supported. */
+/* #define _POSIX2_LOCALEDEF __POSIX2_THIS_VERSION
+ TODO: Uncomment when locales are implemented. */
+/* TODO: _POSIX2_PBS (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_PBS_ACCOUNTING (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_PBS_CHECKPOINT (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_PBS_LOCATE (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_PBS_MESSAGE (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_PBS_TRACK (Obsolescent) - Research what this is. */
+/* TODO: _POSIX2_SW_DEV - Research what this is. (Define to _POSIX2_VERSION) */
+/* #define _POSIX2_UPE 200809L
+ TODO: Uncomment when bg, ex, fc, fg, jobs, more, talk, vi are implemented. */
+/* TODO: _XOPEN_CRYPT - Research what this is. */
+#define _XOPEN_ENH_I18N 1
+#define _XOPEN_REALTIME 1
+#define _XOPEN_REALTIME 1
+/* TODO: _XOPEN_STREAMS (Obsolescent) - Probably don't want to support this. */
+/* TODO: _XOPEN_UNIX - Decide whether we actually support this (probably not),
+ but also whether this header should lie. */
+/* TODO: _XOPEN_UUCP - Research what this is. */
+
+/* TODO: _POSIX_ASYNC_IO - Research what exactly this is. */
+/* TODO: _POSIX_PRIO_IO - Research what exactly this is. */
+/* TODO: _POSIX_SYNC_IO - Research what exactly this is. */
+/* TODO: _POSIX_TIMESTAMP_RESOLUTION - Research what exactly this is. */
+/* TODO: _POSIX2_SYMLINKS - Research what exactly this is. */
#ifndef NULL
#define __need_NULL
@@ -102,7 +198,11 @@ __BEGIN_DECLS
#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 29 /* obsolescent */
#define _CS_V6_ENV 30 /* obsolescent */
-/* TODO: F_* is missing here. */
+/* Sortix will not support POSIX advisory locks and doesn't declare:
+ F_LOCK
+ F_TEST
+ F_TLOCK
+ F_UNLOCK */
#define _PC_2_SYMLINKS 1
#define _PC_ALLOC_SIZE_MIN 2
@@ -256,7 +356,7 @@ __BEGIN_DECLS
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
-/* TODO: _POSIX_VDISABLE is missing here. */
+#define _POSIX_VDISABLE '\0'
#ifndef __size_t_defined
#define __size_t_defined
@@ -294,44 +394,30 @@ typedef __pid_t pid_t;
typedef __useconds_t useconds_t;
#endif
+/* TODO: intptr_t is not declared because doesn't allow other headers
+ to define some, but not all, of the fixed width types. Additionally,
+ intptr_t was only added for the sake of sbrk(), but that was removed in
+ POSIX 2001. The legacy sbrk() system call left supported by the kernel
+ can do with __uintptr for now, as it will be removed soon enough. */
+
+/* Somehow programs are required to declare environ themselves according to
+ the POSIX specification. */
+#if __USE_SORTIX
extern char** environ;
#if defined(__is_sortix_libc)
extern char** __environ_malloced;
extern size_t __environ_used;
extern size_t __environ_length;
#endif
-
-/* TODO: These are not implemented in sortix libc yet. */
-#if 0
-char* crypt(const char*, const char*);
-char* ctermid(char*);
-void encrypt(char [64], int);
-int fdatasync(int);
-int fexecve(int, char* const [], char* const []);
-long fpathconf(int, int);
-int getgroups(int, gid_t []);
-
-pid_t getpgrp(void);
-pid_t getsid(pid_t);
-int nice(int);
-int pause(void);
-int setregid(gid_t, gid_t);
-int setreuid(uid_t, uid_t);
-pid_t setsid(void);
-void swab(const void* __restrict, void* __restrict, ssize_t);
-void sync(void);
#endif
int access(const char*, int);
unsigned alarm(unsigned);
int chdir(const char*);
int chown(const char*, uid_t, gid_t);
-int chroot(const char*);
int close(int);
-size_t confstr(int, char*, size_t);
-int dup2(int, int);
-int dup3(int, int, int);
int dup(int);
+int dup2(int, int);
void _exit(int) __attribute__ ((noreturn));
int execl(const char*, const char*, ...);
int execle(const char*, const char*, ...);
@@ -340,78 +426,44 @@ int execv(const char*, char* const []);
int execve(const char*, char* const [], char* const []);
int execvp(const char*, char* const []);
pid_t fork(void);
-int faccessat(int, const char*, int, int);
-int fchdir(int);
-int fchdirat(int, const char*);
-int fchown(int, uid_t, gid_t);
-int fchownat(int, const char*, uid_t, gid_t, int);
-int fchroot(int);
-int fchrootat(int, const char*);
+/* TODO: long fpathconf(int, int); */
int fsync(int);
-int ftruncate(int, off_t);
char* getcwd(char*, size_t);
-char* get_current_dir_name(void);
-int getdomainname(char*, size_t);
gid_t getegid(void);
uid_t geteuid(void);
-int gethostname(char*, size_t);
+gid_t getgid(void);
+/* TODO: int getgroups(int, gid_t []); */
char* getlogin(void);
-int getlogin_r(char*, size_t);
-size_t getpagesize(void);
pid_t getpgid(pid_t);
+/* TODO: pid_t getpgrp(void); */
pid_t getpid(void);
pid_t getppid(void);
uid_t getuid(void);
-gid_t getgid(void);
int isatty(int);
-int lchown(const char*, uid_t, gid_t);
int link(const char*, const char*);
-int linkat(int, const char*, int, const char*, int);
+/* lockf will not be implemented. */
off_t lseek(int, off_t, int);
long pathconf(const char*, int);
+/* TODO: int pause(void); */
int pipe(int [2]);
-int pipe2(int [2], int);
-ssize_t pread(int, void*, size_t, off_t);
-ssize_t pwrite(int, const void*, size_t, off_t);
-ssize_t readlink(const char* __restrict, char* __restrict, size_t);
-ssize_t readlinkat(int, const char* __restrict, char* __restrict, size_t);
ssize_t read(int, void*, size_t);
int rmdir(const char*);
-void* sbrk(__intptr_t increment);
-int setegid(gid_t);
-int seteuid(uid_t);
int setgid(gid_t);
int setpgid(pid_t, pid_t);
+/* TODO: pid_t setsid(void); */
int setuid(uid_t);
unsigned sleep(unsigned);
-int symlink(const char*, const char*);
-int symlinkat(const char*, int, const char*);
long sysconf(int);
pid_t tcgetpgrp(int);
int tcsetpgrp(int, pid_t);
-int truncate(const char*, off_t);
-int truncateat(int dirfd, const char*, off_t);
char* ttyname(int);
int ttyname_r(int, char*, size_t);
-int usleep(useconds_t useconds);
-int unlinkat(int, const char*, int);
int unlink(const char*);
ssize_t write(int, const void*, size_t);
-#if __USE_SORTIX
-int alarmns(const struct timespec* delay, struct timespec* odelay);
-int execvpe(const char*, char* const [], char* const []);
-int exit_thread(int, int, const struct exit_thread*);
-int memstat(size_t* memused, size_t* memtotal);
-int mkpartition(int fd, off_t start, off_t length);
-pid_t sfork(int flags);
-pid_t tfork(int flags, struct tfork* regs);
-size_t writeall(int fd, const void* buf, size_t count);
-size_t writeleast(int fd, const void* buf, size_t least, size_t max);
-#endif
-
+#if __USE_SORTIX || 199209L <= __USE_POSIX
+size_t confstr(int, char*, size_t);
/* For compatibility with POSIX, declare getopt(3) here. */
-#if __USE_POSIX
/* These declarations are repeated in . */
#ifndef __getopt_unistd_shared_declared
#define __getopt_unistd_shared_declared
@@ -424,6 +476,105 @@ int getopt(int, char* const*, const char*);
#endif
#endif
+#if __USE_XOPEN
+/* TODO: char* crypt(const char*, const char*); */
+/* TODO: void encrypt(char [64], int); */
+/* gethostid will not be implemented */
+/* TODO: int nice(int); */
+/* setpgrp will not be implemented. */
+/* TODO: void swab(const void* __restrict, void* __restrict, ssize_t); */
+/* TODO: void sync(void); */
+#endif
+
+#if __USE_SORTIX || 420 <= __USE_XOPEN
+/* TODO: int setregid(gid_t, gid_t); (XSI option) */
+/* TODO: int setreuid(uid_t, uid_t); (XSI option) */
+#endif
+
+#if __USE_SORTIX || 199309L <= __USE_POSIX || 420 <= __USE_XOPEN
+int getlogin_r(char*, size_t);
+#endif
+
+#if __USE_SORTIX || 200112L <= __USE_POSIX || 420 <= __USE_XOPEN
+int ftruncate(int, off_t);
+ssize_t readlink(const char* __restrict, char* __restrict, size_t);
+int symlink(const char*, const char*);
+#endif
+
+#if __USE_SORTIX || 200809L <= __USE_POSIX || 420 <= __USE_XOPEN
+int fchdir(int);
+int fchown(int, uid_t, gid_t);
+/* TODO: pid_t getsid(void); */
+int lchown(const char*, uid_t, gid_t);
+int truncate(const char*, off_t);
+#endif
+
+/* TODO: This feature macro seems wrong, verify historically. */
+#if __USE_SORTIX || 199309L <= __USE_POSIX || 500 <= __USE_XOPEN
+/* TODO: fdatasync (SIO option) */
+#endif
+
+/* Functions from POSIX 1995. */
+#if __USE_SORTIX || 199506L <= __USE_POSIX
+int getlogin_r(char*, size_t);
+#endif
+
+#if __USE_SORTIX || 200112L <= __USE_POSIX || 500 <= __USE_XOPEN
+int gethostname(char*, size_t);
+#endif
+
+#if __USE_SORTIX || 200809L <= __USE_POSIX || 500 <= __USE_XOPEN
+ssize_t pread(int, void*, size_t, off_t);
+ssize_t pwrite(int, const void*, size_t, off_t);
+#endif
+
+/* Functions from POSIX 2001. */
+#if __USE_SORTIX || 200112L <= __USE_POSIX
+int setegid(gid_t);
+int seteuid(uid_t);
+#endif
+
+/* Functions from POSIX 2008. */
+#if __USE_SORTIX || 200809L <= __USE_POSIX
+int faccessat(int, const char*, int, int);
+int fchownat(int, const char*, uid_t, gid_t, int);
+/* TODO: int fexecve(int, char* const [], char* const []); */
+int linkat(int, const char*, int, const char*, int);
+ssize_t readlinkat(int, const char* __restrict, char* __restrict, size_t);
+int symlinkat(const char*, int, const char*);
+int unlinkat(int, const char*, int);
+#endif
+
+#if __USE_SORTIX || !(200112L <= __USE_POSIX || 600 <= __USE_XOPEN)
+size_t getpagesize(void);
+#endif
+
+/* Functions copied from elsewhere. */
+#if __USE_SORTIX
+int chroot(const char*);
+int dup3(int, int, int);
+int execvpe(const char*, char* const [], char* const []);
+char* get_current_dir_name(void);
+int getdomainname(char*, size_t);
+int pipe2(int [2], int);
+void* sbrk(__intptr_t increment);
+int usleep(useconds_t useconds);
+#endif
+
+/* Functions that are Sortix extensions. */
+#if __USE_SORTIX
+int alarmns(const struct timespec* delay, struct timespec* odelay);
+int exit_thread(int, int, const struct exit_thread*);
+int fchdirat(int, const char*);
+int fchroot(int);
+int fchrootat(int, const char*);
+int memstat(size_t* memused, size_t* memtotal);
+int mkpartition(int fd, off_t start, off_t length);
+pid_t sfork(int flags);
+pid_t tfork(int flags, struct tfork* regs);
+int truncateat(int dirfd, const char*, off_t);
+#endif
+
__END_DECLS
#endif