serenity/LibC/unistd.cpp

303 lines
6.2 KiB
C++
Raw Normal View History

#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 <sys/ioctl.h>
#include <Kernel/Syscall.h>
extern "C" {
pid_t fork()
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_fork);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int execve(const char* filename, const char** argv, const char** envp)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_execve, (dword)filename, (dword)argv, (dword)envp);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
uid_t getuid()
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_getuid);
}
gid_t getgid()
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_getgid);
}
uid_t geteuid()
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_geteuid);
}
gid_t getegid()
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_getegid);
}
pid_t getpid()
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_getpid);
}
2018-11-06 12:33:06 +00:00
pid_t getppid()
{
return Syscall::invoke(Syscall::SC_getppid);
}
pid_t setsid()
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_setsid);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
pid_t tcgetpgrp(int fd)
{
return ioctl(fd, TIOCGPGRP);
}
int tcsetpgrp(int fd, pid_t pgid)
{
return ioctl(fd, TIOCSPGRP, pgid);
}
int setpgid(pid_t pid, pid_t pgid)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_setpgid, (dword)pid, (dword)pgid);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
pid_t getpgid(pid_t pid)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_getpgid, (dword)pid);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
pid_t getpgrp()
{
2018-11-06 12:23:22 +00:00
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);
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_chdir, (dword)path);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
char* getcwd(char* buffer, size_t size)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_getcwd, (dword)buffer, (dword)size);
__RETURN_WITH_ERRNO(rc, buffer, nullptr);
}
2018-11-06 14:59:57 +00:00
char* getwd(char* buf)
{
auto* p = getcwd(buf, PATH_MAX);
return p;
2018-11-06 14:59:57 +00:00
}
int sleep(unsigned seconds)
{
2018-11-06 12:23:22 +00:00
return Syscall::invoke(Syscall::SC_sleep, (dword)seconds);
}
2018-10-26 07:54:29 +00:00
int gethostname(char* buffer, size_t size)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_gethostname, (dword)buffer, (dword)size);
2018-10-26 07:54:29 +00:00
__RETURN_WITH_ERRNO(rc, rc, -1);
}
ssize_t readlink(const char* path, char* buffer, size_t size)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
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)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_isatty, (dword)fd);
__RETURN_WITH_ERRNO(rc, 1, 0);
}
int getdtablesize()
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_getdtablesize);
2018-11-11 09:11:09 +00:00
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int dup(int old_fd)
{
2018-11-06 12:23:22 +00:00
int rc = Syscall::invoke(Syscall::SC_dup, (dword)old_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int dup2(int old_fd, int new_fd)
{
2018-11-06 12:23:22 +00:00
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);
}
int mknod(const char* pathname, mode_t, dev_t)
{
(void) pathname;
assert(false);
}
long fpathconf(int fd, int name)
{
(void) fd;
(void) name;
assert(false);
}
long pathconf(const char* path, int name)
{
(void) path;
(void) name;
assert(false);
}
void _exit(int status)
{
Syscall::invoke(Syscall::SC_exit, (dword)status);
assert(false);
}
void sync()
{
Syscall::invoke(Syscall::SC_sync);
}
}