1/*        $NetBSD: llib-lposix,v 1.3 2025/03/14 06:40:51 rillig Exp $ */
2
3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Jochen Pohl for
18 *        The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/* LINTLIBRARY */
35
36#define _POSIX_SOURCE
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/utsname.h>
41#include <sys/times.h>
42#include <assert.h>
43#include <ctype.h>
44#include <dirent.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <grp.h>
48#include <locale.h>
49#include <math.h>
50#include <pwd.h>
51#include <setjmp.h>
52#include <signal.h>
53#include <stdarg.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <termios.h>
58#include <time.h>
59#include <unistd.h>
60#include <utime.h>
61
62
63/* PROTOLIB1 */
64
65
66void      (abort)(void);
67int       (abs)(int j);
68int       (access)(const char *path, int amode);
69double    (acos)(double x);
70unsigned (alarm)(unsigned seconds);
71char      *(asctime)(const struct tm *timeptr);
72double    (asin)(double x);
73void      (__assert)(const char *expression, int line, const char *file);
74double    (atan)(double x);
75double    (atan2)(double y, double x);
76int       (atexit)(void (*func)(void));
77double    (atof)(const char *nptr);
78int       (atoi)(const char *nptr);
79long      (atol)(const char *nptr);
80void      *(bsearch)(const void *key, const void *base, size_t nmemb,
81                       size_t size, int (*compar)(const void *, const void *));
82void      *(calloc)(size_t nmemb, size_t size);
83double    (ceil)(double x);
84speed_t   (cfgetispeed)(const struct termios *p);
85speed_t   (cfgetospeed)(const struct termios *p);
86int       (cfsetispeed)(struct termios *p, speed_t speed);
87int       (cfsetospeed)(struct termios *p, speed_t speed);
88int       (chdir)(const char *path);
89int       (chmod)(const char *path, mode_t mode);
90int       (chown)(const char *path, uid_t owner, gid_t group);
91void      (clearerr)(FILE *stream);
92clock_t   (clock)(void);
93int       (close)(int fildes);
94int       (closedir)(DIR *dirp);
95double    (cos)(double x);
96double    (cosh)(double x);
97int       (creat)(const char *path, mode_t mode);
98char      *(ctermid)(char *s);
99char      *(ctime)(const time_t *timer);
100char      *(cuserid)(char *s);
101double    (difftime)(time_t time1, time_t time0);
102div_t     (div)(int numer, int denom);
103int       (dup)(int fildes);
104int       (dup2)(int fildes, int fildes2);
105#ifndef errno
106int       (errno);
107#endif
108int       (execl)(const char *path, const char *arg, ...);
109int       (execle)(const char *path, const char *arg, ...);
110int       (execlp)(const char *file, const char *arg, ...);
111int       (execv)(const char *path, char *const argv[]);
112int       (execve)(const char *path, char *const argv[], char *const *envp);
113int       (execvp)(const char *file, char *const argv[]);
114void      (exit)(int status);
115void      (_exit)(int status);
116double    (exp)(double x);
117double    (fabs)(double x);
118int       (fclose)(FILE *stream);
119int       (fcntl)(int fildes, int cmd, ...);
120FILE      *(fdopen)(int fildes, const char *type);
121int       (feof)(FILE *stream);
122int       (ferror)(FILE *stream);
123int       (fflush)(FILE *stream);
124int       (fgetc)(FILE *stream);
125int       (fgetpos)(FILE *stream, fpos_t *pos);
126char      *(fgets)(char *s, int n, FILE *stream);
127int       (fileno)(FILE *stream);
128double    (floor)(double x);
129double    (fmod)(double x, double y);
130FILE      *(fopen)(const char *filename, const char *mode);
131pid_t     (fork)(void);
132long      (fpathconf)(int fildes, int name);
133/* PRINTFLIKE2 */
134int       (fprintf)(FILE *stream, const char *format, ...);
135int       (fputc)(int c, FILE *stream);
136int       (fputs)(const char *s, FILE *stream);
137size_t    (fread)(void *ptr, size_t size, size_t nmemb, FILE *stream);
138void      (free)(void *ptr);
139FILE      *(freopen)(const char *filename, const char *mode, FILE *stream);
140double    (frepx)(double value, int *exp);
141/* SCANFLIKE2 */
142int       (fscanf)(FILE *stream, const char *format, ...);
143int       (fseek)(FILE *stream, long int offset, int whence);
144int       (fsetpos)(FILE *stream, const fpos_t *pos);
145int       (fstat)(int fildes, struct stat *buf);
146long      (ftell)(FILE *stream);
147size_t    (fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
148int       (getc)(FILE *stream);
149int       (getchar)(void);
150char      *(getcwd)(char *buf, size_t size);
151gid_t     (getegid)(void);
152char      *(getenv)(const char *name);
153uid_t     (geteuid)(void);
154gid_t     (getgid)(void);
155struct    group *(getgrgid)(gid_t gid);
156struct    group *(getgrnam)(const char *name);
157int       (getgroups)(int gidsetsize, gid_t grouplist[]);
158char      *(getlogin)(void);
159pid_t     (getpgrp)(void);
160pid_t     (getpid)(void);
161pid_t     (getppid)(void);
162struct    passwd *(getpwnam)(const char *name);
163struct    passwd *(getpwuid)(uid_t uid);
164char      *(gets)(char *s);
165uid_t     (getuid)(void);
166struct    tm *(gmtime)(const time_t *timer);
167int       (isalnum)(int c);
168int       (isalpha)(int c);
169int       (isatty)(int fildes);
170int       (iscntrl)(int c);
171int       (isdigit)(int c);
172int       (isgraph)(int c);
173int       (islower)(int c);
174int       (isprint)(int c);
175int       (ispunct)(int c);
176int       (isspace)(int c);
177int       (isupper)(int c);
178int       (isxdigit)(int c);
179int       (kill)(pid_t pid, int sig);
180long      (labs)(long j);
181double    (ldexp)(double x, int exp);
182ldiv_t    (ldiv)(long numer, long denom);
183int       (link)(const char *existing, const char *new);
184struct    lconv *(localeconv)(void);
185struct    tm *(localtime)(const time_t *timer);
186double    (log)(double x);
187double    (log10)(double x);
188void      (longjmp)(jmp_buf env, int val);
189off_t     (lseek)(int fildes, off_t offset, int whence);
190void      *(malloc)(size_t size);
191int       (mblen)(const char *s, size_t n);
192size_t    (mbstowcs)(wchar_t *pwcs, const char *s, size_t n);
193int       (mbtowc)(wchar_t *pwc, const char *s, size_t n);
194void      *(memchr)(const void *s, int c, size_t n);
195int       (memcmp)(const void *s1, const void *s2, size_t n);
196void      *(memcpy)(void *s1, const void *s2, size_t n);
197void      *(memmove)(void *s1, const void *s2, size_t n);
198void      *(memset)(void *s, int c, size_t n);
199int       (mkdir)(const char *path, mode_t mode);
200int       (mkfifo)(const char *path, mode_t mode);
201time_t    (mktime)(struct tm *timeptr);
202double    (modf)(double value, double *iptr);
203int       (open)(const char *path, int oflag, ...);
204DIR       *(opendir)(const char *dirname);
205long      (pathconf)(const char *path, int name);
206int       (pause)(void);
207void      (perror)(const char *s);
208int       (pipe)(int fildes[2]);
209double    (pow)(double x, double y);
210/* PRINTFLIKE1 */
211int       (printf)(const char *format, ...);
212int       (putc)(int c, FILE *stream);
213int       (putchar)(int c);
214int       (puts)(const char *s);
215void      (qsort)(void *base, size_t nmemb, size_t size,
216                  int (*compar)(const void *, const void *));
217int       (raise)(int sig);
218int       (rand)(void);
219ssize_t   (read)(int fildes, void *buf, size_t nbyte);
220struct    dirent *(readdir)(DIR *dirp);
221void      *(realloc)(void *ptr, size_t size);
222int       (remove)(const char *filename);
223int       (rename)(const char *old, const char *new);
224void      (rewind)(FILE *stream);
225void      (rewinddir)(DIR *dirp);
226int       (rmdir)(const char *path);
227/* SCANFLIKE1 */
228int       (scanf)(const char *format, ...);
229void      (setbuf)(FILE *stream, char *buf);
230int       (setgid)(gid_t gid);
231int       (setjmp)(jmp_buf env);
232char      *(setlocale)(int category, const char *locale);
233int       (setpgid)(pid_t pid, pid_t pgid);
234pid_t     (setsid)(void);
235int       (setuid)(uid_t uid);
236int       (setvbuf)(FILE *stream, char *buf, int mode, size_t size);
237int       (sigaction)(int sig, const struct sigaction *act,
238                        struct sigaction *oact);
239int       (sigaddset)(sigset_t *set, int signo);
240int       (sigdelset)(sigset_t *set, int signo);
241int       (sigemptyset)(sigset_t *set);
242int       (sigfillset)(sigset_t *set);
243int       (sigismember)(const sigset_t *set, int signo);
244void      (siglongjmp)(sigjmp_buf env, int val);
245void      (*(signal)(int sig, void (*func)(int)))(int);
246int       (sigpending)(sigset_t *set);
247int       (sigprocmask)(int how, const sigset_t *set, sigset_t *oset);
248int       (sigsetjmp)(sigjmp_buf env, int savemask);
249int       (sigsuspend)(const sigset_t *sigmask);
250double    (sin)(double x);
251double    (sinh)(double x);
252unsigned (sleep)(unsigned seconds);
253/* PRINTFLIKE2 */
254int       (sprintf)(char *s, const char *format, ...);
255double    (sqrt)(double x);
256void      (srand)(unsigned seed);
257/* SCANFLIKE2 */
258int       (sscanf)(const char *s, const char *format, ...);
259int       (stat)(const char *path, struct stat *buf);
260char      *(strcat)(char *s1, const char *s2);
261char      *(strchr)(const char *s, int c);
262int       (strcmp)(const char *s1, const char *s2);
263int       (strcoll)(const char *s1, const char *s2);
264char      *(strcpy)(char *s1, const char *s2);
265size_t    (strcspn)(const char *s1, const char *s2);
266char      *(strerror)(int errnum);
267size_t    (strftime)(char *s, size_t maxsize, const char *format,
268                        const struct tm *timeptr);
269size_t    (strlen)(const char *s);
270char      *(strncat)(char *s1, const char *s2, size_t n);
271int       (strncmp)(const char *s1, const char *s2, size_t n);
272char      *(strncpy)(char *s1, const char *s2, size_t n);
273char      *(strpbrk)(const char *s1, const char *s2);
274char      *(strrchr)(const char *s, int c);
275size_t    (strspn)(const char *s1, const char *s2);
276char      *(strstr)(const char *s1, const char *s2);
277double    (strtod)(const char *nptr, char **endptr);
278char      *(strtok)(char *s1, const char *s2);
279long      (strtol)(const char *nptr, char **endptr, int base);
280unsigned long (strtoul)(const char *nptr, char **endptr, int base);
281size_t    (strxfrm)(char *s1, const char *s2, size_t n);
282long      (sysconf)(int name);
283int       (system)(const char *string);
284double    (tan)(double x);
285double    (tanh)(double x);
286int       (tcdrain)(int fildes);
287int       (tcflow)(int fildes, int action);
288int       (tcflush)(int fildes, int queue_selector);
289int       (tcgetattr)(int fildes, struct termios *tp);
290pid_t     (tcgetpgrp)(int fildes);
291int       (tcsendbreak)(int fildes, int duration);
292int       (tcsetattr)(int fildes, int options, const struct termios *tp);
293int       (tcsetpgrp)(int fildes, pid_t pgrpid);
294time_t    (time)(time_t *timer);
295clock_t   (times)(struct tms *buffer);
296FILE      *(tmpfile)(void);
297char      *(tmpnam)(char *s);
298int       (tolower)(int c);
299int       (toupper)(int c);
300char      *(ttyname)(int filedes);
301void      (tzset)(void);
302mode_t    (umask)(mode_t cmask);
303int       (uname)(struct utsname *name);
304int       (ungetc)(int c, FILE *stream);
305int       (unlink)(const char *path);
306int       (utime)(const char *path, const struct utimbuf *times);
307int       (vfprintf)(FILE *stream, const char *format, va_list arg);
308int       (vprintf)(const char *format, va_list arg);
309int       (vsprintf)(char *s, const char *format, va_list arg);
310pid_t     (wait)(int *statloc);
311pid_t     (waitpid)(pid_t pid, int *stat_loc, int options);
312size_t    (wcstombs)(char *s, const wchar_t *pwcs, size_t n);
313int       (wctomb)(char *s, wchar_t wchar);
314ssize_t   (write)(int fildes, const void *buf, size_t nbyte);
315