This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathunistd.c
314 lines (246 loc) · 6.63 KB
/
unistd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2020. All rights reserved.
*/
#include <stddef.h>
#include <sys/syscall.h>
#include <assert.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char** environ = NULL;
char** __original_environ = NULL;
pid_t fork() {
return syscall(SYS_FORK);
}
pid_t vfork() {
return fork();
}
int execv(const char* path, char* const argv[]) {
return execve(path, argv, environ);
}
int execve(const char* filename, char* const argv[], char* const envp[]) {
return syscall4(SYS_EXECVE, (int) filename, (int) argv, (int) envp);
}
int execvpe(const char* filename, char* const argv[], char* const envp[]) {
char cwd[512];
getcwd(cwd, 512);
// If the path contains a slash, ignore the path
if(strchr(filename, '/'))
execve(filename, argv, envp);
// Get the path
char* path = getenv("PATH");
if(!path)
path = DEFAULT_PATH;
path = strdup(path);
// Try each element of path
char* path_elem = strtok(path, ":");
while(path_elem) {
// Create a string with the full path of the executable
size_t path_len = strlen(path_elem);
char* full_path = malloc(path_len + strlen(filename) + 2);
strcpy(full_path, path_elem);
full_path[path_len] = '/';
strcpy(full_path + path_len + 1, filename);
// Try executing it
int res = execve(full_path, argv, envp);
free(full_path);
if(res && errno != ENOENT)
return res;
path_elem = strtok(NULL, ":");
}
free(path);
errno = ENOENT;
return -1;
}
int execvp(const char* filename, char* const argv[]) {
return execvpe(filename, argv, environ);
}
int execl(const char* filename, const char* arg, ...) {
return -1;
}
int execlp(const char* filename, const char* arg, ...) {
return -1;
}
pid_t getpid() {
return syscall(SYS_GETPID);
}
pid_t getppid() {
return syscall(SYS_GETPPID);
}
pid_t getsid(pid_t pid) {
return syscall(SYS_GETSID);
}
pid_t setsid() {
return syscall(SYS_SETSID);
}
int setpgid(pid_t pid, pid_t pgid) {
return syscall3(SYS_SETPGID, pid, pgid);
}
pid_t getpgid(pid_t pid) {
return syscall2(SYS_GETPGID, pid);
}
pid_t getpgrp() {
return syscall(SYS_GETPGRP);
}
uid_t getuid() {
return syscall(SYS_GETUID);
}
uid_t geteuid() {
return syscall(SYS_GETEUID);
}
gid_t getgid() {
return syscall(SYS_GETGID);
}
gid_t getegid() {
return syscall(SYS_GETEGID);
}
int getgroups(int ngroups, gid_t list[]) {
return syscall3(SYS_GETGROUPS, ngroups, (int) list);
}
int setgroups(size_t ngroups, const gid_t* list) {
return syscall3(SYS_SETGROUPS, ngroups, (int) list);
}
int setuid(uid_t uid) {
return syscall2(SYS_SETUID, uid);
}
int setgid(gid_t gid) {
return syscall2(SYS_SETGID, gid);
}
ssize_t read(int fd, void* buf, size_t count) {
return syscall4(SYS_READ, fd, (int) buf, count);
}
ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
return -1;
}
ssize_t write(int fd, const void* buf, size_t count) {
return syscall4(SYS_WRITE, fd, (int) buf, count);
}
ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
return -1;
}
off_t lseek(int fd, off_t off, int whence) {
return syscall4(SYS_LSEEK, fd, off, whence);
}
int fchown(int fd, uid_t uid, gid_t gid) {
return syscall4(SYS_FCHOWN, fd, uid, gid);
}
int ftruncate(int fd, off_t length) {
return syscall3(SYS_FTRUNCATE, fd, length);
}
int close(int fd) {
return syscall2(SYS_CLOSE, fd);
}
int isatty(int fd) {
return syscall2(SYS_ISATTY, fd) == 1 ? 1 : 0;
}
ssize_t readlink(const char* path, char* buf, size_t size) {
return syscall4(SYS_READLINK, (int) path, (int) buf, (int) size);
}
ssize_t readlinkat(int fd, const char* path, char* buf, size_t size) {
struct readlinkat_args args = {fd, path, buf, size};
return syscall2(SYS_READLINKAT, (int) &args);
}
int link(const char* oldpath, const char* newpath) {
return syscall3(SYS_LINK, (int) oldpath, (int) newpath);
}
int unlink(const char* pathname) {
return syscall2(SYS_UNLINK, (int) pathname);
}
int symlink(const char* target, const char* linkname) {
return syscall3(SYS_SYMLINK, (int) target, (int) linkname);
}
int rmdir(const char* pathname) {
return syscall2(SYS_RMDIR, (int) pathname);
}
int chown(const char* pathname, uid_t uid, gid_t gid) {
return syscall4(SYS_CHOWN, (int) pathname, (int) uid, (int) gid);
}
int truncate(const char* path, off_t length) {
return syscall3(SYS_TRUNCATE, (int) path, (int) length);
}
int access(const char* path, int how) {
return syscall3(SYS_ACCESS, (int) path, (int) how);
}
int dup(int fd) {
return syscall2(SYS_DUP, fd);
}
int dup2(int old_fd, int new_fd) {
return syscall3(SYS_DUP2, old_fd, new_fd);
}
int pipe(int pipefd[2]) {
return syscall3(SYS_PIPE, (int) pipefd, 0);
}
int pipe2(int pipefd[2], int flags) {
return syscall3(SYS_PIPE, (int) pipefd, flags);
}
long pathconf(const char* path, int name) {
return fpathconf(0, name);
}
long fpathconf(int fd, int name) {
switch (name) {
case _PC_NAME_MAX:
return NAME_MAX;
case _PC_PATH_MAX:
return PATH_MAX;
case _PC_VDISABLE:
return _POSIX_VDISABLE;
case _PC_LINK_MAX:
return LINK_MAX;
default:
return 0;
}
}
int chdir(const char* pathname) {
return syscall2(SYS_CHDIR, (int) pathname);
}
int fchdir(int fd) {
return -1;
}
char* getcwd(char* buf, size_t size) {
return (char*) syscall3(SYS_GETCWD, (int) buf, (int) size);
}
char* getwd(char* buf) {
return (char*) syscall2(SYS_GETCWD, (int) buf);
}
int sleep(unsigned secs) {
struct timespec time = {secs, 0};
struct timespec remainder;
if(syscall3_noerr(SYS_SLEEP, (int) &time, (int) &remainder) < 0)
return remainder.tv_sec;
return 0;
}
int usleep(useconds_t usec) {
struct timespec time = {0, usec * 1000};
struct timespec remainder;
return syscall3(SYS_SLEEP, (int) &time, (int) &remainder);
}
pid_t tcgetpgrp(int fd) {
return ioctl(fd, TIOCGPGRP);
}
int tcsetpgrp(int fd, pid_t pgid) {
return ioctl(fd, TIOCSPGRP, pgid);
}
unsigned int alarm(unsigned int seconds) {
return -1; // TODO
}
void _exit(int status) {
syscall2(SYS_EXIT, status);
__builtin_unreachable();
}
int getpagesize() {
return PAGE_SIZE;
}