BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionLibC / commit / 03048a95d88cc7a78171393371f5c22a0250a014 / unistd

// Related

OrionLibC

Barry Importing existing Orion LibC 03048a9 (2 years, 2 months ago)
diff --git a/unistd/chdir.c b/unistd/chdir.c
new file mode 100644
index 0000000..c146c8c
--- /dev/null
+++ b/unistd/chdir.c
@@ -0,0 +1,15 @@
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Change the process' file system current working directory */
+int
+chdir(const char *path)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_CHDIR),
+	             "c" (1), "S" (&path));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/chroot.c b/unistd/chroot.c
new file mode 100644
index 0000000..e9fd64c
--- /dev/null
+++ b/unistd/chroot.c
@@ -0,0 +1,15 @@
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Change the process' file system root */
+int
+chroot(const char *path)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_CHROOT),
+	             "c" (1), "S" (&path));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/clone.c b/unistd/clone.c
new file mode 100644
index 0000000..f7f4b34
--- /dev/null
+++ b/unistd/clone.c
@@ -0,0 +1,13 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+
+/* Fork the current process */
+pid_t
+clone(int flags)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_CLONE),
+	             "c" (1), "S" (&flags));
+	return ret;
+}
diff --git a/unistd/close.c b/unistd/close.c
new file mode 100644
index 0000000..d2cf9fc
--- /dev/null
+++ b/unistd/close.c
@@ -0,0 +1,15 @@
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Close a file */
+int
+close(int fd)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_CLOSE),
+	             "c" (1), "S" (&fd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/dup.c b/unistd/dup.c
new file mode 100644
index 0000000..a4aabfc
--- /dev/null
+++ b/unistd/dup.c
@@ -0,0 +1,28 @@
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Duplicate an open file descriptor */
+int
+dup(int oldfd)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_DUP),
+	             "c" (1), "S" (&oldfd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
+
+/* Duplicate an open file descriptor to a specified file descriptor */
+int
+dup2(int oldfd, int newfd)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_DUP2),
+	             "c" (2), "S" (&oldfd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/execve.c b/unistd/execve.c
new file mode 100644
index 0000000..6fcf69f
--- /dev/null
+++ b/unistd/execve.c
@@ -0,0 +1,32 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Execute a binary with the specified arguments and environment */
+int
+execve(const char *path, char *argv[], char *envp[])
+{
+	struct {
+		const char *path;
+		char **argv, **envp;
+	} args;
+	args.path = path;
+	args.argv = argv;
+	args.envp = envp;
+
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_EXECVE),
+	             "c" (3), "S" (&args));
+
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
+
+/* Execute a binary with the specified arguments */
+int
+execv(const char *path, char *argv[])
+{
+	return execve(path, argv, NULL); /* FIXME: environment */
+}
diff --git a/unistd/fork.c b/unistd/fork.c
new file mode 100644
index 0000000..f827758
--- /dev/null
+++ b/unistd/fork.c
@@ -0,0 +1,11 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/sched.h>
+
+/* Fork the current process */
+pid_t
+fork(void)
+{
+	return clone(CLONE_NONE);
+}
diff --git a/unistd/getcwd.c b/unistd/getcwd.c
new file mode 100644
index 0000000..0107f2f
--- /dev/null
+++ b/unistd/getcwd.c
@@ -0,0 +1,16 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Get the current working directory's path */
+char *
+getcwd(char *buf, size_t size)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETCWD),
+	             "c" (2), "S" (&buf));
+	if ((char *) ret == buf)
+		return (char *) ret;
+	errno = -ret;
+	return NULL;
+}
diff --git a/unistd/getpid.c b/unistd/getpid.c
new file mode 100644
index 0000000..dc5d86a
--- /dev/null
+++ b/unistd/getpid.c
@@ -0,0 +1,13 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+
+/* Get the current process' id */
+pid_t
+getpid(void)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETPID),
+	             "c" (0), "S" (NULL));
+	return ret;
+}
diff --git a/unistd/getuid.c b/unistd/getuid.c
new file mode 100644
index 0000000..3e7174d
--- /dev/null
+++ b/unistd/getuid.c
@@ -0,0 +1,43 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+
+/* Get the current process' user id */
+uid_t
+getuid(void)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETUID),
+	             "c" (0), "S" (NULL));
+	return ret;
+}
+
+/* Get the current process' effective user id */
+uid_t
+geteuid(void)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETEUID),
+	             "c" (0), "S" (NULL));
+	return ret;
+}
+
+/* Get the current process' group id */
+gid_t
+getgid(void)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETGID),
+	             "c" (0), "S" (NULL));
+	return ret;
+}
+
+/* Get the current process' effective group id */
+gid_t
+getegid(void)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_GETEGID),
+	             "c" (0), "S" (NULL));
+	return ret;
+}
diff --git a/unistd/isatty.c b/unistd/isatty.c
new file mode 100644
index 0000000..823019a
--- /dev/null
+++ b/unistd/isatty.c
@@ -0,0 +1,15 @@
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Check if fd refers to a terminal */
+int
+isatty(int fd)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_ISATTY),
+	             "c" (1), "S" (&fd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return 0;
+}
diff --git a/unistd/lseek.c b/unistd/lseek.c
new file mode 100644
index 0000000..aa6510a
--- /dev/null
+++ b/unistd/lseek.c
@@ -0,0 +1,15 @@
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <errno.h>
+
+off_t
+lseek(int fd, off_t offset, int whence)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_LSEEK),
+	             "c" (3), "S" (&fd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/open.c b/unistd/open.c
new file mode 100644
index 0000000..02603d8
--- /dev/null
+++ b/unistd/open.c
@@ -0,0 +1,30 @@
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <errno.h>
+
+/* Open a file */
+int
+open(const char *name, int flags, ...)
+{
+	int ret;
+	if (flags & O_CREATE) {
+		asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_OPEN),
+		             "c" (3), "S" (&name));
+	} else {
+		asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_OPEN),
+		             "c" (2), "S" (&name));
+	}
+
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
+
+/* Create a file */
+int
+create(const char *name, mode_t mode)
+{
+	return open(name, O_CREATE | O_WRONLY | O_TRUNC, mode);
+}
diff --git a/unistd/read.c b/unistd/read.c
new file mode 100644
index 0000000..f4d2d75
--- /dev/null
+++ b/unistd/read.c
@@ -0,0 +1,16 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Read from a file */
+int
+read(int fd, void *buf, size_t count)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_READ),
+	             "c" (3), "S" (&fd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}
diff --git a/unistd/setuid.c b/unistd/setuid.c
new file mode 100644
index 0000000..7dcca04
--- /dev/null
+++ b/unistd/setuid.c
@@ -0,0 +1,43 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+
+/* Set the current process' user id */
+int
+setuid(uid_t uid)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SETUID),
+	             "c" (1), "S" (&uid));
+	return ret;
+}
+
+/* Set the current process' effective user id */
+int
+seteuid(uid_t euid)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SETEUID),
+	             "c" (1), "S" (&euid));
+	return ret;
+}
+
+/* Set the current process' group id */
+int
+setgid(gid_t gid)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SETGID),
+	             "c" (1), "S" (&gid));
+	return ret;
+}
+
+/* Set the current process' effective group id */
+int
+setegid(gid_t egid)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SETEGID),
+	             "c" (1), "S" (&egid));
+	return ret;
+}
diff --git a/unistd/write.c b/unistd/write.c
new file mode 100644
index 0000000..90862b2
--- /dev/null
+++ b/unistd/write.c
@@ -0,0 +1,16 @@
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <errno.h>
+
+/* Write to a file */
+int
+write(int fd, void *buf, size_t count)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_WRITE),
+	             "c" (3), "S" (&fd));
+	if (ret >= 0)
+		return ret;
+	errno = -ret;
+	return -1;
+}