xref: /linux/tools/include/nolibc/unistd.h (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
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))
34 int _sys_faccessat(int fd, const char *path, int amode, int flag)
35 {
36 	return __nolibc_syscall4(__NR_faccessat, fd, path, amode, flag);
37 }
38 
39 static __attribute__((unused))
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))
46 int access(const char *path, int amode)
47 {
48 	return faccessat(AT_FDCWD, path, amode, 0);
49 }
50 
51 #if !defined(_sys_ftruncate64) && defined(__NR_ftruncate64)
52 static __attribute__((unused))
53 int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
54 {
55 	return __nolibc_syscall3(__NR_ftruncate64, fd, length0, length1);
56 }
57 #define _sys_ftruncate64 _sys_ftruncate64
58 #endif
59 
60 static __attribute__((unused))
61 int _sys_ftruncate(int fd, off_t length)
62 {
63 #if defined(_sys_ftruncate64)
64 	return _sys_ftruncate64(fd, __NOLIBC_LLARGPART(length, 0), __NOLIBC_LLARGPART(length, 1));
65 #else
66 	return __nolibc_syscall2(__NR_ftruncate, fd, length);
67 #endif
68 }
69 
70 static __attribute__((unused))
71 int ftruncate(int fd, off_t length)
72 {
73 	return __sysret(_sys_ftruncate(fd, length));
74 }
75 
76 static __attribute__((unused))
77 int msleep(unsigned int msecs)
78 {
79 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
80 
81 	if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
82 		return (my_timeval.tv_sec * 1000) +
83 			(my_timeval.tv_usec / 1000) +
84 			!!(my_timeval.tv_usec % 1000);
85 	else
86 		return 0;
87 }
88 
89 static __attribute__((unused))
90 unsigned int sleep(unsigned int seconds)
91 {
92 	struct timeval my_timeval = { seconds, 0 };
93 
94 	if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
95 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
96 	else
97 		return 0;
98 }
99 
100 static __attribute__((unused))
101 int usleep(unsigned int usecs)
102 {
103 	struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
104 
105 	return _sys_select(0, NULL, NULL, NULL, &my_timeval);
106 }
107 
108 static __attribute__((unused))
109 int tcsetpgrp(int fd, pid_t pid)
110 {
111 	return ioctl(fd, TIOCSPGRP, &pid);
112 }
113 
114 #endif /* _NOLIBC_UNISTD_H */
115