Break up unistd into separate files for each function

This commit is contained in:
K. Lange 2018-05-02 12:20:06 +09:00
parent 4c8f8e43fb
commit 8f2025864c
31 changed files with 255 additions and 185 deletions

View File

@ -1,5 +1,4 @@
#ifndef _SYSCALL_H
#define _SYSCALL_H
#pragma once
#include <stdint.h>
#include <stddef.h>
@ -113,8 +112,8 @@ DECL_SYSCALL3(fswait2,int,int*,int);
DECL_SYSCALL3(chown,char*,int,int);
DECL_SYSCALL3(waitpid, int, int *, int);
DECL_SYSCALL5(mount, char *, char *, char *, unsigned long, void *);
#endif
DECL_SYSCALL1(pipe, int *);
DECL_SYSCALL3(readlink, char *, char *, int);
/*
* vim:tabstop=4
* vim:noexpandtab

View File

@ -21,6 +21,8 @@ extern void _exit(int status);
extern uid_t getuid(void);
extern uid_t geteuid(void);
extern gid_t getgid(void);
extern gid_t getegid(void);
extern char * getcwd(char *buf, size_t size);
extern int pipe(int pipefd[2]);
extern int dup(int oldfd);

View File

@ -6,34 +6,21 @@
DEFN_SYSCALL1(exit, 0, int);
DEFN_SYSCALL1(print, 1, const char *);
DEFN_SYSCALL3(open, 2, const char *, int, int);
DEFN_SYSCALL3(read, 3, int, char *, int);
DEFN_SYSCALL3(write, 4, int, char *, int);
DEFN_SYSCALL1(close, 5, int);
DEFN_SYSCALL2(gettimeofday, 6, void *, void *);
DEFN_SYSCALL3(execve, 7, char *, char **, char **);
DEFN_SYSCALL0(fork, 8);
DEFN_SYSCALL0(getpid, 9);
DEFN_SYSCALL1(sbrk, 10, int);
DEFN_SYSCALL0(getgraphicsaddress, 11);
DEFN_SYSCALL1(uname, 12, void *);
DEFN_SYSCALL5(openpty, 13, int *, int *, char *, void *, void *);
DEFN_SYSCALL3(lseek, 14, int, int, int);
DEFN_SYSCALL2(fstat, 15, int, void *);
DEFN_SYSCALL1(setgraphicsoffset, 16, int);
DEFN_SYSCALL1(wait, 17, unsigned int);
DEFN_SYSCALL0(getgraphicswidth, 18);
DEFN_SYSCALL0(getgraphicsheight, 19);
DEFN_SYSCALL0(getgraphicsdepth, 20);
DEFN_SYSCALL0(mkpipe, 21);
DEFN_SYSCALL2(dup2, 22, int, int);
DEFN_SYSCALL0(getuid, 23);
DEFN_SYSCALL1(setuid, 24, unsigned int);
DEFN_SYSCALL1(kernel_string_XXX, 25, char *);
DEFN_SYSCALL0(reboot, 26);
DEFN_SYSCALL3(readdir, 27, int, int, void *);
DEFN_SYSCALL1(chdir, 28, char *);
DEFN_SYSCALL2(getcwd, 29, char *, size_t);
DEFN_SYSCALL3(clone, 30, uintptr_t, uintptr_t, void *);
DEFN_SYSCALL1(sethostname, 31, char *);
DEFN_SYSCALL1(gethostname, 32, char *);
@ -41,8 +28,6 @@ DEFN_SYSCALL0(mousedevice, 33);
DEFN_SYSCALL2(mkdir, 34, char *, unsigned int);
DEFN_SYSCALL2(shm_obtain, 35, char *, size_t *);
DEFN_SYSCALL1(shm_release, 36, char *);
DEFN_SYSCALL2(send_signal, 37, uint32_t, uint32_t);
DEFN_SYSCALL2(signal, 38, uint32_t, void *);
DEFN_SYSCALL2(share_fd, 39, int, int);
DEFN_SYSCALL1(get_fd, 40, int);
DEFN_SYSCALL0(gettid, 41);
@ -50,7 +35,6 @@ DEFN_SYSCALL0(yield, 42);
DEFN_SYSCALL2(system_function, 43, int, char **);
DEFN_SYSCALL1(open_serial, 44, int);
DEFN_SYSCALL2(sleepabs, 45, unsigned long, unsigned long);
DEFN_SYSCALL2(nanosleep, 46, unsigned long, unsigned long);
DEFN_SYSCALL3(ioctl, 47, int, int, void *);
DEFN_SYSCALL2(access, 48, char *, int);
DEFN_SYSCALL2(stat, 49, char *, void *);
@ -58,10 +42,8 @@ DEFN_SYSCALL2(chmod, 50, char *, int);
DEFN_SYSCALL1(umask, 51, int);
DEFN_SYSCALL1(unlink, 52, char *);
DEFN_SYSCALL3(waitpid, 53, int, int *, int);
DEFN_SYSCALL1(pipe, 54, int *);
DEFN_SYSCALL5(mount, SYS_MOUNT, char *, char *, char *, unsigned long, void *);
DEFN_SYSCALL2(symlink, SYS_SYMLINK, char *, char *);
DEFN_SYSCALL3(readlink, SYS_READLINK, char *, char *, int);
DEFN_SYSCALL2(lstat, SYS_LSTAT, char *, void *);
DEFN_SYSCALL2(fswait, SYS_FSWAIT, int, int *);
DEFN_SYSCALL3(fswait2, SYS_FSWAIT2, int, int *,int);

9
libc/signal/kill.c Normal file
View File

@ -0,0 +1,9 @@
#include <signal.h>
#include <syscall.h>
DEFN_SYSCALL2(send_signal, 37, uint32_t, uint32_t);
int kill(int pid, int sig) {
return syscall_send_signal(pid, sig);
}

8
libc/signal/signal.c Normal file
View File

@ -0,0 +1,8 @@
#include <signal.h>
#include <syscall.h>
DEFN_SYSCALL2(signal, 38, uint32_t, void *);
sighandler_t signal(int signum, sighandler_t handler) {
return (sighandler_t)syscall_signal(signum, (void *)handler);
}

8
libc/sys/uname.c Normal file
View File

@ -0,0 +1,8 @@
#include <sys/utsname.h>
#include <syscall.h>
DEFN_SYSCALL1(uname, 12, void *);
int uname(struct utsname *__name) {
return syscall_uname((void *)__name);
}

9
libc/unistd/chdir.c Normal file
View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL1(chdir, 28, char *);
int chdir(const char *path) {
return syscall_chdir((char*)path);
}

8
libc/unistd/close.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL1(close, 5, int);
int close(int file) {
return syscall_close(file);
}

6
libc/unistd/creat.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
#include <fcntl.h>
int creat(const char *path, mode_t mode) {
return open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
}

8
libc/unistd/dup2.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL2(dup2, 22, int, int);
int dup2(int oldfd, int newfd) {
return syscall_dup2(oldfd, newfd);
}

6
libc/unistd/exit.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
void exit(int val) {
// TODO call exit cleanup handlers (including flushing buffers?)
_exit(val);
}

8
libc/unistd/fork.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL0(fork, 8);
pid_t fork(void) {
return syscall_fork();
}

10
libc/unistd/fstat.c Normal file
View File

@ -0,0 +1,10 @@
#include <unistd.h>
#include <sys/stat.h>
#include <syscall.h>
DEFN_SYSCALL2(fstat, 15, int, void *);
int fstat(int file, struct stat *st) {
syscall_fstat(file, st);
return 0;
}

11
libc/unistd/getcwd.c Normal file
View File

@ -0,0 +1,11 @@
#include <unistd.h>
#include <string.h>
#include <syscall.h>
DEFN_SYSCALL2(getcwd, 29, char *, size_t);
char *getcwd(char *buf, size_t size) {
if (!buf) buf = malloc(size);
return (char *)syscall_getcwd(buf, size);
}

6
libc/unistd/getegid.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
int getegid() {
return getgid();
}

5
libc/unistd/geteuid.c Normal file
View File

@ -0,0 +1,5 @@
#include <unistd.h>
int geteuid() {
return getuid();
}

5
libc/unistd/getgid.c Normal file
View File

@ -0,0 +1,5 @@
#include <unistd.h>
int getgid() {
return getuid();
}

6
libc/unistd/getpgrp.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
int getpgrp() {
/* XXX */
return getgid();
}

8
libc/unistd/getpid.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL0(getpid, 9);
pid_t getpid(void) {
return syscall_getpid();
}

8
libc/unistd/getuid.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL0(getuid, 23);
int getuid() {
return syscall_getuid();
}

5
libc/unistd/getwd.c Normal file
View File

@ -0,0 +1,5 @@
#include <unistd.h>
char *getwd(char *buf) {
return getcwd(buf, 256);
}

10
libc/unistd/isatty.c Normal file
View File

@ -0,0 +1,10 @@
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
int isatty(int fd) {
int dtype = ioctl(fd, IOCTLDTYPE, NULL);
if (dtype == IOCTL_DTYPE_TTY) return 1;
errno = EINVAL;
return 0;
}

9
libc/unistd/link.c Normal file
View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <errno.h>
// TODO:
// We have a system call for this?
int link(const char *old, const char *new) {
errno = EMLINK;
return -1;
}

9
libc/unistd/lseek.c Normal file
View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL3(lseek, 14, int, int, int);
off_t lseek(int file, off_t ptr, int dir) {
return syscall_lseek(file,ptr,dir);
}

30
libc/unistd/open.c Normal file
View File

@ -0,0 +1,30 @@
#include <unistd.h>
#include <fcntl.h>
#include <va_list.h>
#include <errno.h>
#include <syscall.h>
DEFN_SYSCALL3(open, 2, const char *, int, int);
int open(const char *name, int flags, ...) {
va_list argp;
int mode = 0;
int result;
va_start(argp, flags);
if (flags & O_CREAT) mode = va_arg(argp, int);
va_end(argp);
result = syscall_open(name, flags, mode);
if (result == -1) {
if (flags & O_CREAT) {
errno = EACCES;
} else {
errno = ENOENT;
}
} else if (result < 0) {
errno = -result;
}
return result;
}

14
libc/unistd/pipe.c Normal file
View File

@ -0,0 +1,14 @@
#include <unistd.h>
#include <errno.h>
#include <syscall.h>
DEFN_SYSCALL1(pipe, 54, int *);
int pipe(int fildes[2]) {
int ret = syscall_pipe((int *)fildes);
if (ret < 0) {
errno = -ret;
return -1;
}
return ret;
}

8
libc/unistd/read.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL3(read, 3, int, char *, int);
int read(int file, void *ptr, size_t len) {
return syscall_read(file,ptr,len);
}

18
libc/unistd/readlink.c Normal file
View File

@ -0,0 +1,18 @@
#include <unistd.h>
#include <errno.h>
#include <syscall.h>
#include <syscall_nums.h>
DEFN_SYSCALL3(readlink, SYS_READLINK, char *, char *, int);
ssize_t readlink(const char * name, char * buf, size_t len) {
int r = syscall_readlink((char*)name, buf, len);
if (r < 0) {
errno = -r;
return -1;
}
return r;
}

View File

@ -1,163 +0,0 @@
#include <unistd.h>
#include <syscall.h>
#include <va_list.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <signal.h>
#include <string.h>
#ifndef syscall_pipe
DECL_SYSCALL1(pipe, int *);
#endif
#ifndef syscall_readline
DECL_SYSCALL3(readlink, char *, char *, int);
#endif
int open(const char *name, int flags, ...) {
va_list argp;
int mode = 0;
int result;
va_start(argp, flags);
if (flags & O_CREAT) mode = va_arg(argp, int);
va_end(argp);
result = syscall_open(name, flags, mode);
if (result == -1) {
if (flags & O_CREAT) {
errno = EACCES;
} else {
errno = ENOENT;
}
} else if (result < 0) {
errno = -result;
}
return result;
}
int read(int file, void *ptr, size_t len) {
return syscall_read(file,ptr,len);
}
int creat(const char *path, mode_t mode) {
return open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
}
int close(int file) {
return syscall_close(file);
}
int link(const char *old, const char *new) {
errno = EMLINK;
return -1;
}
off_t lseek(int file, off_t ptr, int dir) {
return syscall_lseek(file,ptr,dir);
}
int isatty(int fd) {
int dtype = ioctl(fd, IOCTLDTYPE, NULL);
if (dtype == IOCTL_DTYPE_TTY) return 1;
errno = EINVAL;
return 0;
}
ssize_t write(int file, const void *ptr, size_t len) {
return syscall_write(file,(char *)ptr,len);
}
pid_t getpid(void) {
return syscall_getpid();
}
int dup2(int oldfd, int newfd) {
return syscall_dup2(oldfd, newfd);
}
pid_t fork(void) {
return syscall_fork();
}
void exit(int val) {
_exit(val);
}
int kill(int pid, int sig) {
return syscall_send_signal(pid, sig);
}
sighandler_t signal(int signum, sighandler_t handler) {
return (sighandler_t)syscall_signal(signum, (void *)handler);
}
int uname(struct utsname *__name) {
return syscall_uname((void *)__name);
}
int chdir(const char *path) {
return syscall_chdir((char*)path);
}
char *getcwd(char *buf, size_t size) {
if (!buf) buf = malloc(size);
return (char *)syscall_getcwd(buf, size);
}
char *getwd(char *buf) {
return getcwd(buf, 256);
}
int getuid() {
return syscall_getuid();
}
int getgid() {
return getuid();
}
int getpgrp() {
/* XXX */
return getgid();
}
int geteuid() {
return getuid();
}
int getegid() {
return getgid();
}
int pipe(int fildes[2]) {
int ret = syscall_pipe((int *)fildes);
if (ret < 0) {
errno = -ret;
return -1;
}
return ret;
}
ssize_t readlink(const char * name, char * buf, size_t len) {
int r = syscall_readlink((char*)name, buf, len);
if (r < 0) {
errno = -r;
return -1;
}
return r;
}
int usleep(useconds_t usec) {
syscall_nanosleep(0, usec / 10000);
return 0;
}
int fstat(int file, struct stat *st) {
syscall_fstat(file, st);
return 0;
}

10
libc/unistd/usleep.c Normal file
View File

@ -0,0 +1,10 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL2(nanosleep, 46, unsigned long, unsigned long);
int usleep(useconds_t usec) {
syscall_nanosleep(0, usec / 10000);
return 0;
}

8
libc/unistd/write.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <syscall.h>
DEFN_SYSCALL3(write, 4, int, char *, int);
ssize_t write(int file, const void *ptr, size_t len) {
return syscall_write(file,(char *)ptr,len);
}