Squash commits for public release
This commit is contained in:
119
libs/libc/posix/fs.c
Normal file
119
libs/libc/posix/fs.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sysdep.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int open(const char* pathname, int flags, ...)
|
||||
{
|
||||
mode_t mode = 0;
|
||||
if ((flags & O_CREAT) != 0) {
|
||||
va_list va;
|
||||
va_start(va, flags);
|
||||
mode = (mode_t)va_arg(va, unsigned);
|
||||
va_end(va);
|
||||
}
|
||||
int res = DO_SYSCALL_3(SYS_OPEN, pathname, flags, mode);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
int creat(const char* path, mode_t mode)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_CREAT, path, mode);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_CLOSE, fd);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
ssize_t read(int fd, char* buf, size_t count)
|
||||
{
|
||||
return (ssize_t)DO_SYSCALL_3(SYS_READ, fd, buf, count);
|
||||
}
|
||||
|
||||
ssize_t write(int fd, const void* buf, size_t count)
|
||||
{
|
||||
return (ssize_t)DO_SYSCALL_3(SYS_WRITE, fd, buf, count);
|
||||
}
|
||||
|
||||
int dup(int oldfd)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_DUP, oldfd);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
int dup2(int oldfd, int newfd)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_DUP2, oldfd, newfd);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
off_t lseek(int fd, off_t off, int whence)
|
||||
{
|
||||
return (off_t)DO_SYSCALL_3(SYS_LSEEK, fd, off, whence);
|
||||
}
|
||||
|
||||
int mkdir(const char* path)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_MKDIR, path);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
int rmdir(const char* path)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_RMDIR, path);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
int chdir(const char* path)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_CHDIR, path);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
char* getcwd(char* buf, size_t size)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_GETCWD, buf, size);
|
||||
RETURN_WITH_ERRNO(res, buf, NULL);
|
||||
}
|
||||
|
||||
int unlink(const char* path)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_UNLINK, path);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
int fstat(int nfds, stat_t* stat)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_FSTAT, nfds, stat);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
int select(int nfds, fd_set_t* readfds, fd_set_t* writefds, fd_set_t* exceptfds, timeval_t* timeout)
|
||||
{
|
||||
int res = DO_SYSCALL_5(SYS_SELECT, nfds, readfds, writefds, exceptfds, timeout);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
{
|
||||
mmap_params_t mmap_params = { 0 };
|
||||
mmap_params.addr = addr;
|
||||
mmap_params.size = length;
|
||||
mmap_params.prot = prot;
|
||||
mmap_params.flags = flags;
|
||||
mmap_params.fd = fd;
|
||||
mmap_params.offset = offset;
|
||||
return (void*)DO_SYSCALL_1(SYS_MMAP, &mmap_params);
|
||||
}
|
||||
|
||||
int munmap(void* addr, size_t length)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_MUNMAP, addr, length);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
44
libs/libc/posix/identity.c
Normal file
44
libs/libc/posix/identity.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <assert.h>
|
||||
#include <pwd.h>
|
||||
#include <string.h>
|
||||
#include <sysdep.h>
|
||||
#include <unistd.h>
|
||||
|
||||
uid_t getuid()
|
||||
{
|
||||
return (uid_t)DO_SYSCALL_0(SYS_GETUID);
|
||||
}
|
||||
|
||||
uid_t geteuid()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setuid(uid_t uid)
|
||||
{
|
||||
return DO_SYSCALL_1(SYS_SETUID, uid);
|
||||
}
|
||||
|
||||
int setgid(gid_t gid)
|
||||
{
|
||||
return DO_SYSCALL_1(SYS_SETGID, gid);
|
||||
}
|
||||
|
||||
int setreuid(uid_t ruid, uid_t euid)
|
||||
{
|
||||
return DO_SYSCALL_2(SYS_SETREUID, ruid, euid);
|
||||
}
|
||||
|
||||
int setregid(gid_t rgid, gid_t egid)
|
||||
{
|
||||
return DO_SYSCALL_2(SYS_SETREGID, rgid, egid);
|
||||
}
|
||||
|
||||
static char loginbuf[128];
|
||||
char* getlogin()
|
||||
{
|
||||
passwd_t* passwd = getpwuid(getuid());
|
||||
strncpy(loginbuf, passwd->pw_name, 128);
|
||||
endpwent();
|
||||
return loginbuf;
|
||||
}
|
||||
14
libs/libc/posix/sched.c
Normal file
14
libs/libc/posix/sched.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <sched.h>
|
||||
#include <sysdep.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void sched_yield()
|
||||
{
|
||||
DO_SYSCALL_0(SYS_SCHED_YIELD);
|
||||
}
|
||||
|
||||
int nice(int inc)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_NICE, inc);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
26
libs/libc/posix/signal.c
Normal file
26
libs/libc/posix/signal.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <signal.h>
|
||||
#include <sysdep.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int sigaction(int signo, sighandler_t callback)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_SIGACTION, signo, callback);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
sighandler_t signal(int signo, sighandler_t callback)
|
||||
{
|
||||
sigaction(signo, callback);
|
||||
return SIG_DFL;
|
||||
}
|
||||
|
||||
int raise(int signo)
|
||||
{
|
||||
return kill(getpid(), signo);
|
||||
}
|
||||
|
||||
int kill(pid_t pid, int signo)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_KILL, pid, signo);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
34
libs/libc/posix/system.c
Normal file
34
libs/libc/posix/system.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sysdep.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int uname(utsname_t* buf)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_UNAME, buf);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
#define PATH_CONSTANT "/bin:/usr/bin"
|
||||
#define PATH_CONSTANT_LEN sizeof(PATH_CONSTANT)
|
||||
size_t confstr(int name, char* buf, size_t len)
|
||||
{
|
||||
switch (name) {
|
||||
case _CS_PATH:
|
||||
if (!buf || !len) {
|
||||
return PATH_CONSTANT_LEN;
|
||||
} else {
|
||||
// Return path only if enough space.
|
||||
if (len < PATH_CONSTANT_LEN) {
|
||||
return 0;
|
||||
}
|
||||
memcpy(buf, PATH_CONSTANT, PATH_CONSTANT_LEN);
|
||||
return PATH_CONSTANT_LEN;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
133
libs/libc/posix/tasking.c
Normal file
133
libs/libc/posix/tasking.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sysdep.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int fork()
|
||||
{
|
||||
int res = DO_SYSCALL_0(SYS_FORK);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
int execve(const char* path, char* const argv[], char* const envp[])
|
||||
{
|
||||
int res = DO_SYSCALL_3(SYS_EXECVE, path, argv, envp);
|
||||
RETURN_WITH_ERRNO(res, -1, -1);
|
||||
}
|
||||
|
||||
int execvpe(const char* path, char* const argv[], char* const envp[])
|
||||
{
|
||||
if (strchr(path, '/')) {
|
||||
return execve(path, argv, envp);
|
||||
}
|
||||
|
||||
char* full_path = malloc(256);
|
||||
size_t namelen = strlen(path);
|
||||
char* env_path = getenv("PATH");
|
||||
if (!env_path) {
|
||||
// Get it from confstr
|
||||
env_path = "/bin:/usr/bin";
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int cnt = 0;
|
||||
for (int i = 0; env_path[i]; i += len) {
|
||||
len = 0;
|
||||
while (env_path[i + len] && env_path[i + len] != ':') {
|
||||
len++;
|
||||
}
|
||||
|
||||
memcpy(full_path, &env_path[i], len);
|
||||
full_path[len] = '/';
|
||||
memcpy(&full_path[len + 1], path, namelen + 1);
|
||||
|
||||
int err = execve(full_path, argv, envp);
|
||||
if (env_path[i + len] == ':') {
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
free(full_path);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int execvp(const char* path, char* const argv[])
|
||||
{
|
||||
return execvpe(path, argv, environ);
|
||||
}
|
||||
|
||||
int execlp(const char* path, const char* arg0, ...)
|
||||
{
|
||||
const char* args[16];
|
||||
int nxt = 0;
|
||||
args[nxt++] = arg0;
|
||||
|
||||
va_list va;
|
||||
va_start(va, arg0);
|
||||
for (;;) {
|
||||
const char* arg = va_arg(va, const char*);
|
||||
if (!arg) {
|
||||
break;
|
||||
}
|
||||
args[nxt++] = arg;
|
||||
}
|
||||
va_end(va);
|
||||
args[nxt++] = NULL;
|
||||
return execvpe(path, (char* const*)args, environ);
|
||||
}
|
||||
|
||||
int wait(int pid)
|
||||
{
|
||||
int res = DO_SYSCALL_3(SYS_WAITPID, pid, NULL, 0);
|
||||
RETURN_WITH_ERRNO(res, pid, -1);
|
||||
}
|
||||
|
||||
int waitpid(int pid, int* status, int options)
|
||||
{
|
||||
int res = DO_SYSCALL_3(SYS_WAITPID, pid, status, options);
|
||||
RETURN_WITH_ERRNO(res, pid, -1);
|
||||
}
|
||||
|
||||
pid_t getpid()
|
||||
{
|
||||
int res = DO_SYSCALL_0(SYS_GETPID);
|
||||
RETURN_WITH_ERRNO(res, (pid_t)res, -1);
|
||||
}
|
||||
|
||||
int setpgid(pid_t pid, pid_t pgid)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_SETPGID, pid, pgid);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
pid_t getpgid(pid_t pid)
|
||||
{
|
||||
int res = DO_SYSCALL_1(SYS_GETPGID, pid);
|
||||
RETURN_WITH_ERRNO(res, (pid_t)res, -1);
|
||||
}
|
||||
|
||||
uint32_t usleep(uint32_t usec)
|
||||
{
|
||||
timespec_t ts = { 0 };
|
||||
ts.tv_sec = usec / 1000000;
|
||||
ts.tv_nsec = (usec % 1000000) * 1000;
|
||||
int err = nanosleep(&ts, NULL);
|
||||
if (err) {
|
||||
return 0;
|
||||
}
|
||||
return usec;
|
||||
}
|
||||
|
||||
uint32_t sleep(uint32_t seconds)
|
||||
{
|
||||
timespec_t ts = { 0 };
|
||||
ts.tv_sec = seconds;
|
||||
int err = nanosleep(&ts, NULL);
|
||||
if (err) {
|
||||
return 0;
|
||||
}
|
||||
return seconds;
|
||||
}
|
||||
19
libs/libc/posix/time.c
Normal file
19
libs/libc/posix/time.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <sys/time.h>
|
||||
#include <sysdep.h>
|
||||
|
||||
int nanosleep(const timespec_t* req, timespec_t* rem)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_NANOSLEEP, req, rem);
|
||||
RETURN_WITH_ERRNO(res, 0, -1);
|
||||
}
|
||||
|
||||
int gettimeofday(timeval_t* tv, timezone_t* tz)
|
||||
{
|
||||
int res = DO_SYSCALL_2(SYS_GETTIMEOFDAY, tv, tz);
|
||||
RETURN_WITH_ERRNO(res, res, -1);
|
||||
}
|
||||
|
||||
int settimeofday(const timeval_t* tv, const timezone_t* tz)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
Reference in New Issue
Block a user