summaryrefslogtreecommitdiff
path: root/LibC/unistd.cpp
blob: d1aaeaa3d9203e9c6335b2c0b66c28b4d5381afe (plain)
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
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <Kernel/Syscall.h>

extern "C" {

pid_t fork()
{
    int rc = Syscall::invoke(Syscall::SC_fork);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int execve(const char* filename, const char** argv, const char** envp)
{
    int rc = Syscall::invoke(Syscall::SC_execve, (dword)filename, (dword)argv, (dword)envp);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

uid_t getuid()
{
    return Syscall::invoke(Syscall::SC_getuid);
}

gid_t getgid()
{
    return Syscall::invoke(Syscall::SC_getgid);
}

uid_t geteuid()
{
    return Syscall::invoke(Syscall::SC_geteuid);
}

gid_t getegid()
{
    return Syscall::invoke(Syscall::SC_getegid);
}

pid_t getpid()
{
    return Syscall::invoke(Syscall::SC_getpid);
}

pid_t getppid()
{
    return Syscall::invoke(Syscall::SC_getppid);
}

pid_t setsid()
{
    int rc = Syscall::invoke(Syscall::SC_setsid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

pid_t tcgetpgrp(int fd)
{
    int rc = Syscall::invoke(Syscall::SC_tcgetpgrp, (dword)fd);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int tcsetpgrp(int fd, pid_t pgid)
{
    int rc = Syscall::invoke(Syscall::SC_tcsetpgrp, (dword)fd, (dword)pgid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int setpgid(pid_t pid, pid_t pgid)
{
    int rc = Syscall::invoke(Syscall::SC_setpgid, (dword)pid, (dword)pgid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

pid_t getpgid(pid_t pid)
{
    int rc = Syscall::invoke(Syscall::SC_getpgid, (dword)pid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

pid_t getpgrp()
{
    int rc = Syscall::invoke(Syscall::SC_getpgrp);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int open(const char* path, int options, ...)
{
    va_list ap;
    va_start(ap, options);
    int rc = Syscall::invoke(Syscall::SC_open, (dword)path, (dword)options, (dword)ap);
    va_end(ap);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

ssize_t read(int fd, void* buf, size_t count)
{
    int rc = Syscall::invoke(Syscall::SC_read, (dword)fd, (dword)buf, (dword)count);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

ssize_t write(int fd, const void* buf, size_t count)
{
    int rc = Syscall::invoke(Syscall::SC_write, (dword)fd, (dword)buf, (dword)count);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int ttyname_r(int fd, char* buffer, size_t size)
{
    int rc = Syscall::invoke(Syscall::SC_ttyname_r, (dword)fd, (dword)buffer, (dword)size);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

static char ttyname_buf[32];
char* ttyname(int fd)
{
    if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
        return nullptr;
    return ttyname_buf;
}

int close(int fd)
{
    int rc = Syscall::invoke(Syscall::SC_close, fd);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

pid_t waitpid(pid_t waitee, int* wstatus, int options)
{
    int rc = Syscall::invoke(Syscall::SC_waitpid, waitee, (dword)wstatus, (dword)options);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int lstat(const char* path, struct stat* statbuf)
{
    int rc = Syscall::invoke(Syscall::SC_lstat, (dword)path, (dword)statbuf);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int stat(const char* path, struct stat* statbuf)
{
    int rc = Syscall::invoke(Syscall::SC_stat, (dword)path, (dword)statbuf);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int fstat(int fd, struct stat *statbuf)
{
    int rc = Syscall::invoke(Syscall::SC_fstat, (dword)fd, (dword)statbuf);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int chdir(const char* path)
{
    int rc = Syscall::invoke(Syscall::SC_chdir, (dword)path);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

char* getcwd(char* buffer, size_t size)
{
    int rc = Syscall::invoke(Syscall::SC_getcwd, (dword)buffer, (dword)size);
    __RETURN_WITH_ERRNO(rc, buffer, nullptr);
}

char* getwd(char* buf)
{
    return getcwd(buf, PATH_MAX);
}

int sleep(unsigned seconds)
{
    return Syscall::invoke(Syscall::SC_sleep, (dword)seconds);
}

int gethostname(char* buffer, size_t size)
{
    int rc = Syscall::invoke(Syscall::SC_gethostname, (dword)buffer, (dword)size);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

ssize_t readlink(const char* path, char* buffer, size_t size)
{
    int rc = Syscall::invoke(Syscall::SC_readlink, (dword)path, (dword)buffer, (dword)size);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

off_t lseek(int fd, off_t offset, int whence)
{
    int rc = Syscall::invoke(Syscall::SC_lseek, (dword)fd, (dword)offset, (dword)whence);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int link(const char*, const char*)
{
    assert(false);
}

int unlink(const char*)
{
    assert(false);
}

int isatty(int fd)
{
    int rc = Syscall::invoke(Syscall::SC_isatty, (dword)fd);
    __RETURN_WITH_ERRNO(rc, 1, 0);
}

int getdtablesize()
{
    int rc = Syscall::invoke(Syscall::SC_getdtablesize);
    __RETURN_WITH_ERRNO(rc, 1, 0);
}

int dup(int old_fd)
{
    int rc = Syscall::invoke(Syscall::SC_dup, (dword)old_fd);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int dup2(int old_fd, int new_fd)
{
    int rc = Syscall::invoke(Syscall::SC_dup2, (dword)old_fd, (dword)new_fd);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int setgroups(size_t size, const gid_t* list)
{
    int rc = Syscall::invoke(Syscall::SC_getgroups, (dword)size, (dword)list);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int getgroups(int size, gid_t list[])
{
    int rc = Syscall::invoke(Syscall::SC_getgroups, (dword)size, (dword)list);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int pipe(int pipefd[2])
{
    int rc = Syscall::invoke(Syscall::SC_pipe, (dword)pipefd);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

unsigned int alarm(unsigned int seconds)
{
    return Syscall::invoke(Syscall::SC_alarm, (dword)seconds);
}

int setuid(uid_t uid)
{
    int rc = Syscall::invoke(Syscall::SC_setuid, (dword)uid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int setgid(uid_t gid)
{
    int rc = Syscall::invoke(Syscall::SC_setgid, (dword)gid);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

int access(const char* pathname, int mode)
{
    int rc = Syscall::invoke(Syscall::SC_access, (dword)pathname, (dword)mode);
    __RETURN_WITH_ERRNO(rc, rc, -1);
}

}