aboutsummaryrefslogtreecommitdiff
path: root/pk/file.c
diff options
context:
space:
mode:
authorLuís Marques <luismarques@lowrisc.org>2019-10-22 18:08:57 +0100
committerAndrew Waterman <andrew@sifive.com>2019-10-22 10:08:57 -0700
commit43969b149eb3be5fe18daa7c69a02467e6f16d15 (patch)
tree7dab24aa1df371e12a0a1dd924c36311441d51c7 /pk/file.c
parent15329a8aac5d0f1f1fea01c68230421147c8b50b (diff)
downloadpk-43969b149eb3be5fe18daa7c69a02467e6f16d15.zip
pk-43969b149eb3be5fe18daa7c69a02467e6f16d15.tar.gz
pk-43969b149eb3be5fe18daa7c69a02467e6f16d15.tar.bz2
Implement dup3 syscall (#162)
Diffstat (limited to 'pk/file.c')
-rw-r--r--pk/file.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/pk/file.c b/pk/file.c
index 53087c3..502ff6a 100644
--- a/pk/file.c
+++ b/pk/file.c
@@ -53,6 +53,20 @@ int file_dup(file_t* f)
return -1;
}
+int file_dup3(file_t* f, int newfd)
+{
+ if (newfd < 0 || newfd >= MAX_FDS)
+ return -1;
+
+ if (atomic_cas(&fds[newfd], 0, f) == 0)
+ {
+ file_incref(f);
+ return newfd;
+ }
+
+ return -1;
+}
+
void file_init()
{
// create stdin, stdout, stderr and FDs 0-2