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 /* make sure to include all global symbols */
8 #include "nolibc.h"
9
10 #ifndef _NOLIBC_UNISTD_H
11 #define _NOLIBC_UNISTD_H
12
13 #include "std.h"
14 #include "arch.h"
15 #include "types.h"
16 #include "sys.h"
17
18
19 #define STDIN_FILENO 0
20 #define STDOUT_FILENO 1
21 #define STDERR_FILENO 2
22
23 #define F_OK 0
24 #define X_OK 1
25 #define W_OK 2
26 #define R_OK 4
27
28 /*
29 * int access(const char *path, int amode);
30 * int faccessat(int fd, const char *path, int amode, int flag);
31 */
32
33 static __attribute__((unused))
sys_faccessat(int fd,const char * path,int amode,int flag)34 int sys_faccessat(int fd, const char *path, int amode, int flag)
35 {
36 return my_syscall4(__NR_faccessat, fd, path, amode, flag);
37 }
38
39 static __attribute__((unused))
faccessat(int fd,const char * path,int amode,int flag)40 int faccessat(int fd, const char *path, int amode, int flag)
41 {
42 return __sysret(sys_faccessat(fd, path, amode, flag));
43 }
44
45 static __attribute__((unused))
access(const char * path,int amode)46 int access(const char *path, int amode)
47 {
48 return faccessat(AT_FDCWD, path, amode, 0);
49 }
50
51
52 static __attribute__((unused))
msleep(unsigned int msecs)53 int msleep(unsigned int msecs)
54 {
55 struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
56
57 if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
58 return (my_timeval.tv_sec * 1000) +
59 (my_timeval.tv_usec / 1000) +
60 !!(my_timeval.tv_usec % 1000);
61 else
62 return 0;
63 }
64
65 static __attribute__((unused))
sleep(unsigned int seconds)66 unsigned int sleep(unsigned int seconds)
67 {
68 struct timeval my_timeval = { seconds, 0 };
69
70 if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
71 return my_timeval.tv_sec + !!my_timeval.tv_usec;
72 else
73 return 0;
74 }
75
76 static __attribute__((unused))
usleep(unsigned int usecs)77 int usleep(unsigned int usecs)
78 {
79 struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
80
81 return sys_select(0, 0, 0, 0, &my_timeval);
82 }
83
84 static __attribute__((unused))
tcsetpgrp(int fd,pid_t pid)85 int tcsetpgrp(int fd, pid_t pid)
86 {
87 return ioctl(fd, TIOCSPGRP, &pid);
88 }
89
90 #endif /* _NOLIBC_UNISTD_H */
91