1 /* 2 * Copyright 2004-2015 Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _UNISTD_H_ 6 #define _UNISTD_H_ 7 8 9 #include <null.h> 10 #include <stdint.h> 11 #include <sys/types.h> 12 13 14 /* access modes */ 15 #define R_OK 4 16 #define W_OK 2 17 #define X_OK 1 18 #define F_OK 0 19 20 /* standard file descriptors */ 21 #define STDIN_FILENO 0 22 #define STDOUT_FILENO 1 23 #define STDERR_FILENO 2 24 25 /* function arguments needed by lockf() */ 26 #define F_ULOCK 0 /* unlock locked sections */ 27 #define F_LOCK 1 /* lock a section for exclusive use */ 28 #define F_TLOCK 2 /* test and lock a section for exclusive use */ 29 #define F_TEST 3 /* test a section for locks by other processes */ 30 31 /* POSIX version support */ 32 #define _POSIX_VERSION (199009L) /* TODO: Update! */ 33 34 #define _POSIX_CHOWN_RESTRICTED 1 35 #define _POSIX_JOB_CONTROL 1 36 #define _POSIX_NO_TRUNC 0 37 #define _POSIX_SAVED_IDS 1 38 #define _POSIX_VDISABLE ((unsigned char)-2) 39 /* TODO: Check this! */ 40 /* TODO: Update these to the current POSIX version! Ideally after actually 41 supporting the features. */ 42 #define _POSIX_BARRIERS (200112L) 43 #define _POSIX_SEMAPHORES (200112L) 44 #define _POSIX_THREADS (200112L) 45 #define _POSIX_MAPPED_FILES (200809L) 46 #define _POSIX_THREAD_PROCESS_SHARED (200809L) 47 #define _POSIX_THREAD_ATTR_STACKADDR (200809L) 48 #define _POSIX_THREAD_ATTR_STACKSIZE (200809L) 49 #define _POSIX_THREAD_PRIORITY_SCHEDULING (-1) /* currently unsupported */ 50 #define _POSIX_REALTIME_SIGNALS (200809L) 51 #define _POSIX_MEMORY_PROTECTION (200809L) 52 #define _POSIX_MONOTONIC_CLOCK (200809L) 53 #define _POSIX_TIMERS (200809L) 54 #define _POSIX_CPUTIME (200809L) 55 #define _POSIX_THREAD_CPUTIME (200809L) 56 57 58 /* pathconf() constants */ 59 /* BeOS supported values, do not touch */ 60 #define _PC_CHOWN_RESTRICTED 1 61 #define _PC_MAX_CANON 2 62 #define _PC_MAX_INPUT 3 63 #define _PC_NAME_MAX 4 64 #define _PC_NO_TRUNC 5 65 #define _PC_PATH_MAX 6 66 #define _PC_PIPE_BUF 7 67 #define _PC_VDISABLE 8 68 #define _PC_LINK_MAX 25 69 /* new values */ 70 #define _PC_SYNC_IO 26 71 #define _PC_ASYNC_IO 27 72 #define _PC_PRIO_IO 28 73 #define _PC_SOCK_MAXBUF 29 74 #define _PC_FILESIZEBITS 30 75 #define _PC_REC_INCR_XFER_SIZE 31 76 #define _PC_REC_MAX_XFER_SIZE 32 77 #define _PC_REC_MIN_XFER_SIZE 33 78 #define _PC_REC_XFER_ALIGN 34 79 #define _PC_ALLOC_SIZE_MIN 35 80 #define _PC_SYMLINK_MAX 36 81 #define _PC_2_SYMLINKS 37 82 #define _PC_XATTR_EXISTS 38 83 #define _PC_XATTR_ENABLED 39 84 85 /* sysconf() constants */ 86 /* BeOS supported values, do not touch */ 87 #define _SC_ARG_MAX 15 88 #define _SC_CHILD_MAX 16 89 #define _SC_CLK_TCK 17 90 #define _SC_JOB_CONTROL 18 91 #define _SC_NGROUPS_MAX 19 92 #define _SC_OPEN_MAX 20 93 #define _SC_SAVED_IDS 21 94 #define _SC_STREAM_MAX 22 95 #define _SC_TZNAME_MAX 23 96 #define _SC_VERSION 24 97 /* new values */ 98 #define _SC_GETGR_R_SIZE_MAX 25 99 #define _SC_GETPW_R_SIZE_MAX 26 100 #define _SC_PAGE_SIZE 27 101 #define _SC_PAGESIZE _SC_PAGE_SIZE 102 #define _SC_SEM_NSEMS_MAX 28 103 #define _SC_SEM_VALUE_MAX 29 104 #define _SC_SEMAPHORES 30 105 #define _SC_THREADS 31 106 /* TODO: check */ 107 #define _SC_IOV_MAX 32 108 #define _SC_UIO_MAXIOV _SC_IOV_MAX 109 #define _SC_NPROCESSORS_CONF 34 110 #define _SC_NPROCESSORS_ONLN 35 111 #define _SC_ATEXIT_MAX 37 112 #define _SC_PASS_MAX 39 113 #define _SC_PHYS_PAGES 40 114 #define _SC_AVPHYS_PAGES 41 115 #define _SC_PIPE 42 116 #define _SC_SELECT 43 117 #define _SC_POLL 44 118 #define _SC_MAPPED_FILES 45 119 #define _SC_THREAD_PROCESS_SHARED 46 120 #define _SC_THREAD_STACK_MIN 47 121 #define _SC_THREAD_ATTR_STACKADDR 48 122 #define _SC_THREAD_ATTR_STACKSIZE 49 123 #define _SC_THREAD_PRIORITY_SCHEDULING 50 124 #define _SC_REALTIME_SIGNALS 51 125 #define _SC_MEMORY_PROTECTION 52 126 #define _SC_SIGQUEUE_MAX 53 127 #define _SC_RTSIG_MAX 54 128 #define _SC_MONOTONIC_CLOCK 55 129 #define _SC_DELAYTIMER_MAX 56 130 #define _SC_TIMER_MAX 57 131 #define _SC_TIMERS 58 132 #define _SC_CPUTIME 59 133 #define _SC_THREAD_CPUTIME 60 134 #define _SC_HOST_NAME_MAX 61 135 #define _SC_REGEXP 62 136 #define _SC_SYMLOOP_MAX 63 137 #define _SC_SHELL 64 138 #define _SC_TTY_NAME_MAX 65 139 140 141 /* confstr() constants */ 142 #define _CS_PATH 1 143 144 /* lseek() constants */ 145 #ifndef SEEK_SET 146 # define SEEK_SET 0 147 #endif 148 #ifndef SEEK_CUR 149 # define SEEK_CUR 1 150 #endif 151 #ifndef SEEK_END 152 # define SEEK_END 2 153 #endif 154 #ifndef SEEK_DATA 155 # define SEEK_DATA 3 156 #endif 157 #ifndef SEEK_HOLE 158 # define SEEK_HOLE 4 159 #endif 160 161 162 #ifdef __cplusplus 163 extern "C" { 164 #endif 165 166 /* file functions */ 167 extern int access(const char *path, int accessMode); 168 extern int faccessat(int fd, const char *path, int accessMode, int flag); 169 170 extern int chdir(const char *path); 171 extern int fchdir(int fd); 172 extern char *getcwd(char *buffer, size_t size); 173 174 extern int pipe(int fildes[2]); 175 extern int dup(int fd); 176 extern int dup2(int fd1, int fd2); 177 extern int close(int fd); 178 extern int link(const char *toPath, const char *path); 179 extern int linkat(int toFD, const char *toPath, int pathFD, 180 const char *path, int flag); 181 extern int unlink(const char *name); 182 extern int unlinkat(int fd, const char *path, int flag); 183 extern int rmdir(const char *path); 184 185 extern ssize_t readlink(const char *path, char *buffer, size_t bufferSize); 186 extern ssize_t readlinkat(int fd, const char *path, char *buffer, 187 size_t bufferSize); 188 extern int symlink(const char *toPath, const char *symlinkPath); 189 extern int symlinkat(const char *toPath, int fd, const char *symlinkPath); 190 191 extern int ftruncate(int fd, off_t newSize); 192 extern int truncate(const char *path, off_t newSize); 193 struct ioctl_args { 194 void* argument; 195 size_t size; 196 }; 197 int __ioctl(int fd, ulong cmd, struct ioctl_args args); 198 #ifndef __cplusplus 199 extern int ioctl(int fd, unsigned long op, ...); 200 #ifndef _KERNEL_MODE 201 #define ioctl(a, b, c...) __ioctl(a, b, (struct ioctl_args){ c }) 202 #endif 203 #else 204 inline int 205 ioctl(int fd, unsigned long op, void* argument = NULL, size_t size = 0) 206 { 207 return __ioctl(fd, op, (struct ioctl_args){ argument, size }); 208 } 209 #endif 210 211 extern ssize_t read(int fd, void *buffer, size_t count); 212 extern ssize_t read_pos(int fd, off_t pos, void *buffer, size_t count); 213 extern ssize_t pread(int fd, void *buffer, size_t count, off_t pos); 214 extern ssize_t write(int fd, const void *buffer, size_t count); 215 extern ssize_t write_pos(int fd, off_t pos, const void *buffer,size_t count); 216 extern ssize_t pwrite(int fd, const void *buffer, size_t count, off_t pos); 217 extern off_t lseek(int fd, off_t offset, int whence); 218 219 extern void sync(void); 220 extern int fsync(int fd); 221 222 extern int chown(const char *path, uid_t owner, gid_t group); 223 extern int fchown(int fd, uid_t owner, gid_t group); 224 extern int lchown(const char *path, uid_t owner, gid_t group); 225 extern int fchownat(int fd, const char *path, uid_t owner, gid_t group, 226 int flag); 227 228 extern int getpagesize(void); 229 extern int getdtablesize(void); 230 extern long sysconf(int name); 231 extern long fpathconf(int fd, int name); 232 extern long pathconf(const char *path, int name); 233 extern size_t confstr(int name, char *buf, size_t len); 234 extern int lockf(int fd, int function, off_t size); 235 236 /* process functions */ 237 extern pid_t fork(void); 238 extern pid_t vfork(void); 239 extern int execve(const char *path, char * const argv[], 240 char *const environment[]); 241 extern int execl(const char *path, const char *arg, ...); 242 extern int execv(const char *path, char *const argv[]); 243 extern int execlp(const char *file, const char *arg, ...); 244 extern int execle(const char *path, const char *arg , ... /*, char **envp */); 245 extern int execvp(const char *file, char *const argv[]); 246 extern int execvpe(const char *file, char *const argv[], 247 char *const environment[]); 248 249 extern void _exit(int status) __attribute__ ((noreturn)); 250 251 extern pid_t tcgetpgrp(int fd); 252 extern int tcsetpgrp(int fd, pid_t pgrpid); 253 254 extern int brk(void *addr); 255 extern void *sbrk(intptr_t increment); 256 257 extern unsigned int alarm(unsigned int seconds); 258 extern useconds_t ualarm(useconds_t microSeconds, useconds_t interval); 259 extern unsigned int sleep(unsigned int seconds); 260 extern int usleep(unsigned int microSeconds); 261 extern int pause(void); 262 263 /* process */ 264 extern pid_t getpid(void); 265 extern pid_t getpgrp(void); 266 extern pid_t getppid(void); 267 extern pid_t getsid(pid_t pid); 268 extern pid_t getpgid(pid_t pid); 269 270 extern pid_t setsid(void); 271 extern int setpgid(pid_t pid, pid_t pgid); 272 extern pid_t setpgrp(void); 273 274 extern int chroot(const char *path); 275 276 extern int nice(int incr); 277 278 /* access permissions */ 279 extern gid_t getegid(void); 280 extern uid_t geteuid(void); 281 extern gid_t getgid(void); 282 extern uid_t getuid(void); 283 284 extern int setgid(gid_t gid); 285 extern int setuid(uid_t uid); 286 extern int setegid(gid_t gid); 287 extern int seteuid(uid_t uid); 288 extern int setregid(gid_t rgid, gid_t egid); 289 extern int setreuid(uid_t ruid, uid_t euid); 290 291 extern int getgrouplist(const char* user, gid_t baseGroup, 292 gid_t* groupList, int* groupCount); 293 extern int getgroups(int groupCount, gid_t groupList[]); 294 extern int initgroups(const char* user, gid_t baseGroup); 295 extern int setgroups(int groupCount, const gid_t* groupList); 296 297 extern char *getlogin(void); 298 extern int getlogin_r(char *name, size_t nameSize); 299 300 /* host name */ 301 extern int sethostname(const char *hostName, size_t nameSize); 302 extern int gethostname(char *hostName, size_t nameSize); 303 304 /* tty */ 305 extern int isatty(int fd); 306 extern char *ttyname(int fd); 307 extern int ttyname_r(int fd, char *buffer, size_t bufferSize); 308 309 /* misc */ 310 extern char *crypt(const char *key, const char *salt); 311 extern void encrypt(char block[64], int edflag); 312 extern int getopt(int argc, char *const *argv, const char *shortOpts); 313 extern void swab(const void *src, void *dest, ssize_t nbytes); 314 315 /* getopt() related external variables */ 316 extern char *optarg; 317 extern int optind, opterr, optopt; 318 319 #ifdef __cplusplus 320 } 321 #endif 322 323 #endif /* _UNISTD_H_ */ 324