From f5a76d8eaba565e2a532faf659efe2848d605163 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Fri, 10 Feb 2023 16:02:19 +0100 Subject: add rm (using rmdir and unlink syscalls) --- core/rm.c | 25 +++++++++++++++++++++++++ lib/sys/rmdir.h | 11 +++++++++++ lib/sys/unlink.h | 11 +++++++++++ 3 files changed, 47 insertions(+) create mode 100644 core/rm.c create mode 100644 lib/sys/rmdir.h create mode 100644 lib/sys/unlink.h diff --git a/core/rm.c b/core/rm.c new file mode 100644 index 0000000..a9ea996 --- /dev/null +++ b/core/rm.c @@ -0,0 +1,25 @@ +#include "../lib/sys/rmdir.h" +#include "../lib/sys/unlink.h" +#include "../lib/sys/stat.h" +#include "../lib/sys/errno.h" +#include "../lib/io/io.h" + +int main(int argc, char **argv) +{ + stat_t st; + int err; + for (int i = 1; i < argc; ++i) { + if ((err = stat(argv[i], &st)) < 0) { + wff(STDERR_FD, "rm: %s %s\n", argv[i], errstr[-err]); + continue; + } + + if (st.mode & S_IFREG) { + unlink(argv[i]); + } else if (st.mode & S_IFDIR) { + rmdir(argv[i]); + } else { + wff(STDERR_FD, "rm %s not of type 'regular file' or 'directory'\n", argv[i]); + } + } +} diff --git a/lib/sys/rmdir.h b/lib/sys/rmdir.h new file mode 100644 index 0000000..1656ee9 --- /dev/null +++ b/lib/sys/rmdir.h @@ -0,0 +1,11 @@ +#ifndef RMDIR_H +#define RMDIR_H + +#include "syscalls.h" + +int rmdir(const char *pathname) +{ + return syscall(RMDIR, pathname); +} + +#endif diff --git a/lib/sys/unlink.h b/lib/sys/unlink.h new file mode 100644 index 0000000..a0f66e3 --- /dev/null +++ b/lib/sys/unlink.h @@ -0,0 +1,11 @@ +#ifndef UNLINK_H +#define UNLINK_H + +#include "syscalls.h" + +int unlink(const char *pathname) +{ + return syscall(UNLINK, pathname); +} + +#endif -- cgit v1.2.3-70-g09d2