1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * unistd function definitions for NOLIBC
4  * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
5  */
6 
7 #ifndef _NOLIBC_UNISTD_H
8 #define _NOLIBC_UNISTD_H
9 
10 #include "std.h"
11 #include "arch.h"
12 #include "types.h"
13 #include "sys.h"
14 
15 
16 #define STDIN_FILENO  0
17 #define STDOUT_FILENO 1
18 #define STDERR_FILENO 2
19 
20 
21 static __attribute__((unused))
msleep(unsigned int msecs)22 int msleep(unsigned int msecs)
23 {
24 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
25 
26 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
27 		return (my_timeval.tv_sec * 1000) +
28 			(my_timeval.tv_usec / 1000) +
29 			!!(my_timeval.tv_usec % 1000);
30 	else
31 		return 0;
32 }
33 
34 static __attribute__((unused))
sleep(unsigned int seconds)35 unsigned int sleep(unsigned int seconds)
36 {
37 	struct timeval my_timeval = { seconds, 0 };
38 
39 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
40 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
41 	else
42 		return 0;
43 }
44 
45 static __attribute__((unused))
usleep(unsigned int usecs)46 int usleep(unsigned int usecs)
47 {
48 	struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
49 
50 	return sys_select(0, 0, 0, 0, &my_timeval);
51 }
52 
53 static __attribute__((unused))
tcsetpgrp(int fd,pid_t pid)54 int tcsetpgrp(int fd, pid_t pid)
55 {
56 	return ioctl(fd, TIOCSPGRP, &pid);
57 }
58 
59 #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
60 #define _syscall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
61 #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
62 #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
63 #define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__)
64 
65 /* make sure to include all global symbols */
66 #include "nolibc.h"
67 
68 #endif /* _NOLIBC_UNISTD_H */
69