#ifndef USR_H
#define USR_H
#define SC_EXIT 1
#define SC_FORK 2
#define SC_EXEC 3
#define SC_GETPID 4
#define SC_GETPGID 5
#define SC_SETPGID 6
#define SC_CHDIR 7
#define SC_SBRK 8
#define SC_SLEEP 9
#define SC_WAIT 10
#define SC_KILL 11
#define SC_UNAME 12
#define SC_OPEN 13
#define SC_CLOSE 14
#define SC_DUP 15
#define SC_DUP2 16
#define SC_PIPE 17
#define SC_FCNTL 18
#define SC_READ 19
#define SC_WRITE 20
#define SC_SEEK 21
#define SC_STAT 22
#define SC_IOCTL 23
#define SC_CREATE 24
#define SC_REMOVE 25
#define SC_MKDIR 26
#define SC_RMDIR 27
#define SC_RENAME 28
#define SC_MOUNT 29
#define SC_UMOUNT 30
#define SC_REBOOT 31
#define SC_GETTIME 32
#define SC_MMAP 33
#define SC_MUNMAP 34
#define SC_MPROTECT 35
extern int errno;
static inline int _syscall0(unsigned int nr)
{
register int retval;
asm volatile ("int $0x80"
: "=a" (retval)
: "0" (nr)
: "memory");
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
#define syscall0(nr) _syscall0(nr)
static inline int _syscall1(unsigned int nr, unsigned long a)
{
register int retval;
asm volatile ("int $0x80"
: "=a" (retval)
: "0" (nr), "b" (a)
: "memory");
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
#define syscall1(nr, a) _syscall1(nr, (unsigned long)a)
static inline int _syscall2(unsigned int nr, unsigned long a, unsigned long b)
{
register int retval;
asm volatile ("int $0x80"
: "=a" (retval)
: "0" (nr), "b" (a), "c" (b)
: "memory");
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
#define syscall2(nr, a, b) _syscall2(nr, (unsigned long)a, (unsigned long)b)
static inline int _syscall3(unsigned int nr, unsigned long a, unsigned long b, unsigned long c)
{
register int retval;
asm volatile ("int $0x80"
: "=a" (retval)
: "0" (nr), "b" (a), "c" (b), "d" (c)
: "memory");
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
#define syscall3(nr, a, b, c) _syscall3(nr, (unsigned long)a, (unsigned long)b, (unsigned long)c)
static inline void exit(int status)
{
syscall1(SC_EXIT, status);
}
static inline int fork()
{
return syscall0(SC_FORK);
}
static inline int exec(const char *filename, char *const argv[], char *const envp[])
{
return syscall3(SC_EXEC, filename, argv, envp);
}
static inline int getpgid()
{
return syscall0(SC_GETPGID);
}
static inline int setpgid(int pid, int pgid)
{
return syscall2(SC_SETPGID, pid, pgid);
}
static inline int chdir(const char *path)
{
return syscall1(SC_CHDIR, path);
}
static inline unsigned int sleep(unsigned int seconds)
{
return syscall1(SC_SLEEP, seconds);
}
static inline int wait(int pid, int *status, int options)
{
return syscall3(SC_WAIT, pid, status, options);
}
static inline int kill(int pid, int sig)
{
return syscall2(SC_KILL, pid, sig);
}
static inline int open(const char *filename, int flags)
{
return syscall2(SC_OPEN, filename, flags);
}
static inline int close(int fd)
{
return syscall1(SC_CLOSE, fd);
}
static inline int write(int fd, const char *buf, unsigned int count)
{
return syscall3(SC_WRITE, fd, buf, count);
}
static inline int ioctl(int fd, int request, void *arg)
{
return syscall3(SC_IOCTL, fd, request, arg);
}
static inline int mount(const char *device, const char *dir, const char *type)
{
return syscall3(SC_MOUNT, device, dir, type);
}
static inline int umount(const char *dir)
{
return syscall1(SC_UMOUNT, dir);
}
static inline int reboot(int cmd)
{
return syscall1(SC_REBOOT, cmd);
}
#define EXIT_FAILURE 1
int printf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
void perror(const char *s);
#endif