summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-08-28 12:33:36 +0200
committerdzwdz2022-08-28 12:33:36 +0200
commitc43b0ac7672b0d8fce8b1ea0a0dbe4383d60485e (patch)
tree6f25808cf0b30141349b55ccbbcb521669d53852 /src/user
parent99e59fccdd98c22e91c0050e0c4e7c5742bdb341 (diff)
user/libc: unlink()
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/shell/builtins.c27
-rw-r--r--src/user/lib/unistd.c19
2 files changed, 18 insertions, 28 deletions
diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c
index 1be9b5b..5af11e4 100644
--- a/src/user/app/shell/builtins.c
+++ b/src/user/app/shell/builtins.c
@@ -192,32 +192,9 @@ static void cmd_rm(int argc, char **argv) {
eprintf("no arguments");
return;
}
- const size_t buflen = PATH_MAX;
- char *buf = malloc(buflen);
for (int i = 1; i < argc; i++) {
- handle_t h;
- long ret;
- size_t abslen;
- char *path = argv[i];
- if (*path == '\0') {
- eprintf("ignoring empty argument");
- continue;
- }
- abslen = absolutepath(buf, path, buflen);
- if (!abslen) {
- eprintf("invalid path %s", path);
- continue;
- }
- h = _syscall_open(buf, abslen - 1, 0);
- if (h < 0) {
- eprintf("couldn't open %s (err %u)", path, -h);
- continue;
- }
- ret = _syscall_remove(h);
- if (ret < 0) {
- eprintf("couldn't remove %s (err %u)", path, -ret);
- continue;
- }
+ if (unlink(argv[i]) < 0)
+ perror(argv[i]);
}
}
diff --git a/src/user/lib/unistd.c b/src/user/lib/unistd.c
index f40b876..1a50c3e 100644
--- a/src/user/lib/unistd.c
+++ b/src/user/lib/unistd.c
@@ -22,10 +22,23 @@ _Noreturn void exit(int c) {
}
_Noreturn void _exit(int c) { exit(c); };
-// TODO unlink
int unlink(const char *path) {
- (void)path;
- errno = ENOSYS;
+ size_t len = strlen(path) + 1;
+ char *abspath = malloc(len);
+ if (!abspath) return -1;
+
+ size_t abslen = absolutepath(abspath, path, len);
+ if (abslen == 0) { errno = EINVAL; goto err; }
+
+ handle_t h = _syscall_open(abspath, abslen - 1, 0);
+ if (h < 0) { errno = -h; goto err; }
+
+ long ret = _syscall_remove(h);
+ if (ret < 0) { errno = -ret; goto err; }
+
+ return 0;
+err:
+ free(abspath);
return -1;
}